Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AutoMapper;
- using Ecole.Core.Models;
- using Ecole.DATA.Provider.SQL.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ecole.DATA.Provider.SQL.Repo
- {
- public class CoursRepo
- {
- private readonly EcoleDBContext ctx;
- public CoursRepo()
- {
- ctx = new EcoleDBContext();
- }
- private int Save()
- {
- try
- {
- return ctx.SaveChanges();
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- public CoursDTO GetById (int id)
- {
- return Mapper.Map<CoursDTO>(ctx.Courss.Find(id));
- }
- public IEnumerable<CoursDTO> GetAll()
- {
- return Mapper.Map<IEnumerable<CoursDTO>>(ctx.Courss.ToList());
- }
- public CoursDTO Add (Cours c)
- {
- if (c == null)
- {
- return null;
- }
- ctx.Courss.Add(c);
- Save();
- return Mapper.Map<CoursDTO>(c);
- }
- public int Update (CoursDTO c)
- {
- if (c == null)
- {
- return 0;
- }
- var temp = GetById(c.CoursId);
- if (temp != null)
- {
- ctx.Entry(temp).CurrentValues.SetValues(Mapper.Map<Cours>(c));
- return Save();
- }
- return 0;
- }
- public int Delete (int id)
- {
- var cours = GetById(id);
- if (cours != null)
- {
- ctx.Courss.Remove(Mapper.Map<Cours>(cours));
- return Save();
- }
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement