Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LuCrudBusiness.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using Luticate2.Utils.Dbo;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Luticate2.Utils.Dbo.OrderBy;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Luticate2.Utils.Interfaces;
  8. namespace Luticate2.Utils.Business
  9. {
  10. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  11. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  12. where TDboCreate : class
  13. where TDboRead : class
  14. where TDboUpdate : class
  15. {
  16. protected readonly TDataAccess DataAccess;
  17. protected LuCrudBusiness(TDataAccess dataAccess)
  18. {
  19. DataAccess = dataAccess;
  20. }
  21. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  22. {
  23. return LuResult<TDboCreate>.Ok(obj);
  24. }
  25. protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
  26. {
  27. var list = new List<TDboCreate>();
  28. foreach (var obj in objs)
  29. {
  30. var res = CheckAdd(obj);
  31. if (!res)
  32. {
  33. return res.To<IEnumerable<TDboCreate>>();
  34. }
  35. list.Add(res.Data);
  36. }
  37. return LuResult<IEnumerable<TDboCreate>>.Ok(list);
  38. }
  39. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  40. {
  41. return LuResult<TDboUpdate>.Ok(update);
  42. }
  43. protected virtual LuResult<TDboRead> CheckGet(TDboRead obj)
  44. {
  45. return LuResult<TDboRead>.Ok(obj);
  46. }
  47. protected virtual LuResult<TDboRead> CheckDelete(TDboRead obj)
  48. {
  49. return LuResult<TDboRead>.Ok(obj);
  50. }
  51. protected LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
  52. {
  53. var res = GetSingleById(id);
  54. if (!res)
  55. {
  56. return res.To<TDboUpdate>();
  57. }
  58. return CheckEdit(res.Data, update);
  59. }
  60. protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
  61. {
  62. var res = GetSingleById(id);
  63. if (!res)
  64. {
  65. return res.To<TDboUpdate>();
  66. }
  67. return CheckEdit(res.Data, update);
  68. }
  69. protected LuResult<TDboRead> GetAndCheckGet(string id)
  70. {
  71. var res = GetSingleById(id);
  72. if (!res)
  73. {
  74. return res.To<TDboRead>();
  75. }
  76. return CheckGet(res.Data);
  77. }
  78. protected LuResult<TDboRead> GetAndCheckGet(long id)
  79. {
  80. var res = GetSingleById(id);
  81. if (!res)
  82. {
  83. return res.To<TDboRead>();
  84. }
  85. return CheckGet(res.Data);
  86. }
  87. protected LuResult<TDboRead> GetAndCheckDelete(string id)
  88. {
  89. var res = GetSingleById(id);
  90. if (!res)
  91. {
  92. return res.To<TDboRead>();
  93. }
  94. return CheckDelete(res.Data);
  95. }
  96. protected LuResult<TDboRead> GetAndCheckDelete(long id)
  97. {
  98. var res = GetSingleById(id);
  99. if (!res)
  100. {
  101. return res.To<TDboRead>();
  102. }
  103. return CheckDelete(res.Data);
  104. }
  105. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  106. {
  107. var res = CheckAdd(objs);
  108. if (!res)
  109. {
  110. return res.To<T>();
  111. }
  112. return DataAccess.Add(res.Data, returnFunc);
  113. }
  114. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  115. {
  116. var res = CheckAdd(obj);
  117. if (!res)
  118. {
  119. return res.To<T>();
  120. }
  121. return DataAccess.Add(res.Data, returnFunc);
  122. }
  123. public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
  124. {
  125. var res = CheckAdd(objs);
  126. if (!res)
  127. {
  128. return res.To<IEnumerable<string>>();
  129. }
  130. return DataAccess.AddGuid(res.Data);
  131. }
  132. public LuResult<string> AddGuid(TDboCreate obj)
  133. {
  134. var res = CheckAdd(obj);
  135. if (!res)
  136. {
  137. return res.To<string>();
  138. }
  139. return DataAccess.AddGuid(res.Data);
  140. }
  141. public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
  142. {
  143. var res = CheckAdd(obj);
  144. if (!res)
  145. {
  146. return res.To<IEnumerable<long>>();
  147. }
  148. return DataAccess.AddId(res.Data);
  149. }
  150. public LuResult<long> AddId(TDboCreate obj)
  151. {
  152. var res = CheckAdd(obj);
  153. if (!res)
  154. {
  155. return res.To<long>();
  156. }
  157. return DataAccess.AddId(res.Data);
  158. }
  159. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  160. {
  161. var res = CheckAdd(obj);
  162. if (!res)
  163. {
  164. return res.To<IEnumerable<TDboRead>>();
  165. }
  166. return DataAccess.AddDbo(res.Data);
  167. }
  168. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  169. {
  170. var res = CheckAdd(obj);
  171. if (!res)
  172. {
  173. return res.To<TDboRead>();
  174. }
  175. return DataAccess.AddDbo(res.Data);
  176. }
  177. public LuResult<TDboRead> GetSingleById(string id)
  178. {
  179. var res = GetAndCheckGet(id);
  180. if (!res)
  181. {
  182. return res.To<TDboRead>();
  183. }
  184. return DataAccess.GetSingleById(id);
  185. }
  186. public LuResult<TDboRead> GetSingleById(long id)
  187. {
  188. var res = GetAndCheckGet(id);
  189. if (!res)
  190. {
  191. return res.To<TDboRead>();
  192. }
  193. return DataAccess.GetSingleById(id);
  194. }
  195. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
  196. int perPage = int.MaxValue)
  197. {
  198. return DataAccess.GetMultiple(orderBy, 0, perPage);
  199. }
  200. public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  201. {
  202. var obj = GetAndCheckEdit(id, update);
  203. if (!obj)
  204. {
  205. return obj.To<T>();
  206. }
  207. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  208. }
  209. public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
  210. {
  211. var obj = GetAndCheckEdit(id, update);
  212. if (!obj)
  213. {
  214. return obj.To<long>();
  215. }
  216. return DataAccess.EditSingleByIdId(id, obj.Data);
  217. }
  218. public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
  219. {
  220. var obj = GetAndCheckEdit(id, update);
  221. if (!obj)
  222. {
  223. return obj.To<TDboRead>();
  224. }
  225. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  226. }
  227. public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  228. {
  229. var obj = GetAndCheckEdit(id, update);
  230. if (!obj)
  231. {
  232. return obj.To<T>();
  233. }
  234. return DataAccess.EditSingleById(id, obj.Data, returnFunc);
  235. }
  236. public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
  237. {
  238. var obj = GetAndCheckEdit(id, update);
  239. if (!obj)
  240. {
  241. return obj.To<string>();
  242. }
  243. return DataAccess.EditSingleByIdGuid(id, obj.Data);
  244. }
  245. public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
  246. {
  247. var obj = GetAndCheckEdit(id, update);
  248. if (!obj)
  249. {
  250. return obj.To<TDboRead>();
  251. }
  252. return DataAccess.EditSingleByIdDbo(id, obj.Data);
  253. }
  254. public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
  255. {
  256. var res = GetAndCheckDelete(id);
  257. if (!res)
  258. {
  259. return res.To<T>();
  260. }
  261. return DataAccess.DeleteSingleById(id, returnFunc);
  262. }
  263. public LuResult<string> DeleteSingleByIdGuid(string id)
  264. {
  265. var res = GetAndCheckDelete(id);
  266. if (!res)
  267. {
  268. return res.To<string>();
  269. }
  270. return DataAccess.DeleteSingleByIdGuid(id);
  271. }
  272. public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
  273. {
  274. var res = GetAndCheckDelete(id);
  275. if (!res)
  276. {
  277. return res.To<TDboRead>();
  278. }
  279. return DataAccess.DeleteSingleByIdDbo(id);
  280. }
  281. public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
  282. {
  283. var res = GetAndCheckDelete(id);
  284. if (!res)
  285. {
  286. return res.To<T>();
  287. }
  288. return DataAccess.DeleteSingleById(id, returnFunc);
  289. }
  290. public LuResult<long> DeleteSingleByIdId(long id)
  291. {
  292. var res = GetAndCheckDelete(id);
  293. if (!res)
  294. {
  295. return res.To<long>();
  296. }
  297. return DataAccess.DeleteSingleByIdId(id);
  298. }
  299. public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
  300. {
  301. var res = GetAndCheckDelete(id);
  302. if (!res)
  303. {
  304. return res.To<TDboRead>();
  305. }
  306. return DataAccess.DeleteSingleByIdDbo(id);
  307. }
  308. }
  309. }