// GET: api/Info
[HttpGet]
public IEnumerable<Products> GetData([FromQuery]Products sm,[FromQuery]int skip, [FromQuery]int take, [FromQuery]string sortColName,[FromQuery]string sortdirection, [FromQuery(Name = "price[gt]")] float price1, [FromQuery(Name = "price[lt]")] float price2,[FromQuery(Name="colour[like]")] string clr)
{
var data = _context.Products.ToList();
var sorting = (from p in data
select p);
List<Products> TakeResult = null;
List<Products> ResultOfSort = null;
List<Products> ResultOfSkip = null;
List<Products> ResultOfPrice = null;
// Sorting Part
if (sortColName != null && sortdirection != null)
{
TakeResult = sortMethod(data, sortColName, sortdirection);
}
if (skip != 0 || take != 0 && TakeResult!=null)
{
ResultOfSort = SkipMethod(TakeResult, skip, take);
}
if(price1 != 0 || price2 != 0 && ResultOfSort!=null)
{
ResultOfSkip = PriceMethod(ResultOfSort, price1, price2);
}
if (clr != null && ResultOfSkip!=null)
{
ResultOfPrice = ColourMethod(ResultOfSkip, clr);
return ResultOfPrice;
}
//Main method return
return sorting;
}
//Sorting
private List<Products> sortMethod(List<Products>data,string sortColName, string sortdirection)
{
var sorting = (from p in data
select p).ToList();
var stcolName = typeof(Products).GetProperty(sortColName);
List<Products> sortingRes = null;
switch (sortdirection)
{
case "desc":
sortingRes = sorting.OrderByDescending(o=>stcolName.GetValue(o)).ToList();
break;
default:
sortingRes = sorting.OrderBy(o => stcolName.GetValue(o)).ToList();
break;
}
return sortingRes;
}
//Skip and Take Method
private List<Products> SkipMethod(List<Products> TakeResult, int skip, int take)
{
var data = _context.Products.ToList();
var result = (from s in TakeResult.Skip(skip).Take(take) select s).ToList();
return result;
}
//Price
private List<Products> PriceMethod(List<Products> ResultOfSort, float price1, float price2)
{
List<Products> priceresult = null;
if (price1 != 0)
{
priceresult = ResultOfSort.Where(o => o.Price > price1).ToList();
}
else if (price2 != 0)
{
priceresult = ResultOfSort.Where(o => o.Price < price2).ToList();
}
return priceresult;
}
//Colour Method
private List<Products> ColourMethod(List<Products> ResultOfSkip, string clr)
{
List<Products> resClr = null;
resClr = ResultOfSkip.Where(a => a.Colour == clr).ToList();
return resClr;
}
// GET: api/Info/5
[HttpGet("{id}")]
public async Task<ActionResult<Products>> GetProducts(int id)
{
var products = await _context.Products.FindAsync(id);
if (products == null)
{
return NotFound();
}
return products;
}