public async Task <PagingInformation <ConsolidatedProductEntity> > GetConsolidatedPagedAsync( PagingSortingParams options, Guid?categoryId = null) { logger.LogDebug( $"Attempting to retrieve products consolidated from database: Page: {options.Page}, items per page: {options.ItemsPerPage}, sort by: {options.SortBy}, sort direction: {options.SortDirection}."); var totalProducts = await dbContext.Products.CountAsync(); var totalPages = (int)Math.Ceiling(totalProducts / (double)options.ItemsPerPage); var query = dbContext.ProductsConsolidated .Where(x => x.Language == i18nService.GetCurrentLanguage().Code&& x.Currency == i18nService.GetCurrentCurrency().Code); if (categoryId != null) { query = query.Where(x => x.CategoryId == categoryId); } query = query.PagedAndSorted(options); var products = await query.ToListAsync(); logger.LogInformation($"Retrieved {products.Count} products from the database."); return(new PagingInformation <ConsolidatedProductEntity> { PageCount = totalPages, CurrentPage = options.Page, ItemsPerPage = options.ItemsPerPage, TotalItems = totalProducts, Items = products }); }
public async Task <IActionResult> GetPaged([FromQuery] ApiV1ProductPagingSortingFilteringRequestModel model) { logger.LogDebug($"Attempting to get paged products: Page: {model.Page}, items per page: {model.ItemsPerPage}."); if (ModelState.IsValid) { // Check if the requested language exists if (!i18nService.SupportedLanguages.Any(x => x.Code == model.LanguageCode)) { return(BadRequest(new ApiV1ErrorResponseModel("The requested language does not exist."))); } var pagingSortingOptions = new PagingSortingParams { ItemsPerPage = model.ItemsPerPage, Page = model.Page, SortBy = model.SortBy, SortDirection = model.SortDirection }; var filterOptions = new FilterParams { FilterBy = model.FilterBy, FilterQuery = model.FilterQuery, FilterLanguage = model.LanguageCode }; var productPagingInformation = await productService.GetPagedAsync(pagingSortingOptions, filterOptions, model.FilterByCategory); var productPagingInformationResponse = new PagingInformation <ApiV1ProductReponseModel> { CurrentPage = productPagingInformation.CurrentPage, Items = productPagingInformation.Items.Select(x => x.ToApiV1ProductResponseModel()).ToList(), ItemsPerPage = productPagingInformation.ItemsPerPage, PageCount = productPagingInformation.PageCount, TotalItems = productPagingInformation.TotalItems }; logger.LogInformation( $"Received {productPagingInformationResponse.Items.Count} products from the database."); return(Json(productPagingInformationResponse)); } else { logger.LogWarning($"Error while performing paged request. Validation failed."); return(BadRequest(ModelState.ToApiV1ErrorResponseModel())); } }
/// <summary> /// Applies the specified sorting and paging options to the IQueryable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">Source queryable</param> /// <param name="options">Instance of <see cref="PagingSortingParams"/></param> /// <returns>Modified source queryable</returns> public static IQueryable <T> PagedAndSorted <T>(this IQueryable <T> source, PagingSortingParams options) { if (options == null) { return(source); } // Sorting source = source.OrderBy($"{options.SortBy} {options.SortDirection.ToLower()}"); // Paging source = source .Skip((options.Page - 1) * options.ItemsPerPage) .Take(options.ItemsPerPage); return(source); }
public async Task <PagingInformation <ProductEntity> > GetPagedAsync(PagingSortingParams pagingSortingOptions, FilterParams filterParams, Guid?filterCategoryId = null) { logger.LogDebug( $"Attempting to retrieve products from database: Page: {pagingSortingOptions.Page}, items per page: {pagingSortingOptions.ItemsPerPage}, sort by: {pagingSortingOptions.SortBy}, sort direction: {pagingSortingOptions.SortDirection}."); var productsQuery = dbContext.Products .Include(x => x.Image) .Include(x => x.Prices) .Include(x => x.Translations) as IQueryable <ProductEntity>; if (filterCategoryId != null) { productsQuery = productsQuery.Where(x => x.CategoryId == filterCategoryId); } productsQuery = productsQuery.ProductsAdvancedFiltered(filterParams); var totalProducts = await productsQuery.CountAsync(); var products = await productsQuery.PagedAndSorted(pagingSortingOptions) .ToListAsync(); var totalPages = (int)Math.Ceiling(totalProducts / (double)pagingSortingOptions.ItemsPerPage); logger.LogInformation($"Retrieved {products.Count} products from the database."); return(new PagingInformation <ProductEntity> { PageCount = totalPages, CurrentPage = pagingSortingOptions.Page, ItemsPerPage = pagingSortingOptions.ItemsPerPage, TotalItems = totalProducts, Items = products }); }