Browse Source

refactor; tests; luorderbydbo; modelbinder

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

+ 4
- 0
Luticate2.Auth/Controllers/LuticateExtensions.cs View File

@@ -1,7 +1,9 @@
1 1
 using Luticate2.Auth.Business;
2 2
 using Luticate2.Auth.DataAccess;
3 3
 using Luticate2.Auth.Middlewares;
4
+using Luticate2.Utils.Dbo.OrderBy;
4 5
 using Microsoft.AspNetCore.Builder;
6
+using Microsoft.AspNetCore.Mvc;
5 7
 using Microsoft.Extensions.DependencyInjection;
6 8
 
7 9
 namespace Luticate2.Auth.Controllers
@@ -25,6 +27,8 @@ namespace Luticate2.Auth.Controllers
25 27
         {
26 28
 //            builder.AddApplicationPart(typeof(LuController).GetTypeInfo().Assembly)
27 29
 //                .AddControllersAsServices();
30
+            builder.Services.Configure<MvcOptions>(
31
+                options => options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider()));
28 32
             return builder;
29 33
         }
30 34
 

+ 4
- 4
Luticate2.Utils/Business/LuCrudBusiness.cs View File

@@ -5,15 +5,15 @@ using Luticate2.Utils.Interfaces;
5 5
 
6 6
 namespace Luticate2.Utils.Business
7 7
 {
8
-    public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
9
-        where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
8
+    public abstract class LuCrudBusiness<TNextCrud, TDboCreate, TDboRead, TDboUpdate> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
9
+        where TNextCrud : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
10 10
         where TDboCreate : class
11 11
         where TDboRead : class
12 12
         where TDboUpdate : class
13 13
     {
14
-        protected readonly TDataAccess Db;
14
+        protected readonly TNextCrud Db;
15 15
 
16
-        protected LuCrudBusiness(TDataAccess db)
16
+        protected LuCrudBusiness(TNextCrud db)
17 17
         {
18 18
             Db = db;
19 19
         }

Luticate2.Utils/Dbo/LuBoxedValueDbo.cs → Luticate2.Utils/Dbo/Basic/LuBoxedValueDbo.cs View File


Luticate2.Utils/Dbo/LuDbo.cs → Luticate2.Utils/Dbo/Basic/LuDbo.cs View File


Luticate2.Utils/Dbo/LuPaginatedDbo.cs → Luticate2.Utils/Dbo/Basic/LuPaginatedDbo.cs View File


+ 7
- 0
Luticate2.Utils/Dbo/Filter/LuFilterDbo.cs View File

@@ -0,0 +1,7 @@
1
+namespace Luticate2.Utils.Dbo.Filter
2
+{
3
+    public class LuFilterDbo
4
+    {
5
+
6
+    }
7
+}

+ 79
- 0
Luticate2.Utils/Dbo/OrderBy/LuOrderByBinder.cs View File

@@ -0,0 +1,79 @@
1
+using System.Collections.Generic;
2
+using System.Threading.Tasks;
3
+using Microsoft.AspNetCore.Mvc.ModelBinding;
4
+
5
+namespace Luticate2.Utils.Dbo.OrderBy
6
+{
7
+    public class LuOrderByBinder : IModelBinder
8
+    {
9
+        public static LuResult<LuOrderByDbo> FromString(string data)
10
+        {
11
+            data = data.Trim();
12
+            var dbo = new LuOrderByDbo {Fields = new List<LuOrderByFieldDbo>()};
13
+            if (data != "")
14
+            {
15
+                var fields = data.Split(',');
16
+                foreach (var field in fields)
17
+                {
18
+                    if (field == "")
19
+                    {
20
+                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
21
+                            string.Format("LuOrderByBinder: {0}", data), "Empty order by field");
22
+                    }
23
+                    var value = new LuOrderByFieldDbo();
24
+                    var split = field.Split(':');
25
+                    if (split.Length > 2 || split.Length == 0)
26
+                    {
27
+                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
28
+                            string.Format("LuOrderByBinder: {0}", data), "Invalid field syntax");
29
+                    }
30
+                    value.Name = split[0];
31
+                    var order = split.Length == 2 ? split[1].ToLower() : "asc";
32
+                    if (order == "asc")
33
+                    {
34
+                        value.Asc = true;
35
+                    }
36
+                    else if (order == "desc")
37
+                    {
38
+                        value.Asc = false;
39
+                    }
40
+                    else
41
+                    {
42
+                        return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
43
+                            string.Format("LuOrderByBinder: {0}", data), "Empty order by field order");
44
+                    }
45
+                    dbo.Fields.Add(value);
46
+                }
47
+            }
48
+            return LuResult<LuOrderByDbo>.Ok(dbo);
49
+        }
50
+
51
+        public Task BindModelAsync(ModelBindingContext bindingContext)
52
+        {
53
+            var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
54
+            var data = messageTypeResult.FirstValue;
55
+            var res = FromString(data);
56
+            if (res)
57
+            {
58
+                bindingContext.Result = ModelBindingResult.Success(res.Data);
59
+            }
60
+            else
61
+            {
62
+                bindingContext.Result = ModelBindingResult.Failed();
63
+            }
64
+            return Task.FromResult(0);
65
+        }
66
+    }
67
+
68
+    public class LuOrderByBinderProvider : IModelBinderProvider
69
+    {
70
+        public IModelBinder GetBinder(ModelBinderProviderContext context)
71
+        {
72
+            if (context.Metadata.ModelType == typeof(LuOrderByDbo))
73
+            {
74
+                return new LuOrderByBinder();
75
+            }
76
+            return null;
77
+        }
78
+    }
79
+}

+ 9
- 0
Luticate2.Utils/Dbo/OrderBy/LuOrderByDbo.cs View File

@@ -0,0 +1,9 @@
1
+using System.Collections.Generic;
2
+
3
+namespace Luticate2.Utils.Dbo.OrderBy
4
+{
5
+    public class LuOrderByDbo
6
+    {
7
+        public IList<LuOrderByFieldDbo> Fields { get; set; }
8
+    }
9
+}

+ 9
- 0
Luticate2.Utils/Dbo/OrderBy/LuOrderByFieldDbo.cs View File

@@ -0,0 +1,9 @@
1
+namespace Luticate2.Utils.Dbo.OrderBy
2
+{
3
+    public class LuOrderByFieldDbo
4
+    {
5
+        public string Name { get; set; }
6
+
7
+        public bool Asc { get; set; }
8
+    }
9
+}

Luticate2.Utils/Dbo/LuResult.cs → Luticate2.Utils/Dbo/Result/LuResult.cs View File


Luticate2.Utils/Dbo/LuStatus.cs → Luticate2.Utils/Dbo/Result/LuStatus.cs View File


+ 123
- 0
Test/Utils/Binders/LuOrderByBinderTest.cs View File

@@ -0,0 +1,123 @@
1
+using System.Linq;
2
+using Luticate2.Utils.Dbo;
3
+using Luticate2.Utils.Dbo.OrderBy;
4
+using Xunit;
5
+
6
+namespace Test.Utils.Binders
7
+{
8
+    public class LuOrderByBinderTest
9
+    {
10
+        [Fact]
11
+        public void Test1()
12
+        {
13
+            var res = LuOrderByBinder.FromString("");
14
+            Assert.NotNull(res);
15
+            Assert.Equal(LuStatus.Success, res.Status);
16
+            var dbo = res.Data;
17
+            Assert.NotNull(dbo);
18
+            Assert.NotNull(dbo.Fields);
19
+            Assert.Equal(0, dbo.Fields.Count);
20
+        }
21
+
22
+        [Fact]
23
+        public void Test2()
24
+        {
25
+            var res = LuOrderByBinder.FromString("test");
26
+            Assert.NotNull(res);
27
+            Assert.Equal(LuStatus.Success, res.Status);
28
+            var dbo = res.Data;
29
+            Assert.NotNull(dbo);
30
+            Assert.NotNull(dbo.Fields);
31
+            Assert.Equal(1, dbo.Fields.Count);
32
+
33
+            var field = dbo.Fields[0];
34
+            Assert.NotNull(field);
35
+            Assert.True(field.Asc);
36
+            Assert.Equal("test", field.Name);
37
+        }
38
+
39
+        [Fact]
40
+        public void Test3()
41
+        {
42
+            var res = LuOrderByBinder.FromString("test,test2");
43
+            Assert.NotNull(res);
44
+            Assert.Equal(LuStatus.Success, res.Status);
45
+            var dbo = res.Data;
46
+            Assert.NotNull(dbo);
47
+            Assert.NotNull(dbo.Fields);
48
+            Assert.Equal(2, dbo.Fields.Count);
49
+
50
+            var field = dbo.Fields[0];
51
+            Assert.NotNull(field);
52
+            Assert.True(field.Asc);
53
+            Assert.Equal("test", field.Name);
54
+
55
+            field = dbo.Fields[1];
56
+            Assert.NotNull(field);
57
+            Assert.True(field.Asc);
58
+            Assert.Equal("test2", field.Name);
59
+        }
60
+
61
+        [Fact]
62
+        public void Test4()
63
+        {
64
+            var res = LuOrderByBinder.FromString("test:DESC,test2:ASC");
65
+            Assert.NotNull(res);
66
+            Assert.Equal(LuStatus.Success, res.Status);
67
+            var dbo = res.Data;
68
+            Assert.NotNull(dbo);
69
+            Assert.NotNull(dbo.Fields);
70
+            Assert.Equal(2, dbo.Fields.Count);
71
+
72
+            var field = dbo.Fields[0];
73
+            Assert.NotNull(field);
74
+            Assert.False(field.Asc);
75
+            Assert.Equal("test", field.Name);
76
+
77
+            field = dbo.Fields[1];
78
+            Assert.NotNull(field);
79
+            Assert.True(field.Asc);
80
+            Assert.Equal("test2", field.Name);
81
+        }
82
+
83
+        [Fact]
84
+        public void Test5()
85
+        {
86
+            var res = LuOrderByBinder.FromString(",test2:ASC");
87
+            Assert.NotNull(res);
88
+            Assert.Equal(LuStatus.InputError, res.Status);
89
+        }
90
+
91
+        [Fact]
92
+        public void Test6()
93
+        {
94
+            var res = LuOrderByBinder.FromString("test2:ASC,");
95
+            Assert.NotNull(res);
96
+            Assert.Equal(LuStatus.InputError, res.Status);
97
+        }
98
+
99
+        [Fact]
100
+        public void Test7()
101
+        {
102
+            var res = LuOrderByBinder.FromString(",");
103
+            Assert.NotNull(res);
104
+            Assert.Equal(LuStatus.InputError, res.Status);
105
+        }
106
+
107
+        [Fact]
108
+        public void Test8()
109
+        {
110
+            var res = LuOrderByBinder.FromString("test:firstOrder");
111
+            Assert.NotNull(res);
112
+            Assert.Equal(LuStatus.InputError, res.Status);
113
+        }
114
+
115
+        [Fact]
116
+        public void Test9()
117
+        {
118
+            var res = LuOrderByBinder.FromString("test:42:ASC");
119
+            Assert.NotNull(res);
120
+            Assert.Equal(LuStatus.InputError, res.Status);
121
+        }
122
+    }
123
+}

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

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

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

@@ -1,4 +1,5 @@
1 1
 using Luticate2.Utils.Controllers;
2
+using Luticate2.Utils.Dbo.OrderBy;
2 3
 using Microsoft.AspNetCore.Mvc;
3 4
 using Test.Utils.Dbo.PkGuid;
4 5
 using WebTest.Business;
@@ -20,10 +21,11 @@ namespace WebTest.Controllers
20 21
             return Handle(_busines.GetSingleById(id));
21 22
         }
22 23
 
23
-//        [Route("[controller]")]
24
-//        public PkGuidDbo Get()
25
-//        {
24
+        [Route("[controller]")]
25
+        public PkGuidDbo Get(LuOrderByDbo orderBy)
26
+        {
27
+            return new PkGuidDbo();
26 28
 //            return Handle(_busines.GetMultiple());
27
-//        }
29
+        }
28 30
     }
29 31
 }

+ 1
- 3
WebTest/Startup.cs View File

@@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging;
8 8
 using Test.Utils;
9 9
 using Test.Utils.DataAccess;
10 10
 using WebTest.Business;
11
-using WebTest.Controllers;
12 11
 
13 12
 namespace WebTest
14 13
 {
@@ -43,11 +42,10 @@ namespace WebTest
43 42
 
44 43
             services.AddSingleton<PkGuidBusiness>();
45 44
             services.AddSingleton<LuUtilsPkGuidDataAccess>();
46
-            services.AddDbContext<LuUtilsDbContext>(options => options.UseNpgsql(Tests.CONNECTION_STRING));
45
+            services.AddDbContext<LuUtilsDbContext>(options => options.UseNpgsql(Tests.ConnectionString));
47 46
 
48 47
             services.AddMvc()
49 48
                 .AddLuticate();
50
-
51 49
         }
52 50
 
53 51
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline

Loading…
Cancel
Save