Skip to content

Instantly share code, notes, and snippets.

@enisn
Last active April 16, 2020 18:40
Show Gist options
  • Save enisn/89ff008f34fc55e965b35f5ac22a5143 to your computer and use it in GitHub Desktop.
Save enisn/89ff008f34fc55e965b35f5ac22a5143 to your computer and use it in GitHub Desktop.
Challenge #1 - Solution 1 ProductsController
public class ProductsController
{
private readonly MyDbContext context
public ProductsController(MyDbContext context)
{
this.context = context;
}
[HttpGet]
public async Task<IActionResult> GetAsync([FromQuery]SomeDto filter)
{
var query = context.Products.AsQuaryable();
if(filter.Name != null)
query = query.Where(x => x.Name.Contains(filter.Name));
if(filter.Description != null)
query = query.Where(x => x.Description.Contains(filter.Description));
if(filter.Code != null)
query = query.Where(x => x.Code == filter.Code);
if(filter.Price.Max != null)
query = query.Where(x => x.Price <= filter.Price.Max);
if(filter.Price.Min != null)
query = query.Where(x => x.Price >= filter.Price.Min);
if(filter.ShippingPrice.Max != null)
query = query.Where(x => x.ShippingPrice <= filter.ShippingPrice.Max);
if(filter.ShippingPrice.Min != null)
query = query.Where(x => x.ShippingPrice <= filter.ShippingPrice.Min);
if(filter.Stock.Max != null)
query = query.Where(x => x.Price <= filter.Price.Max);
if(filter.Stock.Min != null)
query = query.Where(x => x.Price >= filter.Price.Min);
if(filter.CreateDate.Max != null)
query = query.Where(x => x.CreateDate <= filter.Price.Max);
if(filter.CreateDate.Min != null)
query = query.Where(x => x.Price >= filter.CreateDate.Min);
return Ok(await query.ToListAsync())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment