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.

LuEfCrudDataAccess.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using Luticate2.Utils.Dbo;
  7. using Luticate2.Utils.Dbo.Basic;
  8. using Luticate2.Utils.Dbo.OrderBy;
  9. using Luticate2.Utils.Dbo.Result;
  10. using Luticate2.Utils.Interfaces;
  11. using Luticate2.Utils.Utils;
  12. using Microsoft.EntityFrameworkCore;
  13. namespace Luticate2.Utils.DataAccess
  14. {
  15. public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
  16. LuEfDataAccess<TModel, TDbContext>, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  17. where TModel : class
  18. where TDboCreate : class
  19. where TDboRead : class
  20. where TDboUpdate : class
  21. where TDbContext : DbContext
  22. {
  23. protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
  24. {
  25. }
  26. protected abstract TModel GetModelFromTCreate(TDboCreate obj);
  27. protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
  28. protected abstract TDboRead GetDboFromModel(TModel model);
  29. protected virtual Expression<Func<TModel, object>> GetOrderByFieldExpression(string fieldName)
  30. {
  31. fieldName = fieldName.ToSnakeCase();
  32. if (!typeof(TModel).HasProperty(fieldName))
  33. {
  34. return null;
  35. }
  36. var param = Expression.Parameter(typeof(TModel), "x");
  37. var prop = Expression.Property(param, fieldName);
  38. var converted = Expression.Convert(prop, typeof(object));
  39. var exp = Expression.Lambda<Func<TModel, object>>(converted, param);
  40. return exp;
  41. }
  42. protected Func<TDboRead, T> GetIdFunc<T>()
  43. {
  44. var param = Expression.Parameter(typeof(TDboRead), "x");
  45. var exp = Expression.Property(param, "Id");
  46. var lambda = Expression.Lambda<Func<TDboRead, T>>(exp, param);
  47. var func = lambda.Compile();
  48. return func;
  49. }
  50. protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
  51. {
  52. EditModelFromTUpdate(obj, model);
  53. return model;
  54. }
  55. protected Guid? GetGuid(string id)
  56. {
  57. Guid guid;
  58. if (Guid.TryParse(id, out guid))
  59. {
  60. return guid;
  61. }
  62. return null;
  63. }
  64. protected LuResult<T> GetNotFoundResult<T>()
  65. {
  66. return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
  67. }
  68. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  69. {
  70. return Execute(() =>
  71. {
  72. var models = objs.Select(GetModelFromTCreate).ToList();
  73. Table.AddRange(models);
  74. Db.SaveChanges();
  75. var dbos = models.Select(GetDboFromModel).ToList();
  76. var res = returnFunc(dbos);
  77. return LuResult<T>.Ok(res);
  78. });
  79. }
  80. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  81. {
  82. return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
  83. }
  84. public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
  85. {
  86. var func = GetIdFunc<string>();
  87. return Add(objs, list => list.Select(func));
  88. }
  89. public LuResult<string> AddGuid(TDboCreate obj)
  90. {
  91. return AddGuid(new List<TDboCreate> {obj}).To(list => list.First());
  92. }
  93. public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
  94. {
  95. var func = GetIdFunc<long>();
  96. return Add(obj, list => list.Select(func));
  97. }
  98. public LuResult<long> AddId(TDboCreate obj)
  99. {
  100. return AddId(new List<TDboCreate> {obj}).To(list => list.First());
  101. }
  102. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  103. {
  104. return Add(obj, read => read);
  105. }
  106. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  107. {
  108. return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
  109. }
  110. public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
  111. {
  112. return Execute(() =>
  113. {
  114. var model = Table.FirstOrDefault(predicate);
  115. if (model == default(TModel))
  116. {
  117. return GetNotFoundResult<TDboRead>();
  118. }
  119. var dbo = GetDboFromModel(model);
  120. return LuResult<TDboRead>.Ok(dbo);
  121. });
  122. }
  123. public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  124. {
  125. return GetSingle(GetExpression(keys));
  126. }
  127. public LuResult<TDboRead> GetSingleById(string id)
  128. {
  129. var guid = GetGuid(id);
  130. if (guid == null)
  131. {
  132. return GetNotFoundResult<TDboRead>();
  133. }
  134. return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
  135. }
  136. public LuResult<TDboRead> GetSingleById(long id)
  137. {
  138. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  139. }
  140. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IOrderedQueryable<TModel>> orderBy,
  141. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  142. params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  143. {
  144. return Execute(() =>
  145. {
  146. var count = Table.Count(predicate);
  147. var ordered = orderBy(Table);
  148. foreach (var func in otherOrderBy)
  149. {
  150. ordered = func(ordered);
  151. }
  152. var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
  153. var result = new LuPaginatedDbo<TDboRead>
  154. {
  155. Count = count,
  156. Data = data
  157. };
  158. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  159. });
  160. }
  161. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  162. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  163. params Expression<Func<TModel, object>>[] otherOrderBy)
  164. {
  165. return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
  166. otherOrderBy.Select<Expression<Func<TModel, object>>, Func<IOrderedQueryable<TModel>,
  167. IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
  168. }
  169. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IOrderedQueryable<TModel>> orderBy,
  170. int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  171. {
  172. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  173. }
  174. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  175. int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
  176. {
  177. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  178. }
  179. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
  180. {
  181. return Execute(() =>
  182. {
  183. var count = Table.Count();
  184. IOrderedQueryable<TModel> ordered = null;
  185. foreach (var field in orderBy.Fields)
  186. {
  187. var exp = GetOrderByFieldExpression(field.Name);
  188. if (exp == null)
  189. {
  190. return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
  191. string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
  192. }
  193. if (ordered != null)
  194. {
  195. ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
  196. }
  197. else
  198. {
  199. ordered = field.Asc ? Table.OrderBy(exp) : Table.OrderByDescending(exp);
  200. }
  201. }
  202. var data = ordered.Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
  203. var result = new LuPaginatedDbo<TDboRead>
  204. {
  205. Count = count,
  206. Data = data
  207. };
  208. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  209. });
  210. }
  211. public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
  212. Func<IEnumerable<TDboRead>, T> returnFunc)
  213. {
  214. return Execute(() =>
  215. {
  216. var models = Table.Where(predicate);
  217. var editedDbos = new List<TDboRead>();
  218. foreach (var model in models)
  219. {
  220. update(model);
  221. editedDbos.Add(GetDboFromModel(model));
  222. Db.Entry(model).State = EntityState.Modified;
  223. }
  224. Db.SaveChanges();
  225. var res = returnFunc(editedDbos);
  226. return LuResult<T>.Ok(res);
  227. });
  228. }
  229. public LuResult<IEnumerable<string>> EditGuid(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  230. {
  231. var func = GetIdFunc<string>();
  232. return Edit(predicate, update, reads => reads.Select(func));
  233. }
  234. public LuResult<IEnumerable<long>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  235. {
  236. var func = GetIdFunc<long>();
  237. return Edit(predicate, update, reads => reads.Select(func));
  238. }
  239. public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  240. {
  241. return Edit(predicate, update, read => read);
  242. }
  243. public LuResult<T> EditSingleById<T>(long id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  244. {
  245. return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
  246. list => returnFunc(list.FirstOrDefault()));
  247. }
  248. public LuResult<long> EditSingleByIdId(long id, Action<TModel> update)
  249. {
  250. var func = GetIdFunc<long>();
  251. return EditSingleById(id, update, func);
  252. }
  253. public LuResult<TDboRead> EditSingleByIdDbo(long id, Action<TModel> update)
  254. {
  255. return EditSingleById(id, update, read => read);
  256. }
  257. public LuResult<T> EditSingleById<T>(string id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  258. {
  259. var guid = GetGuid(id);
  260. if (guid == null)
  261. {
  262. return GetNotFoundResult<T>();
  263. }
  264. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
  265. list => returnFunc(list.FirstOrDefault()));
  266. }
  267. public LuResult<string> EditSingleByIdGuid(string id, Action<TModel> update)
  268. {
  269. return EditSingleById(id, update, GetIdFunc<string>());
  270. }
  271. public LuResult<TDboRead> EditSingleByIdDbo(string id, Action<TModel> update)
  272. {
  273. return EditSingleById(id, update, read => read);
  274. }
  275. public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  276. {
  277. return Edit(GetExpression(new KeyValuePair<string, object>("id", id)),
  278. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  279. }
  280. public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
  281. {
  282. return EditSingleById(id, update, GetIdFunc<long>());
  283. }
  284. public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
  285. {
  286. return EditSingleById(id, update, read => read);
  287. }
  288. public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  289. {
  290. var guid = GetGuid(id);
  291. if (guid == null)
  292. {
  293. return GetNotFoundResult<T>();
  294. }
  295. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)),
  296. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  297. }
  298. public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
  299. {
  300. return EditSingleById(id, update, GetIdFunc<string>());
  301. }
  302. public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
  303. {
  304. return EditSingleById(id, update, read => read);
  305. }
  306. public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
  307. Func<IEnumerable<TDboRead>, T> returnFunc)
  308. {
  309. return Execute(() =>
  310. {
  311. var models = Table.Where(predicate).ToList();
  312. Table.RemoveRange(models);
  313. Db.SaveChanges();
  314. var dbos = models.Select(GetDboFromModel);
  315. return LuResult<T>.Ok(returnFunc(dbos));
  316. });
  317. }
  318. public LuResult<IEnumerable<string>> DeleteGuid(Expression<Func<TModel, bool>> predicate)
  319. {
  320. var func = GetIdFunc<string>();
  321. return Delete(predicate, reads => reads.Select(func));
  322. }
  323. public LuResult<IEnumerable<long>> DeleteId(Expression<Func<TModel, bool>> predicate)
  324. {
  325. var func = GetIdFunc<long>();
  326. return Delete(predicate, reads => reads.Select(func));
  327. }
  328. public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
  329. {
  330. return Delete(predicate, read => read);
  331. }
  332. public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
  333. {
  334. var guid = GetGuid(id);
  335. if (guid == null)
  336. {
  337. return GetNotFoundResult<T>();
  338. }
  339. return Delete(GetExpression(new KeyValuePair<string, object>("id", guid)),
  340. reads => returnFunc(reads.FirstOrDefault()));
  341. }
  342. public LuResult<string> DeleteSingleByIdGuid(string id)
  343. {
  344. var func = GetIdFunc<string>();
  345. return DeleteSingleById(id, func);
  346. }
  347. public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
  348. {
  349. return DeleteSingleById(id, read => read);
  350. }
  351. public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
  352. {
  353. return Delete(GetExpression(new KeyValuePair<string, object>("id", id)),
  354. reads => returnFunc(reads.First()));
  355. }
  356. public LuResult<long> DeleteSingleByIdId(long id)
  357. {
  358. var func = GetIdFunc<long>();
  359. return DeleteSingleById(id, func);
  360. }
  361. public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
  362. {
  363. return DeleteSingleById(id, read => read);
  364. }
  365. }
  366. }