Browse Source

added crud interface; tests; lubusiness; lucontroller

tags/v0.1.0
Robin Thoni 8 years ago
parent
commit
3c407b81c5

+ 0
- 1
Luticate2.Utils/Business/LuBusiness.cs View File

@@ -2,6 +2,5 @@
2 2
 {
3 3
     public abstract class LuBusiness
4 4
     {
5
-
6 5
     }
7 6
 }

+ 262
- 0
Luticate2.Utils/Business/LuCrudBusiness.cs View File

@@ -0,0 +1,262 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using Luticate2.Utils.Dbo;
4
+using Luticate2.Utils.Interfaces;
5
+
6
+namespace Luticate2.Utils.Business
7
+{
8
+    public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
9
+        where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
10
+        where TDboCreate : class
11
+        where TDboRead : class
12
+        where TDboUpdate : class
13
+    {
14
+        protected readonly TDataAccess Db;
15
+
16
+        protected LuCrudBusiness(TDataAccess db)
17
+        {
18
+            Db = db;
19
+        }
20
+
21
+        protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
22
+        {
23
+            return LuResult<TDboCreate>.Ok(obj);
24
+        }
25
+
26
+        protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
27
+        {
28
+            var list = new List<TDboCreate>();
29
+            foreach (var obj in objs)
30
+            {
31
+                var res = CheckAdd(obj);
32
+                if (!res)
33
+                {
34
+                    return res.To<IEnumerable<TDboCreate>>();
35
+                }
36
+                list.Add(res.Data);
37
+            }
38
+            return LuResult<IEnumerable<TDboCreate>>.Ok(list);
39
+        }
40
+
41
+        protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
42
+        {
43
+            return LuResult<TDboUpdate>.Ok(update);
44
+        }
45
+
46
+        protected LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
47
+        {
48
+            var res = GetSingleById(id);
49
+            if (!res)
50
+            {
51
+                return res.To<TDboUpdate>();
52
+            }
53
+            return CheckEdit(res.Data, update);
54
+        }
55
+
56
+        protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
57
+        {
58
+            var res = GetSingleById(id);
59
+            if (!res)
60
+            {
61
+                return res.To<TDboUpdate>();
62
+            }
63
+            return CheckEdit(res.Data, update);
64
+        }
65
+
66
+
67
+
68
+        public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
69
+        {
70
+            var res = CheckAdd(objs);
71
+            if (!res)
72
+            {
73
+                return res.To<T>();
74
+            }
75
+            return Db.Add(res.Data, returnFunc);
76
+        }
77
+
78
+        public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
79
+        {
80
+            var res = CheckAdd(obj);
81
+            if (!res)
82
+            {
83
+                return res.To<T>();
84
+            }
85
+            return Db.Add(res.Data, returnFunc);
86
+        }
87
+
88
+        public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
89
+        {
90
+            var res = CheckAdd(objs);
91
+            if (!res)
92
+            {
93
+                return res.To<IEnumerable<string>>();
94
+            }
95
+            return Db.AddGuid(res.Data);
96
+        }
97
+
98
+        public LuResult<string> AddGuid(TDboCreate obj)
99
+        {
100
+            var res = CheckAdd(obj);
101
+            if (!res)
102
+            {
103
+                return res.To<string>();
104
+            }
105
+            return Db.AddGuid(res.Data);
106
+        }
107
+
108
+        public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
109
+        {
110
+            var res = CheckAdd(obj);
111
+            if (!res)
112
+            {
113
+                return res.To<IEnumerable<long>>();
114
+            }
115
+            return Db.AddId(res.Data);
116
+        }
117
+
118
+        public LuResult<long> AddId(TDboCreate obj)
119
+        {
120
+            var res = CheckAdd(obj);
121
+            if (!res)
122
+            {
123
+                return res.To<long>();
124
+            }
125
+            return Db.AddId(res.Data);
126
+        }
127
+
128
+        public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
129
+        {
130
+            var res = CheckAdd(obj);
131
+            if (!res)
132
+            {
133
+                return res.To<IEnumerable<TDboRead>>();
134
+            }
135
+            return Db.AddDbo(res.Data);
136
+        }
137
+
138
+        public LuResult<TDboRead> AddDbo(TDboCreate obj)
139
+        {
140
+            var res = CheckAdd(obj);
141
+            if (!res)
142
+            {
143
+                return res.To<TDboRead>();
144
+            }
145
+            return Db.AddDbo(res.Data);
146
+        }
147
+
148
+
149
+
150
+
151
+        public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
152
+        {
153
+            return Db.GetSingleByKeys(keys);
154
+        }
155
+
156
+        public LuResult<TDboRead> GetSingleById(string id)
157
+        {
158
+            return Db.GetSingleById(id);
159
+        }
160
+
161
+        public LuResult<TDboRead> GetSingleById(long id)
162
+        {
163
+            return Db.GetSingleById(id);
164
+        }
165
+
166
+
167
+
168
+
169
+        public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
170
+        {
171
+            var obj = GetAndCheckEdit(id, update);
172
+            if (!obj)
173
+            {
174
+                return obj.To<T>();
175
+            }
176
+            return Db.EditSingleById(id, obj.Data, returnFunc);
177
+        }
178
+
179
+        public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
180
+        {
181
+            var obj = GetAndCheckEdit(id, update);
182
+            if (!obj)
183
+            {
184
+                return obj.To<long>();
185
+            }
186
+            return Db.EditSingleByIdId(id, obj.Data);
187
+        }
188
+
189
+        public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
190
+        {
191
+            var obj = GetAndCheckEdit(id, update);
192
+            if (!obj)
193
+            {
194
+                return obj.To<TDboRead>();
195
+            }
196
+            return Db.EditSingleByIdDbo(id, obj.Data);
197
+        }
198
+
199
+        public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
200
+        {
201
+            var obj = GetAndCheckEdit(id, update);
202
+            if (!obj)
203
+            {
204
+                return obj.To<T>();
205
+            }
206
+            return Db.EditSingleById(id, obj.Data, returnFunc);
207
+        }
208
+
209
+        public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
210
+        {
211
+            var obj = GetAndCheckEdit(id, update);
212
+            if (!obj)
213
+            {
214
+                return obj.To<string>();
215
+            }
216
+            return Db.EditSingleByIdGuid(id, obj.Data);
217
+        }
218
+
219
+        public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
220
+        {
221
+            var obj = GetAndCheckEdit(id, update);
222
+            if (!obj)
223
+            {
224
+                return obj.To<TDboRead>();
225
+            }
226
+            return Db.EditSingleByIdDbo(id, obj.Data);
227
+        }
228
+
229
+
230
+
231
+
232
+        public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
233
+        {
234
+            return Db.DeleteSingleById(id, returnFunc);
235
+        }
236
+
237
+        public LuResult<string> DeleteSingleByIdGuid(string id)
238
+        {
239
+            return Db.DeleteSingleByIdGuid(id);
240
+        }
241
+
242
+        public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
243
+        {
244
+            return Db.DeleteSingleByIdDbo(id);
245
+        }
246
+
247
+        public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
248
+        {
249
+            return Db.DeleteSingleById(id, returnFunc);
250
+        }
251
+
252
+        public LuResult<long> DeleteSingleByIdId(long id)
253
+        {
254
+            return Db.DeleteSingleByIdId(id);
255
+        }
256
+
257
+        public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
258
+        {
259
+            return Db.DeleteSingleByIdDbo(id);
260
+        }
261
+    }
262
+}

+ 11
- 0
Luticate2.Utils/Controllers/LuController.cs View File

@@ -1,4 +1,6 @@
1 1
 using System.Collections.Generic;
2
+using Luticate2.Utils.Dbo;
3
+using Microsoft.AspNetCore.Http;
2 4
 using Microsoft.AspNetCore.Mvc;
3 5
 using Microsoft.AspNetCore.SignalR.Infrastructure;
4 6
 
@@ -11,5 +13,14 @@ namespace Luticate2.Utils.Controllers
11 13
         protected IDictionary<object, object> LuItems => (IDictionary<object, object>) HttpContext.Items["luticateItems"];
12 14
 
13 15
 //        protected UsersDbo LuCurrentUser => (UsersDbo) LuItems["luticateItems"];
16
+
17
+        protected T Handle<T>(LuResult<T> result)
18
+        {
19
+            if (result)
20
+            {
21
+                return result.Data;
22
+            }
23
+            return default(T);//TODO
24
+        }
14 25
     }
15 26
 }

Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs → Luticate2.Utils/DataAccess/LuEfCrudInterface.cs View File

@@ -3,19 +3,20 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Linq.Expressions;
5 5
 using Luticate2.Utils.Dbo;
6
+using Luticate2.Utils.Interfaces;
6 7
 using Microsoft.EntityFrameworkCore;
7 8
 
8 9
 namespace Luticate2.Utils.DataAccess
9 10
 {
10
-    public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
11
-            LuEfDataAccess<TModel, TDbContext>
11
+    public abstract class LuEfCrudInterface<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
12
+            LuEfDataAccess<TModel, TDbContext>, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
12 13
         where TModel : class
13 14
         where TDboCreate : class
14 15
         where TDboRead : class
15 16
         where TDboUpdate : class
16 17
         where TDbContext : DbContext
17 18
     {
18
-        protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
19
+        protected LuEfCrudInterface(TDbContext db, DbSet<TModel> table) : base(db, table)
19 20
         {
20 21
         }
21 22
 

+ 70
- 0
Luticate2.Utils/Interfaces/ILuCrudInterface.cs View File

@@ -0,0 +1,70 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using Luticate2.Utils.Dbo;
4
+
5
+namespace Luticate2.Utils.Interfaces
6
+{
7
+    public interface ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
8
+        where TDboCreate : class
9
+        where TDboRead : class
10
+        where TDboUpdate : class
11
+    {
12
+        LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc);
13
+
14
+        LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc);
15
+
16
+
17
+        LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs);
18
+
19
+        LuResult<string> AddGuid(TDboCreate obj);
20
+
21
+
22
+        LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj);
23
+
24
+        LuResult<long> AddId(TDboCreate obj);
25
+
26
+        LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj);
27
+
28
+        LuResult<TDboRead> AddDbo(TDboCreate obj);
29
+
30
+
31
+
32
+
33
+        LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys);
34
+
35
+        LuResult<TDboRead> GetSingleById(string id);
36
+
37
+        LuResult<TDboRead> GetSingleById(long id);
38
+
39
+
40
+
41
+        LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc);
42
+
43
+        LuResult<long> EditSingleByIdId(long id, TDboUpdate update);
44
+
45
+        LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update);
46
+
47
+
48
+        LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc);
49
+
50
+        LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update);
51
+
52
+        LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update);
53
+
54
+
55
+
56
+
57
+        LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc);
58
+
59
+        LuResult<string> DeleteSingleByIdGuid(string id);
60
+
61
+        LuResult<TDboRead> DeleteSingleByIdDbo(string id);
62
+
63
+
64
+        LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc);
65
+
66
+        LuResult<long> DeleteSingleByIdId(long id);
67
+
68
+        LuResult<TDboRead> DeleteSingleByIdDbo(long id);
69
+    }
70
+}

+ 1
- 1
Test/Utils/DataAccess/LuUtilsPkBigSerialDataAccess.cs View File

@@ -4,7 +4,7 @@ using Test.Utils.Dbo.PkBigSerial;
4 4
 
5 5
 namespace Test.Utils.DataAccess
6 6
 {
7
-    public class LuUtilsPkBigSerialDataAccess : LuEfCrudDataAccess<pk_bigserial, PkBigSerialAddDbo, PkBigSerialDbo, PkBigSerialAddDbo, LuUtilsDbContext>
7
+    public class LuUtilsPkBigSerialDataAccess : LuEfCrudInterface<pk_bigserial, PkBigSerialAddDbo, PkBigSerialDbo, PkBigSerialAddDbo, LuUtilsDbContext>
8 8
     {
9 9
         public LuUtilsPkBigSerialDataAccess(LuUtilsDbContext db) : base(db, db.pk_bigserial)
10 10
         {

+ 1
- 1
Test/Utils/DataAccess/LuUtilsPkGuidDataAccess.cs View File

@@ -4,7 +4,7 @@ using Test.Utils.Dbo.PkGuid;
4 4
 
5 5
 namespace Test.Utils.DataAccess
6 6
 {
7
-    public class LuUtilsPkGuidDataAccess : LuEfCrudDataAccess<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext>
7
+    public class LuUtilsPkGuidDataAccess : LuEfCrudInterface<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext>
8 8
     {
9 9
         public LuUtilsPkGuidDataAccess(LuUtilsDbContext db) : base(db, db.pk_guid)
10 10
         {

+ 5
- 2
Test/Utils/Tests.cs View File

@@ -1,15 +1,18 @@
1 1
 using System;
2
+using Castle.Core.Resource;
2 3
 using Test.Utils.DataAccess;
3 4
 
4 5
 namespace Test.Utils
5 6
 {
6 7
     public class Tests
7 8
     {
9
+        public const string CONNECTION_STRING =
10
+            "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
11
+
8 12
         public static LuUtilsDbContext GetDataBaseContext()
9 13
         {
10 14
             return
11
-                new LuUtilsDbContext(
12
-                    "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;");
15
+                new LuUtilsDbContext(CONNECTION_STRING);
13 16
         }
14 17
 
15 18
         public static void TestRealDb(Action<LuUtilsDbContext> func)

+ 13
- 0
WebTest/Business/PkGuidBusiness.cs View File

@@ -0,0 +1,13 @@
1
+using Luticate2.Utils.Business;
2
+using Test.Utils.DataAccess;
3
+using Test.Utils.Dbo.PkGuid;
4
+
5
+namespace WebTest.Business
6
+{
7
+    public class PkGuidBusiness : LuCrudBusiness<LuUtilsPkGuidDataAccess, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo>
8
+    {
9
+        public PkGuidBusiness(LuUtilsPkGuidDataAccess db) : base(db)
10
+        {
11
+        }
12
+    }
13
+}

+ 29
- 0
WebTest/Controllers/PkGuidController.cs View File

@@ -0,0 +1,29 @@
1
+using Luticate2.Utils.Controllers;
2
+using Microsoft.AspNetCore.Mvc;
3
+using Test.Utils.Dbo.PkGuid;
4
+using WebTest.Business;
5
+
6
+namespace WebTest.Controllers
7
+{
8
+    public class PkGuidController : LuController
9
+    {
10
+        private readonly PkGuidBusiness _busines;
11
+
12
+        public PkGuidController(PkGuidBusiness busines)
13
+        {
14
+            _busines = busines;
15
+        }
16
+
17
+        [Route("[controller]/{id}")]
18
+        public PkGuidDbo Get(string id)
19
+        {
20
+            return Handle(_busines.GetSingleById(id));
21
+        }
22
+
23
+//        [Route("[controller]")]
24
+//        public PkGuidDbo Get()
25
+//        {
26
+//            return Handle(_busines.GetMultiple());
27
+//        }
28
+    }
29
+}

+ 9
- 0
WebTest/Startup.cs View File

@@ -1,9 +1,14 @@
1 1
 using Luticate2.Auth.Controllers;
2 2
 using Microsoft.AspNetCore.Builder;
3 3
 using Microsoft.AspNetCore.Hosting;
4
+using Microsoft.EntityFrameworkCore;
4 5
 using Microsoft.Extensions.Configuration;
5 6
 using Microsoft.Extensions.DependencyInjection;
6 7
 using Microsoft.Extensions.Logging;
8
+using Test.Utils;
9
+using Test.Utils.DataAccess;
10
+using WebTest.Business;
11
+using WebTest.Controllers;
7 12
 
8 13
 namespace WebTest
9 14
 {
@@ -36,6 +41,10 @@ namespace WebTest
36 41
 
37 42
             services.AddLuticate();
38 43
 
44
+            services.AddSingleton<PkGuidBusiness>();
45
+            services.AddSingleton<LuUtilsPkGuidDataAccess>();
46
+            services.AddDbContext<LuUtilsDbContext>(options => options.UseNpgsql(Tests.CONNECTION_STRING));
47
+
39 48
             services.AddMvc()
40 49
                 .AddLuticate();
41 50
 

+ 1
- 0
WebTest/project.json View File

@@ -1,6 +1,7 @@
1 1
 {
2 2
   "dependencies": {
3 3
     "Luticate2.Auth": "1.0.*",
4
+    "Test": "1.0.*",
4 5
     "Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
5 6
     "Microsoft.AspNetCore.Mvc": "1.1.0",
6 7
     "Microsoft.AspNetCore.Routing": "1.1.0",

+ 957
- 13
WebTest/project.lock.json
File diff suppressed because it is too large
View File


Loading…
Cancel
Save