Browse Source

added dbo/model conversion tools; added add/edit hooks; tests

tags/v0.3.0
Robin Thoni 7 years ago
parent
commit
74599ffb52

+ 16
- 20
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs View File

@@ -114,9 +114,9 @@ namespace Luticate2.Utils.DataAccess
114 114
 
115 115
 
116 116
 
117
-        protected virtual LuResult<TModel> _Add(TDboCreate dbo, TDbContext db, DbSet<TModel> table)
117
+        protected virtual LuResult<bool> _Add(TModel model, TDboCreate dbo, TDbContext db, DbSet<TModel> table)
118 118
         {
119
-            return LuResult<TModel>.Ok(GetModelFromTCreate(dbo));
119
+            return LuResult<bool>.Ok(true);
120 120
         }
121 121
 
122 122
         public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
@@ -127,20 +127,17 @@ namespace Luticate2.Utils.DataAccess
127 127
                 var transact = BeginTransaction(db);
128 128
                 foreach (var dbo in objs)
129 129
                 {
130
-                    var res = _Add(dbo, db, table);
130
+                    var model = GetModelFromTCreate(dbo);
131
+                    table.Add(model);
132
+                    models.Add(model);
133
+                    var res = _Add(model, dbo, db, table);
131 134
                     if (!res)
132 135
                     {
133 136
                         RollbackTransaction(transact);
134 137
                         return res.To<T>();
135 138
                     }
136
-                    table.Add(res.Data);
137
-                    models.Add(res.Data);
138 139
                 }
139 140
                 db.SaveChanges();
140
-                foreach (var model in models)
141
-                {
142
-                    db.Entry(model).State = EntityState.Detached;
143
-                }
144 141
                 CommitTransaction(transact);
145 142
                 return LuResult<T>.Ok(default(T));
146 143
             });
@@ -186,7 +183,7 @@ namespace Luticate2.Utils.DataAccess
186 183
         {
187 184
             return Execute((db, table) =>
188 185
             {
189
-                var model = GetGetQueryable(db, table).AsNoTracking().FirstOrDefault(predicate);
186
+                var model = GetGetQueryable(db, table).FirstOrDefault(predicate);
190 187
                 if (model == default(TModel))
191 188
                 {
192 189
                     return GetNotFoundResult<TDboRead>();
@@ -240,7 +237,7 @@ namespace Luticate2.Utils.DataAccess
240 237
                 {
241 238
                     ordered = func(ordered);
242 239
                 }
243
-                var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
240
+                var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
244 241
                 var result = new LuPaginatedDbo<TDboRead>
245 242
                 {
246 243
                     Count = count,
@@ -285,7 +282,7 @@ namespace Luticate2.Utils.DataAccess
285 282
                     if (exp == null)
286 283
                     {
287 284
                         return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
288
-                            string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
285
+                            $"LuEfCrudDataAccess: {field.Name}", "Invalid order by field");
289 286
                     }
290 287
                     if (ordered != null)
291 288
                     {
@@ -297,7 +294,7 @@ namespace Luticate2.Utils.DataAccess
297 294
                     }
298 295
                 }
299 296
 
300
-                var data = ordered.Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
297
+                var data = ordered.Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
301 298
                 var result = new LuPaginatedDbo<TDboRead>
302 299
                 {
303 300
                     Count = count,
@@ -327,11 +324,10 @@ namespace Luticate2.Utils.DataAccess
327 324
 //            IList<TModel> models = null;
328 325
 //            var editRes = Execute((db, table) =>
329 326
 //            {
330
-//                models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
327
+//                models = GetEditQueryable(db, table).Where(predicate).ToList();
331 328
 //                foreach (var model in models)
332 329
 //                {
333 330
 //                    update(model);
334
-//                    db.Entry(model).State = EntityState.Modified;
335 331
 //                }
336 332
 //                db.SaveChanges();
337 333
 //                return LuResult<T>.Ok(default(T));
@@ -376,9 +372,8 @@ namespace Luticate2.Utils.DataAccess
376 372
 //            return EditSingleById(id, update, read => read);
377 373
 //        }
378 374
 
379
-        protected virtual LuResult<bool> _EditSingleById(TId id, TModel model, TDboUpdate update, TDbContext db, DbSet<TModel> table)
375
+        protected virtual LuResult<bool> _EditSingleById(TModel model, TDboUpdate update, TDbContext db, DbSet<TModel> table)
380 376
         {
381
-            EditModelFromTUpdate(update, model);
382 377
             return LuResult<bool>.Ok(true);
383 378
         }
384 379
 
@@ -393,13 +388,14 @@ namespace Luticate2.Utils.DataAccess
393 388
             var editRes = Execute((db, table) =>
394 389
             {
395 390
                 var model = GetEditQueryable(db, table).FirstOrDefault(GetExpression(new KeyValuePair<string, object>("id", guid)));
396
-                var res = _EditSingleById(id, model, update, db, table);
391
+                EditModelFromTUpdate(update, model);
392
+                db.SaveChanges();
393
+                var res = _EditSingleById(model, update, db, table);
397 394
                 if (!res)
398 395
                 {
399 396
                     RollbackTransaction(transact);
400 397
                     return res.To<T>();
401 398
                 }
402
-                db.Entry(model).State = EntityState.Modified;
403 399
                 db.SaveChanges();
404 400
                 return LuResult<T>.Ok(default(T));
405 401
             });
@@ -432,7 +428,7 @@ namespace Luticate2.Utils.DataAccess
432 428
             return Execute((db, table) =>
433 429
             {
434 430
                 var transact = BeginTransaction(db);
435
-                var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
431
+                var models = GetDeleteQueryable(db, table).Where(predicate).ToList();
436 432
                 var getRes = GetMultiple(models, returnFunc);
437 433
                 if (!getRes)
438 434
                 {

+ 11
- 1
Luticate2.Utils/Utils/LuCoreUtilsExtensions.cs View File

@@ -20,16 +20,26 @@ namespace Luticate2.Utils.Utils
20 20
             return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
21 21
         }
22 22
 
23
+        public static T ToOptEnum<T>(this string model)
24
+        {
25
+            return model == null ? default(T) : (T) Enum.Parse(typeof(T), model);
26
+        }
27
+
23 28
         public static T ToEnum<T>(this string model)
24 29
         {
25 30
             return (T) Enum.Parse(typeof(T), model);
26 31
         }
27 32
 
28
-        public static Guid? ToGuid(this string str)
33
+        public static Guid? ToOptGuid(this string str)
29 34
         {
30 35
             return str == null ? (Guid?)null : new Guid(str);
31 36
         }
32 37
 
38
+        public static Guid ToGuid(this string str)
39
+        {
40
+            return new Guid(str);
41
+        }
42
+
33 43
         public static string ToDbo(this Guid model)
34 44
         {
35 45
             return model.ToString();

+ 8
- 3
TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs View File

@@ -66,7 +66,7 @@ namespace TestUtils.DataAccess
66 66
             return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
67 67
         }
68 68
 
69
-        protected override LuResult<pk_guid> _Add(PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)
69
+        protected override LuResult<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)
70 70
         {
71 71
             if (dbo.SomeInt == 2424)
72 72
             {
@@ -74,9 +74,14 @@ namespace TestUtils.DataAccess
74 74
             }
75 75
             if (dbo.SomeInt == 4242)
76 76
             {
77
-                return LuResult<pk_guid>.Error(LuStatus.DbError, "Some expected error", "");
77
+                return LuResult<bool>.Error(LuStatus.DbError, "Some expected error", "");
78 78
             }
79
-            return LuResult<pk_guid>.Ok(GetModelFromTCreate(dbo));
79
+            return LuResult<bool>.Ok(true);
80
+        }
81
+
82
+        protected override LuResult<bool> _EditSingleById(pk_guid model, PkGuidAddDbo update, LuUtilsDbContext db, DbSet<pk_guid> table)
83
+        {
84
+            return LuResult<bool>.Ok(true);
80 85
         }
81 86
     }
82 87
 }

+ 2
- 2
TestUtils/Utils/LuCoreUtilsExtensionsTest.cs View File

@@ -7,10 +7,10 @@ namespace TestUtils.Utils
7 7
     public class LuCoreUtilsExtensionsTest
8 8
     {
9 9
         [Fact]
10
-        public void TestStringToGuidNull()
10
+        public void TestStringToOptGuidNull()
11 11
         {
12 12
             string str = null;
13
-            Assert.Null(str.ToGuid());
13
+            Assert.Null(str.ToOptGuid());
14 14
         }
15 15
 
16 16
         [Fact]

Loading…
Cancel
Save