Ver código fonte

refactor; example crud controller

tags/v0.1.0
Robin Thoni 8 anos atrás
pai
commit
24c66716e8

+ 0
- 5
Luticate2.Utils/Business/LuCrudBusiness.cs Ver arquivo

@@ -149,11 +149,6 @@ namespace Luticate2.Utils.Business
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 152
         public LuResult<TDboRead> GetSingleById(string id)
158 153
         {
159 154
             return NextCrud.GetSingleById(id);

+ 0
- 1
Luticate2.Utils/Controllers/LuController.cs Ver arquivo

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

+ 1
- 1
Luticate2.Utils/Controllers/LuUtilsExtensions.cs Ver arquivo

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

+ 18
- 9
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs Ver arquivo

@@ -6,6 +6,7 @@ using System.Reflection;
6 6
 using Luticate2.Utils.Dbo;
7 7
 using Luticate2.Utils.Dbo.OrderBy;
8 8
 using Luticate2.Utils.Interfaces;
9
+using Luticate2.Utils.Utils;
9 10
 using Microsoft.EntityFrameworkCore;
10 11
 
11 12
 namespace Luticate2.Utils.DataAccess
@@ -28,11 +29,18 @@ namespace Luticate2.Utils.DataAccess
28 29
 
29 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 46
         protected Func<TDboRead, T> GetIdFunc<T>()
@@ -190,11 +198,12 @@ namespace Luticate2.Utils.DataAccess
190 198
                 IOrderedQueryable<TModel> ordered = null;
191 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 207
                     if (ordered != null)
199 208
                     {
200 209
                         ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);

+ 1
- 2
Luticate2.Utils/Dbo/OrderBy/LuOrderByBinder.cs Ver arquivo

@@ -55,8 +55,7 @@ namespace Luticate2.Utils.Dbo.OrderBy
55 55
         public Task BindModelAsync(ModelBindingContext bindingContext)
56 56
         {
57 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 59
             if (res)
61 60
             {
62 61
                 bindingContext.Result = ModelBindingResult.Success(res.Data);

+ 0
- 2
Luticate2.Utils/Interfaces/ILuCrudInterface.cs Ver arquivo

@@ -31,8 +31,6 @@ namespace Luticate2.Utils.Interfaces
31 31
 
32 32
 
33 33
 
34
-        LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys);
35
-
36 34
         LuResult<TDboRead> GetSingleById(string id);
37 35
 
38 36
         LuResult<TDboRead> GetSingleById(long id);

+ 19
- 0
Luticate2.Utils/Utils/LuUtilsExtension.cs Ver arquivo

@@ -0,0 +1,19 @@
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 Ver arquivo

@@ -1,4 +1,5 @@
1 1
 using Luticate2.Utils.Business;
2
+using Luticate2.Utils.Dbo;
2 3
 using Test.Utils.DataAccess;
3 4
 using Test.Utils.Dbo.PkGuid;
4 5
 
@@ -9,5 +10,23 @@ namespace WebTest.Business
9 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 Ver arquivo

@@ -1,5 +1,4 @@
1
-using System.Collections.Generic;
2
-using Luticate2.Utils.Controllers;
1
+using Luticate2.Utils.Controllers;
3 2
 using Luticate2.Utils.Dbo;
4 3
 using Luticate2.Utils.Dbo.OrderBy;
5 4
 using Microsoft.AspNetCore.Mvc;
@@ -17,16 +16,39 @@ namespace WebTest.Controllers
17 16
             _busines = busines;
18 17
         }
19 18
 
19
+        [HttpGet]
20 20
         [Route("[controller]/{id}")]
21
-        public PkGuidDbo Get(string id)
21
+        public PkGuidDbo GetSingleById(string id)
22 22
         {
23 23
             return Handle(_busines.GetSingleById(id));
24 24
         }
25 25
 
26
+        [HttpGet]
26 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 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
 }

Carregando…
Cancelar
Salvar