Пример #1
0
        public async Task <IActionResult> ProductGetByCategoryID(string languageId, [FromQuery] ProductGuiGetPagingRequest request)
        {
            var product = await _productService.ProductGetByCategoryID(languageId, request);

            return(Ok(product));
        }
Пример #2
0
        public async Task <PagedResult <ProductView> > ProductGetByCategoryID(string languageId, ProductGuiGetPagingRequest request)
        {
            //SELECT DATA
            var query = from p in _context.Products
                        join pt in _context.ProductTranslations on p.Id equals pt.ProductId
                        join pic in _context.ProductCategories on p.Id equals pic.ProductId
                        join c in _context.Categories on pic.CategoryId equals c.Id
                        where pt.LanguageId == languageId
                        select new { p, pt, pic };

            //FILTER
            if (request.CategoryId.HasValue && request.CategoryId.Value > 0)
            {
                query = query.Where(p => p.pic.CategoryId == request.CategoryId);
            }
            //PAGING
            int totalRow = await query.CountAsync();

            var data = await query.Skip((request.PageIndex - 1) *request.PageSize)
                       .Take(request.PageSize)
                       .Select(x => new ProductView()
            {
                Id             = x.p.Id,
                Name           = x.pt.Name,
                DateCreated    = x.p.DateCreated,
                Description    = x.pt.ShortDescription,
                Details        = x.pt.FullDescription,
                LanguageId     = x.pt.LanguageId,
                OriginalPrice  = x.p.OriginalPrice,
                Price          = x.p.Price,
                SeoAlias       = x.pt.SeoAlias,
                SeoDescription = x.pt.SeoDescription,
                SeoTitle       = x.pt.SeoTitle,
                Stock          = x.p.Stock,
                ViewCount      = x.p.ViewCount
            }).ToListAsync();

            //SELECT
            var pagedResult = new PagedResult <ProductView>()
            {
                TotalRecords = totalRow,
                PageSize     = request.PageSize,
                PageIndex    = request.PageIndex,
                Items        = data
            };

            return(pagedResult);
        }