public async Task <IActionResult> Index(string keyword, int productType, int pageIndex = 1, int pageSize = 3) { var request = new GetRecipePagingRequest() { Keyword = keyword, ProductType = productType, PageIndex = pageIndex, PageSize = pageSize }; var data = await _recipeApiClient.GetRecipePaging(request); ViewBag.Keyword = keyword; return(View(data.ResultObj)); }
public async Task <ApiResult <PagedResult <ProductRecipe> > > GetRecipePaging(GetRecipePagingRequest bundle) { var url = $"/api/Recipe/paging?pageIndex=" + $"{bundle.PageIndex}&pageSize={bundle.PageSize}&keyword={bundle.Keyword}&productType={bundle.ProductType}"; var result = await GetListAsync <ProductRecipe>(url); return(result); }
public async Task <IActionResult> GetAllPaging([FromQuery] GetRecipePagingRequest request) { var result = await _recipeService.GetRecipePaging(request); return(Ok(result)); }
public async Task <ApiResult <PagedResult <ProductRecipe> > > GetRecipePaging(GetRecipePagingRequest bundle) { var query = from s in _context.Products join g in _context.ProductTypes on s.IdProductType equals g.Id join d in _context.Recipes on s.Id equals d.IdProduct into r from d in r.DefaultIfEmpty() where d.Prioritize != false select new { s, d = (d == null) ? "Chưa có công thức" : d.Name, g }; if (bundle.ProductType > 0) { query = query.Where(x => x.g.Id == bundle.ProductType); } if (!string.IsNullOrEmpty(bundle.Keyword)) { query = query.Where(c => c.s.Name.Contains(bundle.Keyword) || c.s.Code.Contains(bundle.Keyword)); } //3. Paging int totalRow = await query.CountAsync(); query = query.OrderByDescending(c => c.s.Id); var data = await query.Skip((bundle.PageIndex - 1) *bundle.PageSize) .Take(bundle.PageSize) .Select(i => new ProductRecipe() { Id = i.s.Id, Code = i.s.Code, Name = i.s.Name, Image = i.s.Image, NameProductType = i.g.Name, NameRecipe = i.d }).ToListAsync(); //4. Select and projection var pagedResult = new PagedResult <ProductRecipe>() { TotalRecords = totalRow, PageIndex = bundle.PageIndex, PageSize = bundle.PageSize, Items = data }; return(new ApiSuccessResult <PagedResult <ProductRecipe> >(pagedResult)); }