Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LuEfCrudDataAccess.cs 13KB

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