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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using Luticate2.Utils.Dbo;
  4. using Luticate2.Utils.Dbo.OrderBy;
  5. using Luticate2.Utils.Interfaces;
  6. namespace Luticate2.Utils.Business
  7. {
  8. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  9. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  10. where TDboCreate : class
  11. where TDboRead : class
  12. where TDboUpdate : class
  13. {
  14. protected readonly TDataAccess DataAccess;
  15. protected LuCrudBusiness(TDataAccess dataAccess)
  16. {
  17. DataAccess = dataAccess;
  18. }
  19. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  20. {
  21. return LuResult<TDboCreate>.Ok(obj);
  22. }
  23. protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
  24. {
  25. var list = new List<TDboCreate>();
  26. foreach (var obj in objs)
  27. {
  28. var res = CheckAdd(obj);
  29. if (!res)
  30. {
  31. return res.To<IEnumerable<TDboCreate>>();
  32. }
  33. list.Add(res.Data);
  34. }
  35. return LuResult<IEnumerable<TDboCreate>>.Ok(list);
  36. }
  37. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  38. {
  39. return LuResult<TDboUpdate>.Ok(update);
  40. }
  41. protected LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
  42. {
  43. var res = GetSingleById(id);
  44. if (!res)
  45. {
  46. return res.To<TDboUpdate>();
  47. }
  48. return CheckEdit(res.Data, update);
  49. }
  50. protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
  51. {
  52. var res = GetSingleById(id);
  53. if (!res)
  54. {
  55. return res.To<TDboUpdate>();
  56. }
  57. return CheckEdit(res.Data, update);
  58. }
  59. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  60. {
  61. var res = CheckAdd(objs);
  62. if (!res)
  63. {
  64. return res.To<T>();
  65. }
  66. return DataAccess.Add(res.Data, returnFunc);
  67. }
  68. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  69. {
  70. var res = CheckAdd(obj);
  71. if (!res)
  72. {
  73. return res.To<T>();
  74. }
  75. return DataAccess.Add(res.Data, returnFunc);
  76. }
  77. public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
  78. {
  79. var res = CheckAdd(objs);
  80. if (!res)
  81. {
  82. return res.To<IEnumerable<string>>();
  83. }
  84. return DataAccess.AddGuid(res.Data);
  85. }
  86. public LuResult<string> AddGuid(TDboCreate obj)
  87. {
  88. var res = CheckAdd(obj);
  89. if (!res)
  90. {
  91. return res.To<string>();
  92. }
  93. return DataAccess.AddGuid(res.Data);
  94. }
  95. public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
  96. {
  97. var res = CheckAdd(obj);
  98. if (!res)
  99. {
  100. return res.To<IEnumerable<long>>();
  101. }
  102. return DataAccess.AddId(res.Data);
  103. }
  104. public LuResult<long> AddId(TDboCreate obj)
  105. {
  106. var res = CheckAdd(obj);
  107. if (!res)
  108. {
  109. return res.To<long>();
  110. }
  111. return DataAccess.AddId(res.Data);
  112. }
  113. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  114. {
  115. var res = CheckAdd(obj);
  116. if (!res)
  117. {
  118. return res.To<IEnumerable<TDboRead>>();
  119. }
  120. return DataAccess.AddDbo(res.Data);
  121. }
  122. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  123. {
  124. var res = CheckAdd(obj);
  125. if (!res)
  126. {
  127. return res.To<TDboRead>();
  128. }
  129. return DataAccess.AddDbo(res.Data);
  130. }
  131. public LuResult<TDboRead> GetSingleById(string id)
  132. {
  133. return DataAccess.GetSingleById(id);
  134. }
  135. public LuResult<TDboRead> GetSingleById(long id)
  136. {
  137. return DataAccess.GetSingleById(id);
  138. }
  139. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
  140. {
  141. return DataAccess.GetMultiple(orderBy, 0, perPage);
  142. }
  143. public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  144. {
  145. var obj = GetAndCheckEdit(id, update);
  146. if (!obj)
  147. {
  148. return obj.To<T>();
  149. }
  150. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  151. }
  152. public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
  153. {
  154. var obj = GetAndCheckEdit(id, update);
  155. if (!obj)
  156. {
  157. return obj.To<long>();
  158. }
  159. return DataAccess.EditSingleByIdId(id, obj.Data);
  160. }
  161. public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
  162. {
  163. var obj = GetAndCheckEdit(id, update);
  164. if (!obj)
  165. {
  166. return obj.To<TDboRead>();
  167. }
  168. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  169. }
  170. public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  171. {
  172. var obj = GetAndCheckEdit(id, update);
  173. if (!obj)
  174. {
  175. return obj.To<T>();
  176. }
  177. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  178. }
  179. public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
  180. {
  181. var obj = GetAndCheckEdit(id, update);
  182. if (!obj)
  183. {
  184. return obj.To<string>();
  185. }
  186. return DataAccess.EditSingleByIdGuid(id, obj.Data);
  187. }
  188. public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
  189. {
  190. var obj = GetAndCheckEdit(id, update);
  191. if (!obj)
  192. {
  193. return obj.To<TDboRead>();
  194. }
  195. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  196. }
  197. public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
  198. {
  199. return DataAccess.DeleteSingleById(id, returnFunc);
  200. }
  201. public LuResult<string> DeleteSingleByIdGuid(string id)
  202. {
  203. return DataAccess.DeleteSingleByIdGuid(id);
  204. }
  205. public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
  206. {
  207. return DataAccess.DeleteSingleByIdDbo(id);
  208. }
  209. public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
  210. {
  211. return DataAccess.DeleteSingleById(id, returnFunc);
  212. }
  213. public LuResult<long> DeleteSingleByIdId(long id)
  214. {
  215. return DataAccess.DeleteSingleByIdId(id);
  216. }
  217. public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
  218. {
  219. return DataAccess.DeleteSingleByIdDbo(id);
  220. }
  221. }
  222. }