浏览代码

perform full get after add/edit/delete

tags/v0.2.3
Robin Thoni 8 年前
父节点
当前提交
0665d6d47d

+ 48
- 12
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs 查看文件

66
             return id;
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
         protected virtual IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
79
         protected virtual IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
70
         {
80
         {
71
             return table;
81
             return table;
107
 
117
 
108
         public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
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
                 table.AddRange(models);
123
                 table.AddRange(models);
114
                 db.SaveChanges();
124
                 db.SaveChanges();
115
                 foreach (var model in models)
125
                 foreach (var model in models)
116
                 {
126
                 {
117
                     db.Entry(model).State = EntityState.Detached;
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
         public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
138
         public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
182
             return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
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
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
214
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
187
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
215
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
279
         public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
307
         public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
280
             Func<IEnumerable<TDboRead>, T> returnFunc)
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
                 foreach (var model in models)
314
                 foreach (var model in models)
286
                 {
315
                 {
287
                     update(model);
316
                     update(model);
288
                     db.Entry(model).State = EntityState.Modified;
317
                     db.Entry(model).State = EntityState.Modified;
289
                 }
318
                 }
290
                 db.SaveChanges();
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
         public LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
329
         public LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
358
             return Execute((db, table) =>
390
             return Execute((db, table) =>
359
             {
391
             {
360
                 var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
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
                 table.RemoveRange(models);
398
                 table.RemoveRange(models);
362
                 db.SaveChanges();
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 查看文件

30
         protected override void OnModelCreating(ModelBuilder modelBuilder)
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
             modelBuilder.Entity<pk_bigserial>()
48
             modelBuilder.Entity<pk_bigserial>()
34
                 .HasKey(c => new { c.id });
49
                 .HasKey(c => new { c.id });
35
             
50
             
58
             
73
             
59
         }
74
         }
60
         
75
         
76
+        public virtual DbSet<fk_pk_guids> fk_pk_guids { get; set; }
77
+        
61
         public virtual DbSet<pk_bigserial> pk_bigserial { get; set; }
78
         public virtual DbSet<pk_bigserial> pk_bigserial { get; set; }
62
         
79
         
63
         public virtual DbSet<pk_guid> pk_guid { get; set; }
80
         public virtual DbSet<pk_guid> pk_guid { get; set; }

+ 48
- 0
TestUtils/DataAccess/LuUtilsFkPkGuidDataAccess.cs 查看文件

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 查看文件

32
             model.some_text = obj.SomeText;
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
             return new PkGuidDbo
37
             return new PkGuidDbo
38
             {
38
             {
44
             };
44
             };
45
         }
45
         }
46
 
46
 
47
+        protected override PkGuidDbo GetDboFromModel(pk_guid model)
48
+        {
49
+            return GetDboFromModelStatic(model);
50
+        }
51
+
47
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
52
         protected override Expression<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
48
         {
53
         {
49
             return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
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 查看文件

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 查看文件

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 查看文件

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

+ 17
- 0
TestUtils/DataAccess/code-from-ds/code-from-ds.json 查看文件

4
     "modelsRelativePath": "TestUtils/DataAccess/Models",
4
     "modelsRelativePath": "TestUtils/DataAccess/Models",
5
     "selection": {
5
     "selection": {
6
         "tables": [
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
                 "columns": [
25
                 "columns": [
9
                     {
26
                     {

+ 9
- 0
TestUtils/Dbo/FkPkGuid/FkPkGuidAddDbo.cs 查看文件

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 查看文件

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 查看文件

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Linq;
3
 using System.Linq;
4
-using Luticate2.Utils.Dbo;
5
 using Luticate2.Utils.Dbo.Result;
4
 using Luticate2.Utils.Dbo.Result;
6
 using TestUtils.DataAccess;
5
 using TestUtils.DataAccess;
6
+using TestUtils.Dbo.FkPkGuid;
7
 using TestUtils.Dbo.PkBigSerial;
7
 using TestUtils.Dbo.PkBigSerial;
8
 using TestUtils.Dbo.PkGuid;
8
 using TestUtils.Dbo.PkGuid;
9
 using Xunit;
9
 using Xunit;
10
 
10
 
11
-namespace TestUtils.EfCrubDataAccesss
11
+namespace TestUtils.EfCrudDataAccess
12
 {
12
 {
13
     public class LuEfCreateDataAccessTest
13
     public class LuEfCreateDataAccessTest
14
     {
14
     {
382
                 Assert.Equal(res.Data.Id, get.Data.Id);
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 查看文件

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

+ 1
- 1
TestUtils/project.json 查看文件

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

+ 1
- 1
WebApiUtils/project.json 查看文件

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

正在加载...
取消
保存