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.

LuEfCrudDataAccess.cs 14KB

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