// biding từ query nếu để class thì thêm [formQuery] public async Task <IActionResult> Index(int?categoryId, string keyword, int pageIndex = 1, int pageSize = 6) { var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId); var request = new GetManageProductPagingRequest() { KeyWord = keyword, PageIndex = pageIndex, PageSize = pageSize, LanguageId = languageId, CategoryId = categoryId }; var data = await _productApiClient.GetProductPagings(request); ViewBag.Keyword = keyword; var categories = await _categoryApiClient.GetAll(languageId); //chuyền lên cho ViewBag.Categories ViewBag.Categories = categories.ResultObj.Select(x => new SelectListItem() // phải SelctListItem thì thẻ select nó mới hiểu chứ chuyền thẳng nó ko hiểu đâu { Text = x.Name, Value = x.Id.ToString(), // để nó dữ lại giá trị khi đã select Selected = categoryId.HasValue && categoryId.Value == x.Id // vì categoryId là object nên ta phải hasvalue vì nó có cả null }); if (TempData["result"] != null) { ViewBag.SuccessMsg = TempData["result"]; } return(View(data.ResultObj)); }
public async Task <IActionResult> Index(string keyword, int?categoryId, int page = 1, int pageSize = 5) { var currentLang = _httpContextAccessor.HttpContext.Session.GetString(SystemConstant.AppSettings.DefaultLanguageId); var request = new GetManageProductPagingRequest() { PageIndex = page, PageSize = pageSize, Keyword = keyword, LanguageId = currentLang, CategoryId = categoryId }; ViewBag.Keyword = keyword; var data = await _productApiClient.GetProductPaging(request); var categories = await _categoryApiClient.GetAll(currentLang); ViewBag.Categories = categories.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name, Selected = categoryId.HasValue && categoryId.Value == x.Id });; if (TempData["success"] != null) { ViewBag.SuccessMsg = TempData["success"]; } return(View(data.ResultObject)); }
public async Task <IActionResult> Index(string name, string keyword, int?categoryId, int pageIndex = 1, int pageSize = 8) { var request = new GetManageProductPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize, CategoryId = categoryId }; var data = await _productApiClient.GetPagings(request); ViewBag.Keyword = keyword; var categories = await _categoryApiClient.GetAll(); 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 <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest 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(x => x.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 ProductViewModel() { 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 //Categories = x.ct.Name }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageIndex = request.PageIndex, PageSize = request.PageSize, Items = data }; return(pagedResult); }
public async Task <IActionResult> Index(string keyword, string categoryId, int pageIndex = 1, int pageSize = 6) { var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId); var request = new GetManageProductPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize, CategoryId = categoryId }; var data = await _productApiClient.GetPagings(request); ViewBag.Keyword = keyword; var categories = await _categoryApiClient.GetAll(); ViewBag.Categories = categories.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString(), Selected = categoryId != null && categoryId == x.Id }); if (TempData["result"] != null) { ViewBag.SuccessMsg = TempData["result"]; } return(View(data)); }
public async Task <IActionResult> Index(string keyword, int?categoryId, int pageIndex = 1, int pageSize = 5) { var awd = User; var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId); var request = new GetManageProductPagingRequest() { 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"]; // } TempData["TotalProducts"] = data.TotalRecords; return(View(data)); }
public async Task <IActionResult> Index(string keyword, int?categoryId, int pageIndex = 1, int pageSize = 5) { var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId); //B2. Dựng request var request = new GetManageProductPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize, LanguageId = languageId, CategoryId = categoryId }; var data = await _productApiClient.GetPagings(request); //Controller sử dụng Service của Project AdminApp để gửi request, lấy về PagedResult<UserViewModel> tạo View ViewBag.Keyword = keyword; //Chuyển keyword ra view để tái hiện lại trên textbox Search 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 //keep Selected mỗi khi submit }); if (TempData["result"] != null) //Nhận TempData["result"] được redirect từ các method Create, Update, Delete { ViewBag.Msg = TempData["result"]; } if (data != null) { return(View(data)); } return(RedirectToAction("Error", "Home")); }
/// <summary> /// lấy ra các bản ghi, Search theo tên Product /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest request) { //lấy ra các bản ghi, Search theo tên product => JOIN với bảng Products để lấy trường Name //b1. 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 join c in _context.Categories on pic.CategoryId equals c.Id where pt.Name.Contains(request.Keyword) select new { p, pt, pic }; //b2. filter if (string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.Name.Contains(request.Keyword)); } if (request.CategoryIds.Count > 0) { query = query.Where(p => request.CategoryIds.Contains(p.pic.CategoryId)); } //b3. paging 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 = x.pt.Name, DateCreated = x.p.DateCreated, Description = x.pt.Description, Details = x.pt.Details, SeoDesciption = x.pt.SeoDesciption, SeoAlias = x.pt.SeoAlias, SeoTitle = x.pt.SeoTitle, LanguageId = x.pt.LanguageId, OriginalPrice = x.p.OriginalPrice, Price = x.p.Price, Stock = x.p.Stock, ViewCount = x.p.ViewCount, } ).ToListAsync(); //b4. select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, Items = data, PageSize = request.pageSize, PageIndex = request.pageIndex, Message = "Success", ResultCode = 1 }; return(pagedResult); }
public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest request) { //Buoc 1: Select join (left join) var query = from p in _context.Products join pd in _context.ProductDetails on p.Id equals pd.ProductId into ppd from pd in ppd.DefaultIfEmpty() 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.ProductId equals c.Id into picc from c in picc.DefaultIfEmpty() join pi in _context.ProductImages on c.Id equals pi.ProductId into ppi from pi in ppi.DefaultIfEmpty() where pi.IsDefaut == true select new { p, pd, pic, pi }; //Buoc 2: Filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pd.Name.Contains(request.Keyword)); } if (request.CategoryId != null && request.CategoryId != 0) { query = query.Where(p => p.pic.CategoryId == request.CategoryId); } //Buoc 3: Paging int totalRow = await query.CountAsync(); var data = await query.Skip((request.PageIndex - 1) *request.PageSize) .Take(request.PageSize) .Select(x => new ProductViewModel() //x là kết quả tìm kiếm được { Id = x.p.Id, Name = x.pd.Name, //Name =x.pd == null? SystemConstants.ProductConstants.NA : x.pd.Name, DateCreated = x.p.DateCreated, Description = x.pd.Description, Details = x.pd.Details, //Details = x.pd == null ? SystemConstants.ProductConstants.NA : x.pd.Details, OriginalPrice = x.p.OriginalPrice, Price = x.p.Price, Stock = x.p.Stock, ViewCount = x.p.ViewCount, ThumbnailImage = x.pi.ImagePath }).ToListAsync(); //Buoc 4: Select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageIndex = request.PageIndex, PageSize = request.PageSize, Items = data }; return(pagedResult); }
//public async Task<List<ProductVm>> GetFeaturedProducts(string languageId, int take) //{ // var data = await GetListAsync<ProductVm>($"/api/products/featured/{languageId}/{take}"); // return data; //} //public async Task<List<ProductVm>> GetLatestProducts( int take) //{ // var data = await GetListAsync<ProductVm>($"/api/products/latest/{take}"); // return data; //} public async Task <PagedResult <ProductVm> > GetPagings(GetManageProductPagingRequest request) { var data = await GetAsync <PagedResult <ProductVm> >( $"/api/products/paging?pageIndex={request.PageIndex}" + $"&pageSize={request.PageSize}" + $"&keyword={request.Keyword}&categoryId={request.CategoryId}"); return(data); }
public async Task <ApiResult <PagedResult <ProductVm> > > GetProductPagings(GetManageProductPagingRequest request) { // chuyền vào đường dẫn url để lấy kết quả trên request và vào trong BaseApiClient thực hiện phương thức GetAsync để giải json và lấy ra PagedResult<ProductVm> var data = await GetAsync <ApiResult <PagedResult <ProductVm> > >( // nó sẽ đọc theo kiểu ApiResult nhe chứ không có nó thì đéo đọc được đâu $"/api/products/paging?pageIndex={request.PageIndex}" + $"&pageSize={request.PageSize}" + $"&keyword={request.KeyWord}&languageId={request.LanguageId}&CategoryId={request.CategoryId}"); return(data); }
public async Task <PagedResult <ProductViewModel> > GetPagings(GetManageProductPagingRequest request) { var result = await GetAsync <PagedResult <ProductViewModel> >( "/api/products/paging?pageIndex=" + $"{request.PageIndex}&pageSize={request.PageSize}" + $"&keyword={request.Keyword}" + $"&languageId={ request.LanguageId}" + $"&categoryId={request.CategoryId}"); return(result); }
public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest 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 productInCate //join c in _context.Categories on pic.CategoryId equals c.id where pt.LanguageId == request.LanguageId select new { p, pt }; // 2.Filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.Name.Contains(request.Keyword)); } //if (request.CategoryIds != null && request.CategoryIds.Count > 0) //{ // query = query.Where(p => request.CategoryIds.Contains(p.pic.CategoryId)); //} // 3.Paging 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 = 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 }).ToListAsync(); // 4.Select and Projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageIndex = request.PageIndex, PageSize = request.PageSize, Items = data }; return(pagedResult); }
public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest request) { 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 join c in _context.Categories on pic.CategoryId equals c.Id select new { p, pt, pic }; if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.Name.Contains(request.Keyword)); } //filter if (request.CategoryIds.Count > 0) { query = query.Where(p => request.CategoryIds.Contains(p.pic.CategoryId)); } //paging 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, Price = x.p.Price, OriginalPrice = x.p.OriginalPrice, Stock = x.p.Stock, ViewCount = x.p.ViewCount, DateCreated = x.p.DateCreated, Name = x.pt.Name, Description = x.pt.Description, Details = x.pt.Details, SeoDescription = x.pt.SeoDescription, SeoTitle = x.pt.SeoTitle, SeoAlias = x.pt.SeoAlias, LanguageId = x.pt.LanguageId } ).ToListAsync(); // select var pagedResult = new PagedResult <ProductViewModel>() { TotalRecord = totalRow, Items = data, }; return(pagedResult); }
public async Task <PagedResult <ProductVm> > GetAllPaging(GetManageProductPagingRequest request) { //1. Select join var query = from p in _context.products join pic in _context.productCategories on p.idCategory equals pic.idCategory into ppic from pic in ppic.DefaultIfEmpty() select new { p, pic }; //2. filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.p.productName.Contains(request.Keyword)); } if (request.CategoryId != null && request.CategoryId != null) { query = query.Where(p => p.pic.idCategory == 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() { idProduct = x.p.idProduct, productName = x.p.productName, idSize = x.p.idSize, idBrand = x.p.idBrand, idColor = x.p.idColor, idType = x.p.idType, idCategory = x.p.idCategory, price = x.p.price, salePrice = x.p.salePrice, detail = x.p.detail, dateAdded = DateTime.Now, photoReview = x.p.photoReview.Substring(22) }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductVm>() { TotalRecords = totalRow, PageSize = request.PageSize, PageIndex = request.PageIndex, Items = data }; return(pagedResult); }
public async Task <PagedResult <ProductViewModel> > GetProductPagings(GetManageProductPagingRequest request) { var data = await GetAsync <PagedResult <ProductViewModel> >( $"/api/products/paging?pageIndex={request.PageIndex}" + $"&pageSize={request.PageSize}" + $"&keyword={request.KeyWord}" + $"&languageId={request.LanguageId}" + $"&categoryId={request.CategoryId}" + $"&minPrice={request.MinPrice}" + $"&maxPrice={request.MaxPrice}"); return(data); }
public async Task <PagedResult <ProductVm> > GetAllPaging(GetManageProductPagingRequest request) { //1. Select join var query = from p in _context.products join pt in _context.productDetails on p.ProductId equals pt.ProductId join pic in _context.ProductInCategories on p.ProductId equals pic.ProductId into ppic from pic in ppic.DefaultIfEmpty() join c in _context.Categories on pic.idCategory equals c.idCategory into picc from c in picc.DefaultIfEmpty() where pt.LanguageId == request.LanguageId select new { p, pt, pic }; //2. filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.ProductName.Contains(request.Keyword)); } if (request.CategoryId != null && request.CategoryId != 0) { query = query.Where(p => p.pic.idCategory == 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.ProductId, ProductName = x.pt.ProductName, price = x.pt.price, salePrice = x.pt.salePrice, ViewCount = x.p.ViewCount, detail = x.pt.detail, LanguageId = x.pt.LanguageId, }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductVm>() { TotalRecords = totalRow, PageSize = request.PageSize, PageIndex = request.PageIndex, Items = data }; return(pagedResult); }
public async Task <IActionResult> Category(int id, string keyword, int pageIndex = 1, int pageSize = 6) { var languageId = CultureInfo.CurrentCulture.Name; var request = new GetManageProductPagingRequest() { KeyWord = keyword, PageIndex = pageIndex, PageSize = pageSize, LanguageId = languageId, CategoryId = id }; var data = await _productApiClient.GetProductPagings(request); return(View(data.ResultObj)); }
public async Task <IActionResult> Index(string keyword, int pageIndex = 1, int pageSize = 4) { var request = new GetManageProductPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize }; var data = await _productApiClient.GetAllPaging(request); ViewBag.Keyword = keyword; if (TempData["result"] != null) { ViewBag.SuccessMsg = TempData["result"]; } return(View(data.ResultObj)); }
public async Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest request) { //1. Select join var query = from p in _context.products join pt in _context.productDetails on p.idProduct equals pt.idProduct join pic in _context.ProductInCategories on p.idProduct equals pic.idProduct join c in _context.productCategories on pic.idCategory equals c.idCategory select new { p, pt, pic }; //2. filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.ProductName.Contains(request.Keyword)); } if (request.CategoryIds.Count > 0) { query = query.Where(p => request.CategoryIds.Contains(p.pic.idCategory)); } //3. Paging 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.idProduct, ProductName = x.pt.ProductName, price = x.pt.price, salePrice = x.pt.salePrice, ViewCount = x.p.ViewCount, detail = x.pt.detail, }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageIndex = request.PageIndex, PageSize = request.PageSize, Items = data }; return(pagedResult); }
public async Task <PagedResult <ProductViewModel> > GetAllByCategoryId(GetManageProductPagingRequest 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 join c in _context.Categories on pic.CategoryId equals c.Id select new { p, pt, pic }; //2. filter if (request.CatrgoryId.HasValue && request.CatrgoryId.Value > 0) { query = query.Where(p => p.pic.CategoryId == request.CatrgoryId); } //3. Paging 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 = 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 }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecord = totalRow, Items = data }; return(pagedResult); }
public async Task <IActionResult> Index(string keyword, int pageIndex = 1, int pageSize = 10) { var languageId = HttpContext.Session.GetString(SystemConstants.AppSettings.DefaultLanguageId); var request = new GetManageProductPagingRequest() { Keyword = keyword, PageIndex = pageIndex, PageSize = pageSize, LanguageId = languageId }; var data = await _productApiClient.GetPagings(request); ViewBag.Keyword = keyword; if (TempData["result"] != null) { ViewBag.SuccessMsg = TempData["result"]; } return(View(data)); }
// phân trang public async Task <ApiResult <PagedResult <ProductVm> > > GetAllPaging(GetManageProductPagingRequest request) { // bước 1 :select join ,, nhớ nhe giữ liệu có đủ mới cho query chỗ này var query = from p in _context.Products join pt in _context.ProductTranslations on p.Id equals pt.ProductId into ppt from pt in ppt.DefaultIfEmpty() //left join join pic in _context.ProductInCategories on p.Id equals pic.ProductId into ppic from pic in ppic.DefaultIfEmpty() // leftjoin join pi in _context.ProductImages on p.Id equals pi.ProductId into ppi from pi in ppi.DefaultIfEmpty() join c in _context.Categories on pic.CategoryId equals c.Id into picc from c in picc.DefaultIfEmpty() // leftjoin where pt.LanguageId == request.LanguageId select new { p, pt, pic, pi }; // bước 3: filter theo điều kiện if (!string.IsNullOrEmpty(request.KeyWord)) { query = query.Where(x => x.pt.Name.Contains(request.KeyWord)); } if (request.CategoryId != null && request.CategoryId != 0) // có nghĩa là có tất cả các tìm kiếm nào { query = query.Where(p => p.pic.CategoryId == request.CategoryId); //một trong só nhữ thằng này thì mới được } // bước 3: paging int totalRow = await query.CountAsync(); // lấy ra tông số số dòng để phân trang var data = await query.Skip((request.PageIndex - 1) *request.PageSize) .Take(request.PageSize)// nếu PageIndex=2 và PageSize=20 thì bỏ qua 10 chỉ lấy 10 bẩn ghi hiện lên ko lấy tất để phù hợp với PageSize .Select(x => new ProductVm() { Id = x.p.Id, Name = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.Name, DateCreated = x.p.DateCreated, Description = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.Description, Details = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.Details, LanguageId = x.pt.LanguageId, OriginalPrice = x.p.OriginalPrice, Price = x.p.Price, SeoAlias = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.SeoAlias, SeoDescription = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.SeoDescription, SeoTitle = x.pt == null ? SystemConstants.ProductConstants.NA : x.pt.SeoTitle, Stock = x.p.Stock, ViewCount = x.p.ViewCount, ThumbnailImage = x.pi.ImagePath }).ToListAsync(); // vì ta Async ở đây nên trên kia ta chỉ cần await để đẩy vào data là song nhớ là ToListAsync nha vì bên PageRsult Item ta để là list // bước 4: selecet and Project var pagedResult = new PagedResult <ProductVm>() { TotalRecords = totalRow, PageSize = request.PageSize, PageIndex = request.PageIndex, Items = data }; if (pagedResult != null) { return(new ApiSuccessResult <PagedResult <ProductVm> >(pagedResult)); } return(new ApiErrorResult <PagedResult <ProductVm> >()); }
public async Task <IActionResult> GetAllPaging([FromQuery] GetManageProductPagingRequest request) { var products = await _productService.GetAllPaging(request); return(Ok(products)); }
public async Task <ApiResult <PagedResult <ProductViewModel> > > GetAllPaging(GetManageProductPagingRequest request) { var client = _httpClientFactory.CreateClient(); client.BaseAddress = new Uri("https://localhost:5001"); var response = await client.GetAsync($"/api/Products/paging?pageIndex=" + $"{request.PageIndex}&pageSize={request.PageSize}&keyword={request.Keyword}"); var body = await response.Content.ReadAsStringAsync(); var pros = JsonConvert.DeserializeObject <ApiSuccessResult <PagedResult <ProductViewModel> > >(body); return(pros); }
public async Task <IActionResult> GetAllPaging([FromQuery] GetManageProductPagingRequest request) //Ham GET nen thuộc tính [FromQuery] chi dinh lay tham so tu URL { var products = await _productService.GetAllPaging(request); return(Ok(products)); }
public async Task <PageResult <ProductVm> > GetAllPaging(GetManageProductPagingRequest request) { throw new NotImplementedException(); }
public Task <PagedResult <ProductViewModel> > GetAllPaging(GetManageProductPagingRequest request) { throw new NotImplementedException(); }
public async Task <ApiResult <PagedResult <ProductViewModel> > > GetAllPaging(GetManageProductPagingRequest request) { //1: Select var query = from p in _context.ProductRepository.GetQuery() join pt in _context.ProductTranslationRepository.GetQuery() on p.Id equals pt.ProductId join pic in _context.ProductInCategoryRepository.GetQuery() on p.Id equals pic.ProductId into pptpic from pic in pptpic.DefaultIfEmpty() join c in _context.CategoryRepository.GetQuery() on pic.CategoryId equals c.Id into pc from c in pc.DefaultIfEmpty() join pi in _context.ProductImageRepository.GetQuery() 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: check filter if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.pt.Name.Contains(request.Keyword)); } if (request.CategoryId != 0 && request.CategoryId != null) { 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 ProductViewModel() { Id = x.p.Id, Price = x.p.Price, Original = x.p.Original, Stock = x.p.Stock, ViewCount = x.p.ViewCount, DateCreated = x.p.DateCreated, Name = x.pt.Name, Description = x.pt.Description, Details = x.pt.Details, SeoDescription = x.pt.SeoDescription, SeoTitle = x.pt.SeoTitle, SeoAlias = x.pt.SeoAlias, LanguageId = x.pt.LanguageId, Image = x.pi.ImagePath }).ToListAsync() ; // select vs projection var PageResult = new PagedResult <ProductViewModel>() { PageSize = request.PageSize, PageIndex = request.PageIndex, TotalRecords = totalRow, Items = data }; return(new ApiSuccessResult <PagedResult <ProductViewModel> >(PageResult)); }
public async Task <ApiResult <PagedResult <ProductViewModel> > > GetAllPaging(GetManageProductPagingRequest request) { var query = from p in _context.ProductImage select new { p }; if (!string.IsNullOrEmpty(request.Keyword)) { query = query.Where(x => x.p.Prod_Name.Contains(request.Keyword)); } //if (request.Supplier_ID.Count > 0) //{ // query = query.Where(p => request.Supplier_ID.Contains(p.pis.Supplier_ID)); //} //Paging int totalRow = await query.CountAsync(); var data = await query.Skip((request.PageIndex - 1) *request.PageSize) .Take(request.PageSize) .Select(x => new ProductViewModel() { Prod_ID = x.p.Prod_ID, Prod_Name = x.p.Prod_Name, DateCreate = x.p.DateCreate, Description = x.p.Description, Price = x.p.Price, Quantity = x.p.Quantity, Status = x.p.Status, ImagePath = x.p.ImagePath }).ToListAsync(); //Select and projection var pagedResult = new PagedResult <ProductViewModel>() { TotalRecords = totalRow, PageSize = request.PageSize, PageIndex = request.PageIndex, Items = data }; return(new ApiSuccessResult <PagedResult <ProductViewModel> >(pagedResult)); }