Browse Source

refactor; example crud controller

tags/v0.1.0
Robin Thoni 8 years ago
parent
commit
24c66716e8

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

149
 
149
 
150
 
150
 
151
 
151
 
152
-        public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
153
-        {
154
-            return NextCrud.GetSingleByKeys(keys);
155
-        }
156
-
157
         public LuResult<TDboRead> GetSingleById(string id)
152
         public LuResult<TDboRead> GetSingleById(string id)
158
         {
153
         {
159
             return NextCrud.GetSingleById(id);
154
             return NextCrud.GetSingleById(id);

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

1
 using System.Collections.Generic;
1
 using System.Collections.Generic;
2
 using Luticate2.Utils.Dbo;
2
 using Luticate2.Utils.Dbo;
3
-using Microsoft.AspNetCore.Http;
4
 using Microsoft.AspNetCore.Mvc;
3
 using Microsoft.AspNetCore.Mvc;
5
 using Microsoft.AspNetCore.SignalR.Infrastructure;
4
 using Microsoft.AspNetCore.SignalR.Infrastructure;
6
 
5
 

+ 1
- 1
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

3
 
3
 
4
 namespace Luticate2.Utils.Controllers
4
 namespace Luticate2.Utils.Controllers
5
 {
5
 {
6
-    public static class LuUtilsExtensions
6
+    public static class LuControllerUtilsExtensions
7
     {
7
     {
8
         public static IDictionary<object, object> GetLuItems(this HttpContext context)
8
         public static IDictionary<object, object> GetLuItems(this HttpContext context)
9
         {
9
         {

+ 18
- 9
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs View File

6
 using Luticate2.Utils.Dbo;
6
 using Luticate2.Utils.Dbo;
7
 using Luticate2.Utils.Dbo.OrderBy;
7
 using Luticate2.Utils.Dbo.OrderBy;
8
 using Luticate2.Utils.Interfaces;
8
 using Luticate2.Utils.Interfaces;
9
+using Luticate2.Utils.Utils;
9
 using Microsoft.EntityFrameworkCore;
10
 using Microsoft.EntityFrameworkCore;
10
 
11
 
11
 namespace Luticate2.Utils.DataAccess
12
 namespace Luticate2.Utils.DataAccess
28
 
29
 
29
         protected abstract TDboRead GetDboFromModel(TModel model);
30
         protected abstract TDboRead GetDboFromModel(TModel model);
30
 
31
 
31
-        protected string GetFieldName(string name)
32
+        protected virtual Expression<Func<TModel, object>> GetOrderByFieldExpression(string fieldName)
32
         {
33
         {
33
-            return
34
-                string.Concat(name.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()))
35
-                    .ToLower();
34
+            fieldName = fieldName.ToSnakeCase();
35
+            if (!typeof(TModel).HasProperty(fieldName))
36
+            {
37
+                return null;
38
+            }
39
+            var param = Expression.Parameter(typeof(TModel), "x");
40
+            var prop = Expression.Property(param, fieldName);
41
+            var converted = Expression.Convert(prop, typeof(object));
42
+            var exp = Expression.Lambda<Func<TModel, object>>(converted, param);
43
+            return exp;
36
         }
44
         }
37
 
45
 
38
         protected Func<TDboRead, T> GetIdFunc<T>()
46
         protected Func<TDboRead, T> GetIdFunc<T>()
190
                 IOrderedQueryable<TModel> ordered = null;
198
                 IOrderedQueryable<TModel> ordered = null;
191
                 foreach (var field in orderBy.Fields)
199
                 foreach (var field in orderBy.Fields)
192
                 {
200
                 {
193
-                    var fieldName = GetFieldName(field.Name);
194
-                    var param = Expression.Parameter(typeof(TModel), "x");
195
-                    var prop = Expression.Property(param, fieldName);
196
-                    var converted = Expression.Convert(prop, typeof(object));
197
-                    var exp = Expression.Lambda<Func<TModel, object>>(converted, param);
201
+                    var exp = GetOrderByFieldExpression(field.Name);
202
+                    if (exp == null)
203
+                    {
204
+                        return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
205
+                            string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
206
+                    }
198
                     if (ordered != null)
207
                     if (ordered != null)
199
                     {
208
                     {
200
                         ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
209
                         ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);

+ 1
- 2
Luticate2.Utils/Dbo/OrderBy/LuOrderByBinder.cs View File

55
         public Task BindModelAsync(ModelBindingContext bindingContext)
55
         public Task BindModelAsync(ModelBindingContext bindingContext)
56
         {
56
         {
57
             var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
57
             var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
58
-            var data = messageTypeResult.FirstValue;
59
-            var res = FromString(data);
58
+            var res = FromString(messageTypeResult.FirstValue);
60
             if (res)
59
             if (res)
61
             {
60
             {
62
                 bindingContext.Result = ModelBindingResult.Success(res.Data);
61
                 bindingContext.Result = ModelBindingResult.Success(res.Data);

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

31
 
31
 
32
 
32
 
33
 
33
 
34
-        LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys);
35
-
36
         LuResult<TDboRead> GetSingleById(string id);
34
         LuResult<TDboRead> GetSingleById(string id);
37
 
35
 
38
         LuResult<TDboRead> GetSingleById(long id);
36
         LuResult<TDboRead> GetSingleById(long id);

+ 19
- 0
Luticate2.Utils/Utils/LuUtilsExtension.cs View File

1
+using System;
2
+using System.Linq;
3
+using System.Reflection;
4
+
5
+namespace Luticate2.Utils.Utils
6
+{
7
+    public static class LuUtilsExtension
8
+    {
9
+        public static string ToSnakeCase(this string str)
10
+        {
11
+            return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
12
+        }
13
+
14
+        public static bool HasProperty(this Type type, string name)
15
+        {
16
+            return type.GetProperty(name) != null;
17
+        }
18
+    }
19
+}

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

1
 using Luticate2.Utils.Business;
1
 using Luticate2.Utils.Business;
2
+using Luticate2.Utils.Dbo;
2
 using Test.Utils.DataAccess;
3
 using Test.Utils.DataAccess;
3
 using Test.Utils.Dbo.PkGuid;
4
 using Test.Utils.Dbo.PkGuid;
4
 
5
 
9
         public PkGuidBusiness(LuUtilsPkGuidDataAccess nextCrud) : base(nextCrud)
10
         public PkGuidBusiness(LuUtilsPkGuidDataAccess nextCrud) : base(nextCrud)
10
         {
11
         {
11
         }
12
         }
13
+
14
+        protected override LuResult<PkGuidAddDbo> CheckAdd(PkGuidAddDbo obj)
15
+        {
16
+            if (obj.SomeText.EndsWith("_edited"))
17
+            {
18
+                return LuResult<PkGuidAddDbo>.Error(LuStatus.InputError, "someText can not end with '_edited'");
19
+            }
20
+            return LuResult<PkGuidAddDbo>.Ok(obj);
21
+        }
22
+
23
+        protected override LuResult<PkGuidAddDbo> CheckEdit(PkGuidDbo dbo, PkGuidAddDbo update)
24
+        {
25
+            if (!update.SomeText.EndsWith("_edited"))
26
+            {
27
+                return LuResult<PkGuidAddDbo>.Error(LuStatus.InputError, "someText must end with '_edited'");
28
+            }
29
+            return LuResult<PkGuidAddDbo>.Ok(update);
30
+        }
12
     }
31
     }
13
 }
32
 }

+ 26
- 4
WebTest/Controllers/PkGuidController.cs View File

1
-using System.Collections.Generic;
2
-using Luticate2.Utils.Controllers;
1
+using Luticate2.Utils.Controllers;
3
 using Luticate2.Utils.Dbo;
2
 using Luticate2.Utils.Dbo;
4
 using Luticate2.Utils.Dbo.OrderBy;
3
 using Luticate2.Utils.Dbo.OrderBy;
5
 using Microsoft.AspNetCore.Mvc;
4
 using Microsoft.AspNetCore.Mvc;
17
             _busines = busines;
16
             _busines = busines;
18
         }
17
         }
19
 
18
 
19
+        [HttpGet]
20
         [Route("[controller]/{id}")]
20
         [Route("[controller]/{id}")]
21
-        public PkGuidDbo Get(string id)
21
+        public PkGuidDbo GetSingleById(string id)
22
         {
22
         {
23
             return Handle(_busines.GetSingleById(id));
23
             return Handle(_busines.GetSingleById(id));
24
         }
24
         }
25
 
25
 
26
+        [HttpGet]
26
         [Route("[controller]")]
27
         [Route("[controller]")]
27
-        public LuPaginatedDbo<PkGuidDbo> Get(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
28
+        public LuPaginatedDbo<PkGuidDbo> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
28
         {
29
         {
29
             return Handle(_busines.GetMultiple(orderBy, page, perPage));
30
             return Handle(_busines.GetMultiple(orderBy, page, perPage));
30
         }
31
         }
32
+
33
+        [HttpPost]
34
+        [Route("[controller]")]
35
+        public PkGuidDbo Add([FromBody]PkGuidAddDbo data)
36
+        {
37
+            return Handle(_busines.AddDbo(data));
38
+        }
39
+
40
+        [HttpPost]
41
+        [Route("[controller]/{id}")]
42
+        public PkGuidDbo Edit(string id, [FromBody]PkGuidAddDbo data)
43
+        {
44
+            return Handle(_busines.EditSingleByIdDbo(id, data));
45
+        }
46
+
47
+        [HttpDelete]
48
+        [Route("[controller]/{id}")]
49
+        public PkGuidDbo Delete(string id)
50
+        {
51
+            return Handle(_busines.DeleteSingleByIdDbo(id));
52
+        }
31
     }
53
     }
32
 }
54
 }

Loading…
Cancel
Save