//https: //localhost:5001/api/CarAdministration?page=1&itemsPerPage=10&sortBy[]=name&sortDesc[]=false&mustSort=false&multiSort=false
        public CarAdministratorDataTableModel Get([FromQuery] int page, [FromQuery] int itemsPerPage,
                                                  [FromQuery(Name = "sortBy[]")] List <string> sortBy, [FromQuery(Name = "sortDesc[]")] List <bool> sortDesc)
        {
            var carAdministratorFilter = new CarAdministratorFilter
            {
                ItemsPerPage = itemsPerPage,
                Page         = page,
                SortFilters  = sortBy.Select((s, i) => new SortFilter {
                    By = s, Descending = sortDesc[i]
                })
            };

            logger.LogInformation($"{nameof(sortBy)} {string.Concat(sortBy)}");
            logger.LogInformation($"{nameof(sortDesc)} {string.Concat(sortDesc)}");
            return(database.GetCarAdministrators(carAdministratorFilter));
        }
Exemple #2
0
        public CarAdministratorDataTableModel GetCarAdministrators(CarAdministratorFilter filter)
        {
            // todo additional filtering function
            // could be passed as argument
            //Func<IEnumerable<CarAdministrator>, IEnumerable<CarAdministrator>> additionalFunc = (query) =>
            //{
            //    query = query.Where(i => i.Name != "Fero");
            //    return query;
            //};

            var resultQuery = carAdministrators;

            foreach (var sortFilter in filter.SortFilters)
            {
                resultQuery = sortFilter.By switch
                {
                    "name" => sortFilter.Descending
                                                ? resultQuery.OrderByDescending(i => i.Name)
                                                : resultQuery.OrderBy(i => i.Name),
                    "note" => sortFilter.Descending
                                                ? resultQuery.OrderByDescending(i => i.Note)
                                                : resultQuery.OrderBy(i => i.Note),
                    _ => throw new ArgumentOutOfRangeException()
                };
            }
            // todo additional filtering function
            // resultQuery = additionalFunc(resultQuery);

            var itemsToSkip = (filter.Page - 1) * filter.ItemsPerPage;

            resultQuery = resultQuery.Skip(itemsToSkip).Take(filter.ItemsPerPage);

            return(new CarAdministratorDataTableModel {
                Items = resultQuery, TotalItems = carAdministrators.Count()
            });
        }