public async Task <IActionResult> GetAllProducts([FromQuery] ProductQueryParameters queryParameters) { var products = _context.Products.AsQueryable(); // min/max price products = (queryParameters.MinPrice, queryParameters.MaxPrice) switch { (null, null) => products, (_, null) => products.Where(p => p.Price > queryParameters.MinPrice.Value), (null, _) => products.Where(p => p.Price < queryParameters.MaxPrice.Value), (_, _) => products.Where(p => p.Price >= queryParameters.MinPrice.Value && p.Price <= queryParameters.MaxPrice.Value ) }; // sku products = !string.IsNullOrEmpty(queryParameters.Sku) ? products.Where(p => p.Sku == queryParameters.Sku) : products; // search products = !string.IsNullOrEmpty(queryParameters.Name) ? products.Where(p => p.Name.ToLower().Contains(queryParameters.Name.ToLower())) : products; // order products = !string.IsNullOrEmpty(queryParameters.SortBy) && typeof(Product).GetProperty(queryParameters.SortBy) != null ? products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder) : products; // pagination products = products .Skip(queryParameters.Size * (queryParameters.Page - 1)) .Take(queryParameters.Size); return(Ok(await products.ToArrayAsync())); }
public async Task <IActionResult> GetAllProducts([FromQuery] ProductQueryParameters queryParameters) { IQueryable <Product> products = _context.Products.Where(p => p.IsAvailable); if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) { products = products .Where(p => p.Price >= queryParameters.MinPrice.Value && p.Price <= queryParameters.MaxPrice.Value); } if (!String.IsNullOrEmpty(queryParameters.Sku)) { products = products.Where(p => p.Sku == queryParameters.Sku); } if (!String.IsNullOrEmpty(queryParameters.Name)) { products = products.Where(p => p.Name.ToLower().Contains(queryParameters.Name.ToLower())); } if (!String.IsNullOrEmpty(queryParameters.SearchTerm)) { products = products.Where(p => p.Sku.ToLower().Contains(queryParameters.SearchTerm.ToLower()) || p.Name.ToLower().Contains(queryParameters.SearchTerm.ToLower())); } if (!String.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } products = products .Skip(queryParameters.Size * (queryParameters.Page - 1)) .Take(queryParameters.Size); return(Ok(await products.ToArrayAsync())); }
public async Task Can_get_items_descending_order() { using (var context = new StoreContext(ContextOptions)) { var queryParameters = new ProductQueryParameters(); queryParameters.SortBy = "Price"; queryParameters.SortOrder = "desc"; var repo = new ProductRepository(context); var items = await repo.GetAllAsync(queryParameters); Assert.Equal(4, items.Count()); Assert.Equal("Name4", items.ElementAt(0).Name); Assert.Equal("Name3", items.ElementAt(1).Name); Assert.Equal("Name2", items.ElementAt(2).Name); Assert.Equal("Name1", items.ElementAt(3).Name); } }
public async Task <IActionResult> GetAllProduct([FromQuery] ProductQueryParameters queryParameters) { IQueryable <Product> products = _context.Products.Where(s => s.IsAvailable == true); // thesee to if conditions just check the some filters which we want to apply or not... if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) { products = products .Where(s => s.Price >= queryParameters.MinPrice && s.Price <= queryParameters.MaxPrice); } if (!string.IsNullOrEmpty(queryParameters.Sku)) { products = products .Where(s => s.Sku.ToLower() .Contains(queryParameters.Sku.ToLower())); } if (!string.IsNullOrEmpty(queryParameters.Name)) { products = products .Where(s => s.Name.ToLower() .Contains(queryParameters.Name.ToLower())); } if (!string.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } products = products.Skip(queryParameters.Size * (queryParameters.Page - 1)).Take(queryParameters.Size); if (products == null) { return(NotFound()); } return(Ok(await products.Include(x => x.Category).ToListAsync())); }
public async Task <IActionResult> GetAllProducts([FromQuery] ProductQueryParameters queryParameters) //to see the amount of product you want you need to specify that amount as int here, and to see others you need to specify page number { IQueryable <Product> products = _context.Products; //querying all data //filtering by price if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) //we check if MaxPrice and MinPrice has been set { products = products.Where( p => p.Price >= queryParameters.MinPrice.Value && p.Price <= queryParameters.MaxPrice.Value); } //filtering by sku if (!string.IsNullOrEmpty(queryParameters.Sku)) { products = products.Where(p => p.Sku == queryParameters.Sku); } //serching items example you can pass this parameters: https://localhost:44388/products?size=10&page=1&name=Orange mineral water if (!string.IsNullOrEmpty(queryParameters.Name)) //!string.IsNullOrEmpty() check if only this paramter has been set, ie has been specified by the user. { products = products.Where( p => p.Name.ToLower().Contains(queryParameters.Name.ToLower())); } //Sorting items example: https://localhost:44388/products?sortBy=Price&sortOrder=desc if (!string.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } products = products .Skip(queryParameters.Size * (queryParameters.Page - 1)) // example, for page number 2 we have to skip one times the size given items, show from the next item number follow .Take(queryParameters.Size); return(Ok(await products.ToArrayAsync())); }
public async Task <IActionResult> GetAllProducts([FromQuery] ProductQueryParameters queryParameters) //to see the amount of product you want you need to specify that amount as int here, and to see others you need to specify page number { IQueryable <Product> products = _context.Products.Where(p => p.IsAvailable == true); //we make small changes so that it can deffer with Version 1 //filtering by price if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) { products = products.Where( p => p.Price >= queryParameters.MinPrice.Value && p.Price <= queryParameters.MaxPrice.Value); } //filtering by sku if (!string.IsNullOrEmpty(queryParameters.Sku)) { products = products.Where(p => p.Sku == queryParameters.Sku); } //serching items example you can pass this parameters: https://localhost:44388/v2.0/products?size=10&page=1&name=Orange mineral water if (!string.IsNullOrEmpty(queryParameters.Name)) { products = products.Where( p => p.Name.ToLower().Contains(queryParameters.Name.ToLower())); } //Sorting items example: https://localhost:44388/v2.0/products?sortBy=Price&sortOrder=desc if (!string.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } products = products .Skip(queryParameters.Size * (queryParameters.Page - 1)) .Take(queryParameters.Size); return(Ok(await products.ToArrayAsync())); }
public async Task <IActionResult> GetProducts([FromQuery] ProductQueryParameters query) { IQueryable <Product> products = _context.Products; if (query.MinPrice != null && query.MaxPrice != null) { products = products.Where(p => p.Price >= query.MinPrice && p.Price <= query.MaxPrice); } if (!string.IsNullOrEmpty(query.Sku)) { products = products.Where(p => p.Sku == query.Sku); } products = products.Skip(query.Size * (query.Page - 1)) .Take(query.Size); return(Ok(await products.ToListAsync())); }
public async Task <IEnumerable <Product> > GetAllProductsAsync(ProductQueryParameters queryParameters) { IQueryable <Product> products = _context.Products; //price filter if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) { products = products.Where(p => p.Price >= queryParameters.MinPrice.Value && p.Price <= queryParameters.MaxPrice.Value); } //sku filter if (!string.IsNullOrEmpty(queryParameters.Sku)) { products = products.Where(p => p.Sku == queryParameters.Sku); } //name filter if (!string.IsNullOrEmpty(queryParameters.Name)) { products = products.Where(p => p.Name.Contains(queryParameters.Name, StringComparison.InvariantCultureIgnoreCase)); } //order filter if (!string.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } //pagination products = products .Skip(queryParameters.Size * (queryParameters.Page - 1)) .Take(queryParameters.Size); return(await products.ToListAsync()); }
public async Task <Category> GetAllProductsFromSubCategory(string mainCategory, string catName, ProductQueryParameters queryParameters) { var mainCategoryId = _context.CategoryMain .Where(m => m.Name == mainCategory) .FirstOrDefault(); if (mainCategoryId != null) { // Main category exists var subCategoryId = _context.Categories .Where(s => s.Name == catName && s.CategoryMainId == mainCategoryId.Id) .FirstOrDefault(); if (subCategoryId != null) { // Sub category exists and is part of main category var categorizedProducts = _context.Categories .Where(c => c.Name == catName && c.CategoryMainId == mainCategoryId.Id) .Include(p => p.Products) .FirstOrDefaultAsync(); return(await categorizedProducts); } else { // Main category exists, sub doesnt. return(null); } } // Default return type return(null); }
public ProductSpecificationToCountNumberOfProducts(ProductQueryParameters parameters) : base( x => (!parameters.TypeId.HasValue || x.ProductTypeId == parameters.TypeId.Value) && (!parameters.BrandId.HasValue || x.ProductBrandId == parameters.BrandId.Value) && (string.IsNullOrWhiteSpace(parameters.Search) || x.Name.ToLower().Contains(parameters.Search))) { }
public async Task <ActionResult <Pagination <ProductToReturnDto> > > GetProducts([FromQuery] ProductQueryParameters parameters) { var productSpecifications = new ProductWithTypeAndBrandSpecifications(parameters); var numberOfProductsSpecifications = new ProductSpecificationToCountNumberOfProducts(parameters); var products = await _productRepo.GetAllWithSpecifications(productSpecifications); var totalItems = await _productRepo.CountNumberOfProducts(numberOfProductsSpecifications); var productsToReturn = _mapper.Map <IReadOnlyCollection <ProductToReturnDto> >(products); return(Ok(new Pagination <ProductToReturnDto>(parameters.PageIndex, totalItems, parameters.PageSize, productsToReturn))); }
public async Task <ActionResult <Category> > GetAllProductsFromSubCategory(string mainCategory, string catName, [FromQuery] ProductQueryParameters queryParameters) { var products = await _ProductsDbAccess.GetAllProductsFromSubCategory(mainCategory, catName, queryParameters); if (products == null) { // This explicitly means that either of the categories is not found. // This shouldn't happen if the categories exist but there's simply no products. return(NotFound()); } return(products); }
public async Task <IEnumerable <Product> > GetAllAsync(ProductQueryParameters queryParameters) { try { var products = _context.Products.AsQueryable().Include("ProductType").Include("ProductBrand"); if (!products.Any()) { return(new List <Product>()); } if (queryParameters.UPC != null) { products = products.Where(p => p.UPC.Contains(queryParameters.UPC)); if (!products.Any()) { return(new List <Product>()); } } else { if (queryParameters.Name != null) { products = products.Where(p => p.Name.ToLower().Contains(queryParameters.Name.ToLower())); } if (queryParameters.MinPrice != null && queryParameters.MaxPrice != null) { products = products.Where(x => x.Price >= queryParameters.MinPrice.Value && x.Price <= queryParameters.MaxPrice.Value); } if (queryParameters.ProductBrandId != null) { products = products.Where(p => p.ProductBrand.Id == queryParameters.ProductBrandId); } if (queryParameters.ProductTypeId != null) { products = products.Where(p => p.ProductType.Id == queryParameters.ProductTypeId); } if (queryParameters.Description != null) { products = products.Where(p => p.Description.ToLower().Contains(queryParameters.Description.ToLower())); } if (!string.IsNullOrEmpty(queryParameters.SortBy)) { if (typeof(Product).GetProperty(queryParameters.SortBy) != null) { products = products.OrderByCustom(queryParameters.SortBy, queryParameters.SortOrder); } } products = products.Skip(queryParameters.Size * (queryParameters.Page - 1)) .Take(queryParameters.Size); } return(await products.ToListAsync()); } catch (Exception ex) { throw new Exception($"Couldn't retrieve entities: {ex.Message}"); } }
public ProductsViewModel(ProductQueryParameters parameters, PagedResult <ProductViewModel> pagedResult) { Parameters = parameters; PagedResult = pagedResult; }
public MockProductRepository MockGetAllAsync(Task <IEnumerable <Product> > results, ProductQueryParameters queryParameters) { Setup(x => x.GetAllAsync(queryParameters)).Returns(results); return(this); }