using System.Threading.Tasks; using Luticate2.Utils.Controllers; using Luticate2.Utils.Dbo.Filter; using Luticate2.Utils.Dbo.OrderBy; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Luticate2.Utils.Dbo.PaginatedRequest { public class LuPaginatedRequestBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { var filterValue = bindingContext.ValueProvider.GetValue("filter"); var orderByValue = bindingContext.ValueProvider.GetValue("orderBy"); var pageValue = bindingContext.ValueProvider.GetValue("page"); var perPageValue = bindingContext.ValueProvider.GetValue("perPage"); var filter = LuFilterBinder.FromString(filterValue.FirstValue); if (!filter) { throw new LuResultException(filter.To()); } var orderBy = LuOrderByBinder.FromString(orderByValue.FirstValue); if (!orderBy) { throw new LuResultException(orderBy.To()); } var dbo = new LuPaginatedRequestDbo { Filter = filter.Data, OrderBy = orderBy.Data, Page = pageValue.FirstValue == null ? 0 : int.Parse(pageValue.FirstValue), PerPage = perPageValue.FirstValue == null ? int.MaxValue : int.Parse(perPageValue.FirstValue) }; bindingContext.Result = ModelBindingResult.Success(dbo); return Task.FromResult(0); } } public class LuPaginatedRequestBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context.Metadata.ModelType == typeof(LuPaginatedRequestDbo)) { return new LuPaginatedRequestBinder(); } return null; } } }