public async Task <IActionResult> Index(string keyword, int?categoryId, int pageIndex = 1, int pageSize = 10)
        {
            var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId);

            var request = new GetManageProductPaggingRequest()
            {
                KeyWord    = keyword,
                pageIndex  = pageIndex,
                pageSize   = pageSize,
                LanguageId = languageId,
                CategoryId = categoryId
            };
            var data = await _productApiClient.GetPagings(request);

            ViewBag.Keyword = keyword;

            var categories = await _categoryApiClient.GetAll(languageId);

            ViewBag.Categories = categories.Select(x => new SelectListItem()
            {
                Text     = x.Name,
                Value    = x.Id.ToString(),
                Selected = categoryId.HasValue && categoryId.Value == x.Id
            });

            if (TempData["result"] != null)
            {
                ViewBag.SuccessMsg = TempData["result"];
            }
            return(View(data));
        }
        public async Task <PagedResult <ProductVm> > GetAllPading(GetManageProductPaggingRequest request)
        {
            //1. Select Join
            var query = from p in _context.Products
                        join pt in _context.ProductTranslations on p.Id equals pt.ProductId
                        join pic in _context.ProductInCategories on p.Id equals pic.ProductId into ppic
                        from pic in ppic.DefaultIfEmpty()
                        join c in _context.Categories on pic.CategoryId equals c.Id into picc
                        from c in picc.DefaultIfEmpty()
                        join pi in _context.ProductImages on p.Id equals pi.ProductId into ppi
                        from pi in ppi.DefaultIfEmpty()
                        where pt.LanguageId == request.LanguageId && pi.IsDefault == true
                        select new { p, pt, pic, pi };

            //2. Filter
            if (!string.IsNullOrEmpty(request.KeyWord))
            {
                query = query.Where(p => p.pt.Name.Contains(request.KeyWord));
            }

            if (request.CategoryId != null && request.CategoryId != 0)
            {
                query = query.Where(p => p.pic.CategoryId == request.CategoryId);
            }
            //3. Paging
            int totalRow = await query.CountAsync();

            var data = await query.Skip((request.pageIndex - 1) *request.pageSize)
                       .Take(request.pageSize)
                       .Select(x => new ProductVm()
            {
                Id             = x.p.Id,
                Name           = x.pt.Name,
                DateCreated    = x.p.DateCreated,
                Description    = x.pt.Description,
                Details        = x.pt.Details,
                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,
                ThumbnailImage = x.pi.ImagePath
            }).ToListAsync();

            //4. Select and projection
            var pagedResult = new PagedResult <ProductVm>()
            {
                TotalRecords = totalRow,
                Items        = data,
                PageIndex    = request.pageIndex,
                PageSize     = request.pageSize
            };

            return(pagedResult);
        }
        public async Task <PagedResult <ProductVm> > GetPagings(GetManageProductPaggingRequest request)
        {
            var data = await GetAsync <PagedResult <ProductVm> >(
                $"/api/products/paging?pageIndex={request.pageIndex}" +
                $"&pageSize={request.pageSize}" +
                $"&keyword={request.KeyWord}&languageId={request.LanguageId}&categoryId={request.CategoryId}");

            return(data);
        }
Exemple #4
0
        public async Task <IActionResult> GetAllPaging([FromQuery] GetManageProductPaggingRequest request)
        {
            var products = await _poductService.GetAllPading(request);

            return(Ok(products));
        }