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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Luticate2.Utils.Dbo.Filter;
  6. using Luticate2.Utils.Dbo.OrderBy;
  7. using Luticate2.Utils.Dbo.Result;
  8. using Luticate2.Utils.Interfaces;
  9. namespace Luticate2.Utils.Business
  10. {
  11. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate, TId> :
  12. LuBusiness,
  13. ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  14. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  15. where TDboCreate : class
  16. where TDboRead : class
  17. where TDboUpdate : class
  18. {
  19. protected readonly TDataAccess DataAccess;
  20. private readonly LuNotificationsBusiness _notificationsBusiness;
  21. protected string EntityType = null;
  22. protected LuCrudBusiness(TDataAccess dataAccess, LuNotificationsBusiness notificationsBusiness)
  23. {
  24. DataAccess = dataAccess;
  25. _notificationsBusiness = notificationsBusiness;
  26. }
  27. protected virtual string GetEntityType()
  28. {
  29. return EntityType;
  30. }
  31. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  32. {
  33. return LuResult<TDboCreate>.Ok(obj);
  34. }
  35. protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
  36. {
  37. var list = new List<TDboCreate>();
  38. foreach (var obj in objs)
  39. {
  40. var res = CheckAdd(obj);
  41. if (!res)
  42. {
  43. return res.To<IEnumerable<TDboCreate>>();
  44. }
  45. list.Add(res.Data);
  46. }
  47. return LuResult<IEnumerable<TDboCreate>>.Ok(list);
  48. }
  49. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  50. {
  51. return LuResult<TDboUpdate>.Ok(update);
  52. }
  53. protected LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
  54. {
  55. var res = GetSingleById(id);
  56. if (!res)
  57. {
  58. return res.To<TDboUpdate>();
  59. }
  60. return CheckEdit(res.Data, update);
  61. }
  62. public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  63. {
  64. var res = CheckAdd(objs);
  65. if (!res)
  66. {
  67. return res.To<T>();
  68. }
  69. return DataAccess.Add(res.Data, returnFunc);
  70. }
  71. public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  72. {
  73. var res = CheckAdd(obj);
  74. if (!res)
  75. {
  76. return res.To<T>();
  77. }
  78. var newEntity = DataAccess.AddDbo(res.Data);
  79. if (newEntity)
  80. {
  81. _notificationsBusiness.NotifyCreate(GetEntityType(), newEntity.Data);
  82. return LuResult<T>.Ok(returnFunc(newEntity.Data));
  83. }
  84. return newEntity.To<T>();
  85. }
  86. public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
  87. {
  88. return Add(objs, reads => reads.Select(read => (TId)((dynamic)read).Id));
  89. }
  90. public virtual LuResult<TId> AddId(TDboCreate obj)
  91. {
  92. return Add(obj, read => (TId)((dynamic)read).Id);
  93. }
  94. public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> objs)
  95. {
  96. return Add(objs, reads => reads);
  97. }
  98. public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
  99. {
  100. return Add(obj, read => read);
  101. }
  102. public virtual LuResult<TDboRead> GetSingleById(TId id)
  103. {
  104. return DataAccess.GetSingleById(id);
  105. }
  106. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
  107. int page = 0, int perPage = int.MaxValue)
  108. {
  109. return DataAccess.GetMultiple(orderBy, filter, page, perPage);
  110. }
  111. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
  112. int perPage = int.MaxValue)
  113. {
  114. return DataAccess.GetMultiple(orderBy, page, perPage);
  115. }
  116. public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  117. {
  118. var originalEntity = GetSingleById(id);
  119. if (!originalEntity)
  120. {
  121. return originalEntity.To<T>();
  122. }
  123. var obj = CheckEdit(originalEntity.Data, update);
  124. if (!obj)
  125. {
  126. return obj.To<T>();
  127. }
  128. var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
  129. if (editedEntity)
  130. {
  131. _notificationsBusiness.NotifyUpdate(GetEntityType(), originalEntity.Data, editedEntity.Data);
  132. return LuResult<T>.Ok(returnFunc(editedEntity.Data));
  133. }
  134. return editedEntity.To<T>();
  135. }
  136. public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
  137. {
  138. return EditSingleById(id, update, read => (TId)((dynamic)read).Id);
  139. }
  140. public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
  141. {
  142. return EditSingleById(id, update, read => read);
  143. }
  144. public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  145. {
  146. var originalEntity = GetSingleById(id);
  147. if (!originalEntity)
  148. {
  149. return originalEntity.To<T>();
  150. }
  151. var res = DataAccess.DeleteSingleById(id, returnFunc);
  152. if (res)
  153. {
  154. _notificationsBusiness.NotifyDelete(GetEntityType(), originalEntity.Data);
  155. }
  156. return res;
  157. }
  158. public virtual LuResult<TId> DeleteSingleByIdId(TId id)
  159. {
  160. return DeleteSingleById(id, read => (TId)((dynamic)read).Id);
  161. }
  162. public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
  163. {
  164. return DeleteSingleById(id, read => read);
  165. }
  166. }
  167. }