|
@@ -114,18 +114,34 @@ namespace Luticate2.Utils.DataAccess
|
114
|
114
|
|
115
|
115
|
|
116
|
116
|
|
|
117
|
+ protected virtual LuResult<T> _Add<T>(TDboCreate obj, TDbContext db, DbSet<TModel> table)
|
|
118
|
+ {
|
|
119
|
+ return LuResult<T>.Ok(default(T));
|
|
120
|
+ }
|
117
|
121
|
|
118
|
122
|
public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
|
119
|
123
|
{
|
120
|
|
- var models = objs.Select(GetModelFromTCreate).ToList();
|
|
124
|
+ var models = new List<TModel>();
|
121
|
125
|
var addRes = Execute((db, table) =>
|
122
|
126
|
{
|
123
|
|
- table.AddRange(models);
|
|
127
|
+ var transact = BeginTransaction(db);
|
|
128
|
+ foreach (var dbo in objs)
|
|
129
|
+ {
|
|
130
|
+ var model = GetModelFromTCreate(dbo);
|
|
131
|
+ table.Add(model);
|
|
132
|
+ models.Add(model);
|
|
133
|
+ var res = _Add<T>(dbo, db, table);
|
|
134
|
+ if (!res)
|
|
135
|
+ {
|
|
136
|
+ return res;
|
|
137
|
+ }
|
|
138
|
+ }
|
124
|
139
|
db.SaveChanges();
|
125
|
140
|
foreach (var model in models)
|
126
|
141
|
{
|
127
|
142
|
db.Entry(model).State = EntityState.Detached;
|
128
|
143
|
}
|
|
144
|
+ CommitTransaction(transact);
|
129
|
145
|
return LuResult<T>.Ok(default(T));
|
130
|
146
|
});
|
131
|
147
|
if (!addRes)
|
|
@@ -304,71 +320,97 @@ namespace Luticate2.Utils.DataAccess
|
304
|
320
|
|
305
|
321
|
|
306
|
322
|
|
307
|
|
- public virtual LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
|
308
|
|
- Func<IEnumerable<TDboRead>, T> returnFunc)
|
|
323
|
+
|
|
324
|
+// public virtual LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
|
|
325
|
+// Func<IEnumerable<TDboRead>, T> returnFunc)
|
|
326
|
+// {
|
|
327
|
+// IList<TModel> models = null;
|
|
328
|
+// var editRes = Execute((db, table) =>
|
|
329
|
+// {
|
|
330
|
+// models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
|
|
331
|
+// foreach (var model in models)
|
|
332
|
+// {
|
|
333
|
+// update(model);
|
|
334
|
+// db.Entry(model).State = EntityState.Modified;
|
|
335
|
+// }
|
|
336
|
+// db.SaveChanges();
|
|
337
|
+// return LuResult<T>.Ok(default(T));
|
|
338
|
+// });
|
|
339
|
+// if (!editRes)
|
|
340
|
+// {
|
|
341
|
+// return editRes;
|
|
342
|
+// }
|
|
343
|
+// return GetMultiple(models, returnFunc);
|
|
344
|
+// }
|
|
345
|
+//
|
|
346
|
+// public virtual LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
|
|
347
|
+// {
|
|
348
|
+// var func = GetIdFunc<TId>();
|
|
349
|
+// return Edit(predicate, update, reads => reads.Select(func));
|
|
350
|
+// }
|
|
351
|
+//
|
|
352
|
+// public virtual LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
|
|
353
|
+// {
|
|
354
|
+// return Edit(predicate, update, read => read);
|
|
355
|
+// }
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+// public virtual LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
|
|
359
|
+// {
|
|
360
|
+// var guid = GetId(id);
|
|
361
|
+// if (guid == null)
|
|
362
|
+// {
|
|
363
|
+// return GetNotFoundResult<T>();
|
|
364
|
+// }
|
|
365
|
+// return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
|
|
366
|
+// list => returnFunc(list.FirstOrDefault()));
|
|
367
|
+// }
|
|
368
|
+//
|
|
369
|
+// public virtual LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
|
|
370
|
+// {
|
|
371
|
+// return EditSingleById(id, update, GetIdFunc<TId>());
|
|
372
|
+// }
|
|
373
|
+//
|
|
374
|
+// public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
|
|
375
|
+// {
|
|
376
|
+// return EditSingleById(id, update, read => read);
|
|
377
|
+// }
|
|
378
|
+
|
|
379
|
+ protected virtual LuResult<T> _EditSingleById<T>(TId id, TModel model, TDboUpdate obj, TDbContext db, DbSet<TModel> table)
|
|
380
|
+ {
|
|
381
|
+ return LuResult<T>.Ok(default(T));
|
|
382
|
+ }
|
|
383
|
+
|
|
384
|
+ public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
|
309
|
385
|
{
|
310
|
|
- IList<TModel> models = null;
|
|
386
|
+ var guid = GetId(id);
|
|
387
|
+ if (guid == null)
|
|
388
|
+ {
|
|
389
|
+ return GetNotFoundResult<T>();
|
|
390
|
+ }
|
|
391
|
+ var transact = BeginTransaction(null);
|
311
|
392
|
var editRes = Execute((db, table) =>
|
312
|
393
|
{
|
313
|
|
- models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
|
314
|
|
- foreach (var model in models)
|
|
394
|
+ var model = GetEditQueryable(db, table).FirstOrDefault(GetExpression(new KeyValuePair<string, object>("id", guid)));
|
|
395
|
+ EditModelFromTUpdate(update, model);
|
|
396
|
+ var res = _EditSingleById<T>(id, model, update, db, table);
|
|
397
|
+ if (!res)
|
315
|
398
|
{
|
316
|
|
- update(model);
|
317
|
|
- db.Entry(model).State = EntityState.Modified;
|
|
399
|
+ RollbackTransaction(transact);
|
|
400
|
+ return res;
|
318
|
401
|
}
|
|
402
|
+ db.Entry(model).State = EntityState.Modified;
|
319
|
403
|
db.SaveChanges();
|
320
|
404
|
return LuResult<T>.Ok(default(T));
|
321
|
405
|
});
|
322
|
406
|
if (!editRes)
|
323
|
407
|
{
|
|
408
|
+ RollbackTransaction(transact);
|
324
|
409
|
return editRes;
|
325
|
410
|
}
|
326
|
|
- return GetMultiple(models, returnFunc);
|
327
|
|
- }
|
328
|
|
-
|
329
|
|
- public virtual LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
|
330
|
|
- {
|
331
|
|
- var func = GetIdFunc<TId>();
|
332
|
|
- return Edit(predicate, update, reads => reads.Select(func));
|
333
|
|
- }
|
334
|
|
-
|
335
|
|
- public virtual LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
|
336
|
|
- {
|
337
|
|
- return Edit(predicate, update, read => read);
|
338
|
|
- }
|
339
|
|
-
|
340
|
|
-
|
341
|
|
- public virtual LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
|
342
|
|
- {
|
343
|
|
- var guid = GetId(id);
|
344
|
|
- if (guid == null)
|
345
|
|
- {
|
346
|
|
- return GetNotFoundResult<T>();
|
347
|
|
- }
|
348
|
|
- return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
|
349
|
|
- list => returnFunc(list.FirstOrDefault()));
|
350
|
|
- }
|
351
|
|
-
|
352
|
|
- public virtual LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
|
353
|
|
- {
|
354
|
|
- return EditSingleById(id, update, GetIdFunc<TId>());
|
355
|
|
- }
|
356
|
|
-
|
357
|
|
- public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
|
358
|
|
- {
|
359
|
|
- return EditSingleById(id, update, read => read);
|
360
|
|
- }
|
361
|
|
-
|
362
|
|
-
|
363
|
|
- public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
|
364
|
|
- {
|
365
|
|
- var guid = GetId(id);
|
366
|
|
- if (guid == null)
|
367
|
|
- {
|
368
|
|
- return GetNotFoundResult<T>();
|
369
|
|
- }
|
370
|
|
- return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)),
|
371
|
|
- model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
|
|
411
|
+ var getRes = GetSingleById(id).To(returnFunc);
|
|
412
|
+ CommitTransaction(transact);
|
|
413
|
+ return getRes;
|
372
|
414
|
}
|
373
|
415
|
|
374
|
416
|
public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
|
|
@@ -389,14 +431,17 @@ namespace Luticate2.Utils.DataAccess
|
389
|
431
|
{
|
390
|
432
|
return Execute((db, table) =>
|
391
|
433
|
{
|
|
434
|
+ var transact = BeginTransaction(db);
|
392
|
435
|
var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
|
393
|
436
|
var getRes = GetMultiple(models, returnFunc);
|
394
|
437
|
if (!getRes)
|
395
|
438
|
{
|
|
439
|
+ RollbackTransaction(transact);
|
396
|
440
|
return getRes;
|
397
|
441
|
}
|
398
|
442
|
table.RemoveRange(models);
|
399
|
443
|
db.SaveChanges();
|
|
444
|
+ CommitTransaction(transact);
|
400
|
445
|
return getRes;
|
401
|
446
|
});
|
402
|
447
|
}
|