public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            // get data from query by values privider
            var filterNameValue     = bindingContext.ValueProvider.GetValue("name");
            var filterLanguageValue = bindingContext.ValueProvider.GetValue("languageId");
            var filterDateFromValue = bindingContext.ValueProvider.GetValue("dateFrom");
            var filterDateToValue   = bindingContext.ValueProvider.GetValue("dateTo");
            var sortOrderValue      = bindingContext.ValueProvider.GetValue("sortOrder");
            var sortPropertyValue   = bindingContext.ValueProvider.GetValue("sortProperty");
            var pageIndexValue      = bindingContext.ValueProvider.GetValue("pageIndex");
            var pageSizeValue       = bindingContext.ValueProvider.GetValue("pageSize");

            // get values
            string filterName = filterNameValue.FirstValue;

            int.TryParse(filterLanguageValue.FirstValue, out int filterLanguage);
            DateTime.TryParse(filterDateFromValue.FirstValue, out DateTime filterDateFrom);
            filterDateFrom = filterDateFrom.ToUniversalTime().AbsoluteStart();
            DateTime.TryParse(filterDateToValue.FirstValue, out DateTime filterDateTo);
            filterDateTo = filterDateTo == DateTime.MinValue ? DateTime.MaxValue : filterDateTo.ToUniversalTime().AbsoluteEnd();
            string sortProperty = sortPropertyValue.FirstValue ?? "Name";
            string sortOrder    = sortOrderValue.FirstValue ?? "Desc";

            int pageIndex = int.TryParse(pageIndexValue.FirstValue, out pageIndex) ? pageIndex : 1;
            int pageSize  = int.TryParse(pageSizeValue.FirstValue, out pageSize) ? pageSize : 5;

            //fill properties
            GroupsListOptionsModel model = (GroupsListOptionsModel)bindingContext.Model ?? new GroupsListOptionsModel();

            model.Name         = filterName;
            model.LanguageId   = filterLanguage;
            model.DateFrom     = filterDateFrom;
            model.DateTo       = filterDateTo;
            model.SortProperty = sortProperty;
            model.SortOrder    = sortOrder;
            model.PageIndex    = pageIndex;
            model.PageSize     = pageSize;

            // set binding result
            bindingContext.Result = ModelBindingResult.Success(model);
            return(Task.CompletedTask);
        }
        public virtual IQueryOptions Map(GroupsListOptionsModel optionsModel)
        {
            List <QueryFilter> filters = new List <QueryFilter>();

            if (optionsModel.Name != null)
            {
                filters.Add(new QueryFilterElement("Name", optionsModel.Name, FilterElementOperation.Contains, new QueryFilterAdditionalOptions()
                {
                    IgnoreCase = true
                }));
            }
            if (optionsModel.LanguageId != 0)
            {
                filters.Add(new QueryFilterElement("Language.Id", optionsModel.LanguageId, FilterElementOperation.Equal));
            }
            if (optionsModel.DateFrom != DateTime.MinValue)
            {
                filters.Add(new QueryFilterElement("Date", optionsModel.DateFrom, FilterElementOperation.GreaterThanOrEqual));
            }
            if (optionsModel.DateTo != DateTime.MaxValue)
            {
                filters.Add(new QueryFilterElement("Date", optionsModel.DateTo, FilterElementOperation.LessThanOrEqual));
            }

            List <QuerySorter> sorters   = new List <QuerySorter>();
            SortOrder          sortOrder = Enum.Parse <SortOrder>(optionsModel.SortOrder, true);

            sorters.Add(new QuerySorter(optionsModel.SortProperty, sortOrder));

            List <QueryIncluder> includers = new List <QueryIncluder>();

            includers.Add(new QueryIncluder("Language"));

            int            take      = optionsModel.PageSize;
            int            skip      = optionsModel.PageSize * (Math.Max(optionsModel.PageIndex, 1) - 1);
            QueryPagenator pagenator = new QueryPagenator(take, skip);

            IQueryOptions queryOptions = new QueryOptions(
                filters: filters,
                sorters: sorters,
                includers: includers,
                pagenator: pagenator);

            return(queryOptions);
        }
示例#3
0
        public async Task <IActionResult> Count([FromQuery] GroupsListOptionsModel groupsListOptionsModel)
        {
            int pageTotal = await _groupManager.CountAsync(_queryOptionsAdapter.Map(groupsListOptionsModel));

            return(Ok(pageTotal));
        }
示例#4
0
        public async Task <IActionResult> Index([FromQuery] GroupsListOptionsModel groupsListOptionsModel)
        {
            IEnumerable <GroupDto> groupsDto = await _groupManager.GetListAsync(_queryOptionsAdapter.Map(groupsListOptionsModel));

            return(Ok(_dataAdapter.Map <IEnumerable <GroupViewModel> >(groupsDto)));
        }