//Lấy danh sách các sản phẩm có danh mục public async Task <PagedResult <ProductViewModel> > GetAllPagingByCategory(GetProductByCategoryPagingRequest request) { //1. Lấy ra các bảng trong cơ sở dữ liệu var query = from p in _context.Products join im in _context.ProductImages on p.Id equals im.ProductId join pic in _context.ProductInCategories on p.Id equals pic.ProductId join c in _context.Categories on pic.CategoryId equals c.Id where im.IsDefault == true select new { p, im, pic }; //2. Tim kiem san pham if (request.CategoryId.HasValue && request.CategoryId.Value > 0) { query = query.Where(p => p.pic.CategoryId == request.CategoryId); } if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.p.Name_Phone.Contains(request.Keyword)); } //3. Phan trang san pham int totalRow = await query.CountAsync(); var data = await query.Skip((request.PageIndex - 1) *request.PageSize) .Take(request.PageSize) .Select(x => new ProductViewModel() { Id = x.p.Id, Name_Phone = x.p.Name_Phone, DateCreated = x.p.DateCreated, Description = x.p.Description, Details = x.p.Details, Price = x.p.Price, Promotion_Price = x.p.Promotion_Price, ImagePath = _storageService.GetFileUrl(x.im.ImagePath), categoryId = x.pic.CategoryId }).ToListAsync(); var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageSize = request.PageSize, PageIndex = request.PageIndex, Items = data }; return(pagedResult); }
public async Task <PagedResult <ProductViewModel> > GetProductPagings(GetProductByCategoryPagingRequest request) { var client = _httpClientFactory.CreateClient(); client.BaseAddress = new Uri(_configuration["BaseAddress"]); var response = await client.GetAsync($"/api/products/category?pageIndex=" + $"{request.PageIndex}&pageSize={request.PageSize}&CategoryId={request.CategoryId}&keyword={request.Keyword}"); var body = await response.Content.ReadAsStringAsync(); var product = JsonConvert.DeserializeObject <PagedResult <ProductViewModel> >(body); return(product); }
public async Task <IActionResult> Details(int id) { List <CategoryViewModel> categoryList = await _categoryApiClient.GetList(); ViewBag.CategoryList = categoryList; List <ProductImageViewModel> imageList = await _productApiClient.GetListImage(id); ViewBag.ImageList = imageList; List <CartItems> cart = SessionExtensions.Get <List <CartItems> >(HttpContext.Session, "cart"); var list = new List <CartItems>(); if (cart != null) { list = cart; } ViewBag.Cart = list; decimal total = 0; foreach (var item in list) { total += (item.Price * item.Quantity); } ViewBag.total = total; ViewBag.productId = id; var result = await _productApiClient.GetProductId(id); var request = new GetProductByCategoryPagingRequest() { CategoryId = result.categoryId, PageIndex = 1, PageSize = 6 }; var data = await _productApiClient.GetProductPagings(request); ViewBag.data = data; ViewBag.Url = _configuration["BaseAddress"]; return(View(result)); }
public async Task <IActionResult> Index(string keyword, int pageIndex = 1, int pageSize = 10) { var request = new GetProductByCategoryPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize }; List <CategoryViewModel> categoryList = await _categoryApiClient.GetList(); ViewBag.CategoryList = categoryList; List <CartItems> cart = SessionExtensions.Get <List <CartItems> >(HttpContext.Session, "cart"); var list = new List <CartItems>(); if (cart != null) { list = cart; } ViewBag.Cart = list; decimal total = 0; foreach (var item in list) { total += (item.Price * item.Quantity); } ViewBag.total = total; var data = await _productApiClient.GetProductPagings(request); ViewBag.Url = _configuration["BaseAddress"]; ViewBag.Keyword = keyword; return(View(data)); }
public async Task <IActionResult> GetAllPagingByCategory([FromQuery] GetProductByCategoryPagingRequest request) { var products = await _productService.GetAllPagingByCategory(request); return(Ok(products)); }