Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using AutoMapper;
  2. using Ecole.Core.Models;
  3. using Ecole.DATA.Provider.SQL.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Ecole.DATA.Provider.SQL.Repo
  11. {
  12. public class CoursRepo
  13. {
  14. private readonly EcoleDBContext ctx;
  15.  
  16. public CoursRepo()
  17. {
  18. ctx = new EcoleDBContext();
  19. }
  20.  
  21. private int Save()
  22. {
  23. try
  24. {
  25. return ctx.SaveChanges();
  26. }
  27. catch (Exception ex)
  28. {
  29.  
  30. throw new Exception(ex.Message);
  31. }
  32. }
  33.  
  34. public CoursDTO GetById (int id)
  35. {
  36. return Mapper.Map<CoursDTO>(ctx.Courss.Find(id));
  37. }
  38.  
  39. public IEnumerable<CoursDTO> GetAll()
  40. {
  41. return Mapper.Map<IEnumerable<CoursDTO>>(ctx.Courss.ToList());
  42. }
  43.  
  44. public CoursDTO Add (Cours c)
  45. {
  46. if (c == null)
  47. {
  48. return null;
  49. }
  50. ctx.Courss.Add(c);
  51. Save();
  52. return Mapper.Map<CoursDTO>(c);
  53. }
  54.  
  55. public int Update (CoursDTO c)
  56. {
  57. if (c == null)
  58. {
  59. return 0;
  60. }
  61.  
  62. var temp = GetById(c.CoursId);
  63.  
  64. if (temp != null)
  65. {
  66. ctx.Entry(temp).CurrentValues.SetValues(Mapper.Map<Cours>(c));
  67. return Save();
  68. }
  69. return 0;
  70. }
  71.  
  72. public int Delete (int id)
  73. {
  74. var cours = GetById(id);
  75.  
  76. if (cours != null)
  77. {
  78. ctx.Courss.Remove(Mapper.Map<Cours>(cours));
  79. return Save();
  80. }
  81. return 0;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement