Browse Source

added pagination request dbo

tags/v0.1.0
Robin Thoni 8 years ago
parent
commit
551a508dbb

+ 3
- 3
Luticate2.Utils/Controllers/LuCrudController.cs View File

@@ -2,6 +2,7 @@
2 2
 using Luticate2.Utils.Dbo.Basic;
3 3
 using Luticate2.Utils.Dbo.Filter;
4 4
 using Luticate2.Utils.Dbo.OrderBy;
5
+using Luticate2.Utils.Dbo.PaginatedRequest;
5 6
 using Luticate2.Utils.Interfaces;
6 7
 using Microsoft.AspNetCore.Mvc;
7 8
 
@@ -29,10 +30,9 @@ namespace Luticate2.Utils.Controllers
29 30
 
30 31
         [HttpGet]
31 32
         [Route("[controller]")]
32
-        public LuApiWrapperDbo<LuPaginatedDbo<TDboRead>> GetMultiple([Required]LuOrderByDbo orderBy, LuFilterDbo filter,
33
-            int page = 0, int perPage = int.MaxValue)
33
+        public LuApiWrapperDbo<LuPaginatedDbo<TDboRead>> GetMultiple([Required]LuPaginatedRequestDbo request)
34 34
         {
35
-            return Handle(Busines.GetMultiple(orderBy, filter, page, perPage));
35
+            return Handle(Busines.GetMultiple(request.OrderBy, request.Filter, request.Page, request.PerPage));
36 36
         }
37 37
 
38 38
         [HttpPost]

+ 2
- 0
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

@@ -4,6 +4,7 @@ using Luticate2.Utils.DataAccess.Npgsql;
4 4
 using Luticate2.Utils.Dbo.Basic;
5 5
 using Luticate2.Utils.Dbo.Filter;
6 6
 using Luticate2.Utils.Dbo.OrderBy;
7
+using Luticate2.Utils.Dbo.PaginatedRequest;
7 8
 using Luticate2.Utils.Dbo.Result;
8 9
 using Luticate2.Utils.Hubs;
9 10
 using Luticate2.Utils.Middlewares;
@@ -59,6 +60,7 @@ namespace Luticate2.Utils.Controllers
59 60
                 {
60 61
                     options.ModelBinderProviders.Insert(0, new LuOrderByBinderProvider());
61 62
                     options.ModelBinderProviders.Insert(0, new LuFilterBinderProvider());
63
+                    options.ModelBinderProviders.Insert(0, new LuPaginatedRequestBinderProvider());
62 64
                     options.Filters.Add(typeof(LuModelStateFilter));
63 65
                 });
64 66
             return builder;

+ 4
- 1
Luticate2.Utils/Dbo/Filter/LuFilterDbo.cs View File

@@ -1,7 +1,10 @@
1
-namespace Luticate2.Utils.Dbo.Filter
1
+using System.ComponentModel.DataAnnotations;
2
+
3
+namespace Luticate2.Utils.Dbo.Filter
2 4
 {
3 5
     public class LuFilterDbo
4 6
     {
7
+        [Required]
5 8
         public string Query { get; set; }
6 9
     }
7 10
 }

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

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

+ 55
- 0
Luticate2.Utils/Dbo/PaginatedRequest/LuPaginatedRequestBinder.cs View File

@@ -0,0 +1,55 @@
1
+using System.Threading.Tasks;
2
+using Luticate2.Utils.Controllers;
3
+using Luticate2.Utils.Dbo.Filter;
4
+using Luticate2.Utils.Dbo.OrderBy;
5
+using Microsoft.AspNetCore.Mvc.ModelBinding;
6
+
7
+namespace Luticate2.Utils.Dbo.PaginatedRequest
8
+{
9
+    public class LuPaginatedRequestBinder : IModelBinder
10
+    {
11
+        public Task BindModelAsync(ModelBindingContext bindingContext)
12
+        {
13
+            var filterValue = bindingContext.ValueProvider.GetValue("filter");
14
+            var orderByValue = bindingContext.ValueProvider.GetValue("orderBy");
15
+            var pageValue = bindingContext.ValueProvider.GetValue("page");
16
+            var perPageValue = bindingContext.ValueProvider.GetValue("perPage");
17
+
18
+            var filter = LuFilterBinder.FromString(filterValue.FirstValue);
19
+            if (!filter)
20
+            {
21
+                throw new LuResultException(filter.To<object>());
22
+            }
23
+
24
+            var orderBy = LuOrderByBinder.FromString(orderByValue.FirstValue);
25
+            if (!orderBy)
26
+            {
27
+                throw new LuResultException(orderBy.To<object>());
28
+            }
29
+
30
+            var dbo = new LuPaginatedRequestDbo
31
+            {
32
+                Filter = filter.Data,
33
+                OrderBy = orderBy.Data,
34
+                Page = pageValue.FirstValue == null ? 0 : int.Parse(pageValue.FirstValue),
35
+                PerPage = perPageValue.FirstValue == null ? int.MaxValue : int.Parse(perPageValue.FirstValue)
36
+            };
37
+
38
+            bindingContext.Result = ModelBindingResult.Success(dbo);
39
+
40
+            return Task.FromResult(0);
41
+        }
42
+    }
43
+
44
+    public class LuPaginatedRequestBinderProvider : IModelBinderProvider
45
+    {
46
+        public IModelBinder GetBinder(ModelBinderProviderContext context)
47
+        {
48
+            if (context.Metadata.ModelType == typeof(LuPaginatedRequestDbo))
49
+            {
50
+                return new LuPaginatedRequestBinder();
51
+            }
52
+            return null;
53
+        }
54
+    }
55
+}

+ 27
- 0
Luticate2.Utils/Dbo/PaginatedRequest/LuPaginatedRequestDbo.cs View File

@@ -0,0 +1,27 @@
1
+using System.ComponentModel.DataAnnotations;
2
+using Luticate2.Utils.Dbo.Filter;
3
+using Luticate2.Utils.Dbo.OrderBy;
4
+
5
+namespace Luticate2.Utils.Dbo.PaginatedRequest
6
+{
7
+    public class LuPaginatedRequestDbo<TFilterDbo> where TFilterDbo : LuFilterDbo
8
+    {
9
+        [Required]
10
+        public TFilterDbo Filter { get; set; }
11
+
12
+        [Required]
13
+        public LuOrderByDbo OrderBy { get; set; }
14
+
15
+        [Required]
16
+        [Range(0, int.MaxValue)]
17
+        public int Page { get; set; }
18
+
19
+        [Required]
20
+        [Range(1, int.MaxValue)]
21
+        public int PerPage { get; set; }
22
+    }
23
+
24
+    public class LuPaginatedRequestDbo : LuPaginatedRequestDbo<LuFilterDbo>
25
+    {
26
+    }
27
+}

Loading…
Cancel
Save