API
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AgencyRepository.cs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 AgencyRepository : IRepository<Agency>
  10. {
  11. private readonly DataContext _dbContext;
  12. public AgencyRepository(DataContext dbContext)
  13. {
  14. _dbContext = dbContext;
  15. }
  16. public List<Agency> Get(Func<Agency, bool> where)
  17. {
  18. return _dbContext.Agencies.Where(where).ToList();
  19. }
  20. public List<Agency> GetAll()
  21. {
  22. return _dbContext.Agencies.ToList();
  23. }
  24. public Agency GetDetailed(Func<Agency, bool> first)
  25. {
  26. var item = _dbContext.Agencies.FirstOrDefault(first);
  27. //AgencyRepository account = new AgencyRepository(_dbContext);
  28. //item = GetDetailedObject(item, account);
  29. return item;
  30. }
  31. public void Insert(Agency item)
  32. {
  33. item.Id = NewId();
  34. _dbContext.Add(item);
  35. Save();
  36. }
  37. public void Insert(IEnumerable<Agency> item)
  38. {
  39. int id = NewId();
  40. foreach (var i in item)
  41. {
  42. i.Id = id;
  43. _dbContext.Add(i);
  44. id += 1;
  45. }
  46. Save();
  47. }
  48. public void Remove(Agency item)
  49. {
  50. var i = _dbContext.Agencies.Find(item);
  51. _dbContext.Agencies.Remove(i);
  52. Save();
  53. }
  54. public void Remove(IEnumerable<Agency> items)
  55. {
  56. foreach (var item in items)
  57. {
  58. Agency i = _dbContext.Agencies.Find(item);
  59. _dbContext.Agencies.Remove(i);
  60. }
  61. Save();
  62. }
  63. public void RemoveAtId(int item)
  64. {
  65. var i = _dbContext.Agencies.Find(item);
  66. _dbContext.Agencies.Remove(i);
  67. Save();
  68. }
  69. public void Update(Agency item)
  70. {
  71. _dbContext.Entry(item).State = EntityState.Modified;
  72. Save();
  73. }
  74. public void Save()
  75. {
  76. _dbContext.SaveChanges();
  77. }
  78. public List<Agency> GetDetailedAll()
  79. {
  80. //TODO: GetDetailed Agency
  81. throw new NotImplementedException();
  82. }
  83. public int NewId()
  84. {
  85. int id = 0;
  86. if (_dbContext.Agencies.Count() > 0)
  87. {
  88. id = _dbContext.Agencies.Max(x => x.Id);
  89. }
  90. id += 1;
  91. return id;
  92. }
  93. }
  94. }