API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AgentRepository.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Context;
  6. using UnivateProperties_API.Model.Users;
  7. namespace UnivateProperties_API.Repository.Users
  8. {
  9. public class AgentRepository : IRepository<Agent>
  10. {
  11. private readonly DataContext _dbContext;
  12. public AgentRepository(DataContext dbContext)
  13. {
  14. _dbContext = dbContext;
  15. }
  16. public List<Agent> Get(Func<Agent, bool> where)
  17. {
  18. return _dbContext.Agents.Where(where).ToList();
  19. }
  20. public List<Agent> GetAll()
  21. {
  22. return _dbContext.Agents.ToList();
  23. }
  24. public Agent GetDetailed(Func<Agent, bool> first)
  25. {
  26. var item = _dbContext.Agents.FirstOrDefault(first);
  27. AgentRepository account = new AgentRepository(_dbContext);
  28. item = GetDetailedObject(item, account);
  29. return item;
  30. }
  31. private Agent GetDetailedObject(Agent item, AgentRepository repo)
  32. {
  33. item = repo.GetDetailed(x => x.Id == item.Id);
  34. return item;
  35. }
  36. public void Insert(Agent item)
  37. {
  38. if (item.AgencyId != 0 && item.Agency == null)
  39. {
  40. AgencyRepository arepo = new AgencyRepository(_dbContext);
  41. item.Agency = arepo.Get(a => a.Id == item.AgencyId).FirstOrDefault();
  42. }
  43. _dbContext.Add(item);
  44. Save();
  45. }
  46. public void Insert(IEnumerable<Agent> item)
  47. {
  48. foreach (var i in item)
  49. {
  50. _dbContext.Add(item);
  51. }
  52. Save();
  53. }
  54. public void Remove(Agent item)
  55. {
  56. var i = _dbContext.Agents.Find(item);
  57. _dbContext.Agents.Remove(i);
  58. Save();
  59. }
  60. public void Remove(IEnumerable<Agent> items)
  61. {
  62. foreach (var item in items)
  63. {
  64. Agent i = _dbContext.Agents.Find(item);
  65. _dbContext.Agents.Remove(i);
  66. }
  67. Save();
  68. }
  69. public void RemoveAtId(int item)
  70. {
  71. var i = _dbContext.Agents.Find(item);
  72. _dbContext.Agents.Remove(i);
  73. Save();
  74. }
  75. public void Update(Agent item)
  76. {
  77. _dbContext.Entry(item).State = EntityState.Modified;
  78. Save();
  79. }
  80. public void Save()
  81. {
  82. _dbContext.SaveChanges();
  83. }
  84. public List<Agent> GetDetailedAll()
  85. {
  86. // TODO: GetDetailed
  87. throw new NotImplementedException();
  88. }
  89. }
  90. }