Quellcode durchsuchen

perform full get after add/edit/delete

tags/v0.2.3
Robin Thoni vor 8 Jahren
Ursprung
Commit
0665d6d47d

+ 48
- 12
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs Datei anzeigen

@@ -66,6 +66,16 @@ namespace Luticate2.Utils.DataAccess
66 66
             return id;
67 67
         }
68 68
 
69
+        protected virtual object GetModelId(object id)
70
+        {
71
+            var g = id as Guid?;
72
+            if (g != null)
73
+            {
74
+                return g.ToString();
75
+            }
76
+            return id;
77
+        }
78
+
69 79
         protected virtual IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
70 80
         {
71 81
             return table;
@@ -107,19 +117,22 @@ namespace Luticate2.Utils.DataAccess
107 117
 
108 118
         public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
109 119
         {
110
-            return Execute((db, table) =>
120
+            var models = objs.Select(GetModelFromTCreate).ToList();
121
+            var addRes = Execute((db, table) =>
111 122
             {
112
-                var models = objs.Select(GetModelFromTCreate).ToList();
113 123
                 table.AddRange(models);
114 124
                 db.SaveChanges();
115 125
                 foreach (var model in models)
116 126
                 {
117 127
                     db.Entry(model).State = EntityState.Detached;
118 128
                 }
119
-                var dbos = models.Select(GetDboFromModel).ToList();
120
-                var res = returnFunc(dbos);
121
-                return LuResult<T>.Ok(res);
129
+                return LuResult<T>.Ok(default(T));
122 130
             });
131
+            if (!addRes)
132
+            {
133
+                return addRes;
134
+            }
135
+            return GetMultiple(models, returnFunc);
123 136
         }
124 137
 
125 138
         public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
@@ -182,6 +195,21 @@ namespace Luticate2.Utils.DataAccess
182 195
             return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
183 196
         }
184 197
 
198
+        protected LuResult<T> GetMultiple<T>(IList<TModel> models, Func<IEnumerable<TDboRead>, T> returnFunc)
199
+        {
200
+            var dbos = new List<TDboRead>();
201
+            foreach (var model in models)
202
+            {
203
+                var getRes = GetSingleById((TId) GetModelId(((dynamic)model).id));
204
+                if (!getRes)
205
+                {
206
+                    return getRes.To<T>();
207
+                }
208
+                dbos.Add(getRes.Data);
209
+            }
210
+            var res = returnFunc(dbos);
211
+            return LuResult<T>.Ok(res);
212
+        }
185 213
 
186 214
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
187 215
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
@@ -279,19 +307,23 @@ namespace Luticate2.Utils.DataAccess
279 307
         public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
280 308
             Func<IEnumerable<TDboRead>, T> returnFunc)
281 309
         {
282
-            return Execute((db, table) =>
310
+            IList<TModel> models = null;
311
+            var editRes = Execute((db, table) =>
283 312
             {
284
-                var models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
313
+                models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
285 314
                 foreach (var model in models)
286 315
                 {
287 316
                     update(model);
288 317
                     db.Entry(model).State = EntityState.Modified;
289 318
                 }
290 319
                 db.SaveChanges();
291
-                var editedDbos = models.Select(GetDboFromModel).ToList();
292
-                var res = returnFunc(editedDbos);
293
-                return LuResult<T>.Ok(res);
320
+                return LuResult<T>.Ok(default(T));
294 321
             });
322
+            if (!editRes)
323
+            {
324
+                return editRes;
325
+            }
326
+            return GetMultiple(models, returnFunc);
295 327
         }
296 328
 
297 329
         public LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
@@ -358,10 +390,14 @@ namespace Luticate2.Utils.DataAccess
358 390
             return Execute((db, table) =>
359 391
             {
360 392
                 var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
393
+                var getRes = GetMultiple(models, returnFunc);
394
+                if (!getRes)
395
+                {
396
+                    return getRes;
397
+                }
361 398
                 table.RemoveRange(models);
362 399
                 db.SaveChanges();
363
-                var dbos = models.Select(GetDboFromModel);
364
-                return LuResult<T>.Ok(returnFunc(dbos));
400
+                return getRes;
365 401
             });
366 402
         }
367 403
 

+ 17
- 0
TestUtils/DataAccess/LuUtilsDbContext.cs Datei anzeigen

@@ -30,6 +30,21 @@ namespace TestUtils.DataAccess
30 30
         protected override void OnModelCreating(ModelBuilder modelBuilder)
31 31
         {
32 32
             
33
+            modelBuilder.Entity<fk_pk_guids>()
34
+                .HasKey(c => new { c.id });
35
+            
36
+            modelBuilder.Entity<fk_pk_guids>()
37
+                .Property(e => e.id)
38
+                .HasDefaultValueSql("get_uuid()");
39
+            
40
+            
41
+            modelBuilder.Entity<fk_pk_guids>()
42
+                .HasOne(e => e.fk_pk_guid)
43
+                .WithMany(e => e.fk_pk_guids_fk)
44
+                .HasForeignKey("pk_guid_id")
45
+                .HasConstraintName("fk_pk_guid_pk_guid_id_fkey");
46
+            
47
+            
33 48
             modelBuilder.Entity<pk_bigserial>()
34 49
                 .HasKey(c => new { c.id });
35 50
             
@@ -58,6 +73,8 @@ namespace TestUtils.DataAccess
58 73
             
59 74
         }
60 75
         
76
+        public virtual DbSet<fk_pk_guids> fk_pk_guids { get; set; }
77
+        
61 78
         public virtual DbSet<pk_bigserial> pk_bigserial { get; set; }
62 79
         
63 80
         public virtual DbSet<pk_guid> pk_guid { get; set; }

+ 48
- 0
TestUtils/DataAccess/LuUtilsFkPkGuidDataAccess.cs Datei anzeigen

@@ -0,0 +1,48 @@
1
+using System;
2
+using System.Linq;
3
+using Luticate2.Utils.DataAccess;
4
+using Microsoft.EntityFrameworkCore;
5
+using TestUtils.DataAccess.Models;
6
+using TestUtils.Dbo.FkPkGuid;
7
+
8
+namespace TestUtils.DataAccess
9
+{
10
+    public class LuUtilsFkPkGuidDataAccess : LuEfCrudDataAccess<fk_pk_guids, FkPkGuidAddDbo, FkPkGuidDbo, FkPkGuidAddDbo, LuUtilsDbContext, string>
11
+    {
12
+        public LuUtilsFkPkGuidDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
13
+        {
14
+        }
15
+
16
+        protected override DbSet<fk_pk_guids> GetTable(LuUtilsDbContext db)
17
+        {
18
+            return db.fk_pk_guids;
19
+        }
20
+
21
+        protected override IQueryable<fk_pk_guids> GetGetQueryable(LuUtilsDbContext db, DbSet<fk_pk_guids> table)
22
+        {
23
+            return table.Include(guids => guids.fk_pk_guid);
24
+        }
25
+
26
+        protected override fk_pk_guids GetModelFromTCreate(FkPkGuidAddDbo obj)
27
+        {
28
+            return GetModelFromTUpdate(obj, new fk_pk_guids());
29
+        }
30
+
31
+        protected override void EditModelFromTUpdate(FkPkGuidAddDbo obj, fk_pk_guids model)
32
+        {
33
+            model.name = obj.Name;
34
+            model.pk_guid_id = obj.PkGuidId == null ? (Guid?) null : new Guid(obj.PkGuidId);
35
+        }
36
+
37
+        protected override FkPkGuidDbo GetDboFromModel(fk_pk_guids model)
38
+        {
39
+            return new FkPkGuidDbo
40
+            {
41
+                Id = model.id.ToString(),
42
+                Name = model.name,
43
+                PkGuid = model.fk_pk_guid == null ? null : LuUtilsPkGuidDataAccess.GetDboFromModelStatic(model.fk_pk_guid),
44
+                PkGuidId = model.pk_guid_id?.ToString()
45
+            };
46
+        }
47
+    }
48
+}

+ 6
- 1
TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs Datei anzeigen

@@ -32,7 +32,7 @@ namespace TestUtils.DataAccess
32 32
             model.some_text = obj.SomeText;
33 33
         }
34 34
 
35
-        protected override PkGuidDbo GetDboFromModel(pk_guid model)
35
+        public static PkGuidDbo GetDboFromModelStatic(pk_guid model)
36 36
         {
37 37
             return new PkGuidDbo
38 38
             {
@@ -44,6 +44,11 @@ namespace TestUtils.DataAccess
44 44
             };
45 45
         }
46 46
 
47
+        protected override PkGuidDbo GetDboFromModel(pk_guid model)
48
+        {
49
+            return GetDboFromModelStatic(model);
50
+        }
51
+
47 52
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
48 53
         {
49 54
             return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());

+ 22
- 0
TestUtils/DataAccess/Models/fk_pk_guids.cs Datei anzeigen

@@ -0,0 +1,22 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.ComponentModel.DataAnnotations.Schema;
5
+
6
+namespace TestUtils.DataAccess.Models
7
+{
8
+    public partial class fk_pk_guids
9
+    {
10
+        
11
+        public Guid id { get; set; }
12
+        
13
+        public string name { get; set; }
14
+        
15
+        public Guid? pk_guid_id { get; set; }
16
+        
17
+        
18
+        public virtual pk_guid fk_pk_guid { get; set; }
19
+        
20
+        
21
+    }
22
+}

+ 2
- 0
TestUtils/DataAccess/Models/pk_guid.cs Datei anzeigen

@@ -20,5 +20,7 @@ namespace TestUtils.DataAccess.Models
20 20
         
21 21
         
22 22
         
23
+        public virtual IList<fk_pk_guids> fk_pk_guids_fk { get; set; }
24
+        
23 25
     }
24 26
 }

+ 1
- 2
TestUtils/DataAccess/code-from-ds/DataSource.twig Datei anzeigen

@@ -1,6 +1,5 @@
1 1
 using Microsoft.EntityFrameworkCore;
2
-using Luticate2.Auth.DataAccess.Models;
3
-using Test.Utils.DataAccess.Models;
2
+using TestUtils.DataAccess.Models;
4 3
 
5 4
 namespace TestUtils.DataAccess
6 5
 {

+ 17
- 0
TestUtils/DataAccess/code-from-ds/code-from-ds.json Datei anzeigen

@@ -4,6 +4,23 @@
4 4
     "modelsRelativePath": "TestUtils/DataAccess/Models",
5 5
     "selection": {
6 6
         "tables": [
7
+            {
8
+                "columns": [
9
+                    {
10
+                        "column": "id",
11
+                        "selected": true
12
+                    },
13
+                    {
14
+                        "column": "name",
15
+                        "selected": true
16
+                    },
17
+                    {
18
+                        "column": "pk_guid_id",
19
+                        "selected": true
20
+                    }
21
+                ],
22
+                "table": "fk_pk_guids"
23
+            },
7 24
             {
8 25
                 "columns": [
9 26
                     {

+ 9
- 0
TestUtils/Dbo/FkPkGuid/FkPkGuidAddDbo.cs Datei anzeigen

@@ -0,0 +1,9 @@
1
+namespace TestUtils.Dbo.FkPkGuid
2
+{
3
+    public class FkPkGuidAddDbo
4
+    {
5
+        public string Name { get; set; }
6
+
7
+        public string PkGuidId { get; set; }
8
+    }
9
+}

+ 15
- 0
TestUtils/Dbo/FkPkGuid/FkPkGuidDbo.cs Datei anzeigen

@@ -0,0 +1,15 @@
1
+using TestUtils.Dbo.PkGuid;
2
+
3
+namespace TestUtils.Dbo.FkPkGuid
4
+{
5
+    public class FkPkGuidDbo
6
+    {
7
+        public string Id { get; set; }
8
+
9
+        public string Name { get; set; }
10
+
11
+        public string PkGuidId { get; set; }
12
+
13
+        public PkGuidDbo PkGuid { get; set; }
14
+    }
15
+}

+ 27
- 2
TestUtils/EfCrudDataAccess/LuEfCreateDataAccessTest.cs Datei anzeigen

@@ -1,14 +1,14 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4
-using Luticate2.Utils.Dbo;
5 4
 using Luticate2.Utils.Dbo.Result;
6 5
 using TestUtils.DataAccess;
6
+using TestUtils.Dbo.FkPkGuid;
7 7
 using TestUtils.Dbo.PkBigSerial;
8 8
 using TestUtils.Dbo.PkGuid;
9 9
 using Xunit;
10 10
 
11
-namespace TestUtils.EfCrubDataAccesss
11
+namespace TestUtils.EfCrudDataAccess
12 12
 {
13 13
     public class LuEfCreateDataAccessTest
14 14
     {
@@ -382,5 +382,30 @@ namespace TestUtils.EfCrubDataAccesss
382 382
                 Assert.Equal(res.Data.Id, get.Data.Id);
383 383
             });
384 384
         }
385
+
386
+
387
+        [Fact]
388
+        public void TestAddDboSingleFk1()
389
+        {
390
+            Tests.TestRealDb(context =>
391
+            {
392
+                var pkGuidDataAccess = new LuUtilsPkGuidDataAccess(context);
393
+                var fkPkGuidDataAccess = new LuUtilsFkPkGuidDataAccess(context);
394
+                var pkGuidDbo = pkGuidDataAccess.AddDbo(new PkGuidAddDbo
395
+                {
396
+                    SomeInt = 42,
397
+                    SomeText = "Test."
398
+                });
399
+                Assert.Equal(LuStatus.Success, pkGuidDbo.Status);
400
+                var fkPkGuidDbo = fkPkGuidDataAccess.AddDbo(new FkPkGuidAddDbo
401
+                {
402
+                    Name = "Test.",
403
+                    PkGuidId = pkGuidDbo.Data.Id
404
+                });
405
+                Assert.Equal(LuStatus.Success, fkPkGuidDbo.Status);
406
+                Assert.NotNull(fkPkGuidDbo.Data.PkGuid);
407
+                Assert.Equal(pkGuidDbo.Data.Id, fkPkGuidDbo.Data.PkGuid.Id);
408
+            });
409
+        }
385 410
     }
386 411
 }

+ 4
- 1
TestUtils/Tests.cs Datei anzeigen

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using System.Linq;
2 3
 using Microsoft.EntityFrameworkCore;
3 4
 using Microsoft.Extensions.DependencyInjection;
4 5
 using TestUtils.DataAccess;
@@ -16,15 +17,17 @@ namespace TestUtils
16 17
             serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
17 18
                 ServiceLifetime.Transient);
18 19
             var serviceProvider = serviceCollection.BuildServiceProvider();
19
-            var dbContext = (LuUtilsDbContext)serviceProvider.GetService(typeof(LuUtilsDbContext));
20 20
             try
21 21
             {
22 22
                 func(serviceProvider);
23 23
             }
24 24
             finally
25 25
             {
26
+                var dbContext = (LuUtilsDbContext)serviceProvider.GetService(typeof(LuUtilsDbContext));
26 27
                 dbContext.pk_bigserial.RemoveRange(dbContext.pk_bigserial);
27 28
                 dbContext.pk_guid.RemoveRange(dbContext.pk_guid);
29
+                dbContext.fk_pk_guids.RemoveRange(dbContext.fk_pk_guids);
30
+
28 31
                 dbContext.SaveChanges();
29 32
                 dbContext.Dispose();
30 33
             }

+ 1
- 1
TestUtils/project.json Datei anzeigen

@@ -5,7 +5,7 @@
5 5
     },
6 6
     "dependencies": {
7 7
         "dotnet-test-xunit": "1.0.0-rc2-*",
8
-        "Luticate2.Utils": "0.1.*",
8
+        "Luticate2.Utils": "0.2.*",
9 9
         "Moq": "4.6.38-alpha",
10 10
         "System.Runtime.Serialization.Primitives": "4.1.1",
11 11
         "xunit": "2.1.0"

+ 1
- 1
WebApiUtils/project.json Datei anzeigen

@@ -1,6 +1,6 @@
1 1
 {
2 2
     "dependencies": {
3
-        "Luticate2.Utils": "0.1.*",
3
+        "Luticate2.Utils": "0.2.*",
4 4
         "TestUtils": "0.1.*",
5 5
         "Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
6 6
         "Microsoft.AspNetCore.Mvc": "1.1.0",

Laden…
Abbrechen
Speichern