|
@@ -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
|
+}
|