Пример #1
0
        public async Task <PaginationResult <ProductModel> > GetProductsAsync(ProductQuery query)
        {
            var productQuery = query.GenerateQuery();

            var count = await _unitOfWork.ProductRepository.GetAllAsQueryable()
                        .Where(productQuery)
                        .CountAsync();

            var result = await _unitOfWork.ProductRepository.GetAllAsQueryable()
                         .Where(productQuery)
                         .Skip((query.Page - 1) * query.PageSize)
                         .Take(query.PageSize)
                         .Include(product => product.Images)
                         .Include(product => product.Category)
                         .Include(product => product.ProductVariants)
                         .Select(product => new ProductModel()
            {
                Id           = product.Id,
                CategoryId   = product.CategoryId,
                CategoryName = product.Category.Name,
                Slug         = product.Slug,
                Title        = product.Title,
                Description  = product.Description,
                Vendor       = product.Vendor,
                Price        = product.ProductVariants.Min(variation => variation.Price),
                Quantity     = product.ProductVariants.Sum(variant => variant.Quantity),
                Images       = product.Images.Select(img => img.Name).ToList()
            }).ToListAsync();

            return(new PaginationResult <ProductModel>(count, query.Page, query.PageSize, result));
        }