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.

LuCrudBusiness.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 virtual string EntityType { get; set; }
  22. protected virtual Func<string, TDboRead, bool> NotifyCreateFilter { get; set; }
  23. protected virtual Func<string, TDboRead, TDboRead, bool> NotifyUpdateFilter { get; set; }
  24. protected virtual Func<string, TDboRead, bool> NotifyDeleteFilter { get; set; }
  25. protected LuCrudBusiness(TDataAccess dataAccess, LuNotificationsBusiness notificationsBusiness)
  26. {
  27. DataAccess = dataAccess;
  28. _notificationsBusiness = notificationsBusiness;
  29. }
  30. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  31. {
  32. return LuResult<TDboCreate>.Ok(obj);
  33. }
  34. protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
  35. {
  36. var list = new List<TDboCreate>();
  37. foreach (var obj in objs)
  38. {
  39. var res = CheckAdd(obj);
  40. if (!res)
  41. {
  42. return res.To<IEnumerable<TDboCreate>>();
  43. }
  44. list.Add(res.Data);
  45. }
  46. return LuResult<IEnumerable<TDboCreate>>.Ok(list);
  47. }
  48. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  49. {
  50. return LuResult<TDboUpdate>.Ok(update);
  51. }
  52. protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
  53. {
  54. var res = GetSingleById(id);
  55. if (!res)
  56. {
  57. return res.To<TDboUpdate>();
  58. }
  59. return CheckEdit(res.Data, update);
  60. }
  61. public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  62. {
  63. var res = CheckAdd(objs);
  64. if (!res)
  65. {
  66. return res.To<T>();
  67. }
  68. return DataAccess.Add(res.Data, returnFunc);
  69. }
  70. public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  71. {
  72. var res = CheckAdd(obj);
  73. if (!res)
  74. {
  75. return res.To<T>();
  76. }
  77. var newEntity = DataAccess.AddDbo(res.Data);
  78. if (newEntity)
  79. {
  80. _notificationsBusiness.NotifyCreate(EntityType, newEntity.Data,
  81. NotifyCreateFilter != null ? s => NotifyCreateFilter(s, newEntity.Data) : (Func<string, bool>) null);
  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 GetMultiple(orderBy, new LuFilterDbo { Query = ""}, 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(EntityType, originalEntity.Data, editedEntity.Data,
  132. NotifyUpdateFilter != null ? s => NotifyUpdateFilter(s, originalEntity.Data, editedEntity.Data) : (Func<string, bool>) null);
  133. return LuResult<T>.Ok(returnFunc(editedEntity.Data));
  134. }
  135. return editedEntity.To<T>();
  136. }
  137. public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
  138. {
  139. return EditSingleById(id, update, read => (TId)((dynamic)read).Id);
  140. }
  141. public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
  142. {
  143. return EditSingleById(id, update, read => read);
  144. }
  145. public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  146. {
  147. var originalEntity = GetSingleById(id);
  148. if (!originalEntity)
  149. {
  150. return originalEntity.To<T>();
  151. }
  152. var res = DataAccess.DeleteSingleById(id, returnFunc);
  153. if (res)
  154. {
  155. _notificationsBusiness.NotifyDelete(EntityType, originalEntity.Data,
  156. NotifyDeleteFilter != null ? s => NotifyDeleteFilter(s, originalEntity.Data) : (Func<string, bool>) null);
  157. }
  158. return res;
  159. }
  160. public virtual LuResult<TId> DeleteSingleByIdId(TId id)
  161. {
  162. return DeleteSingleById(id, read => (TId)((dynamic)read).Id);
  163. }
  164. public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
  165. {
  166. return DeleteSingleById(id, read => read);
  167. }
  168. }
  169. }