Reputation: 293
So I'm creating a database that allows users to create a "magic item" which they can then upload to my ASP.net Web API. This works just fine. And I want to show all these items in my web page by pulling them from the api. This also works fine. But now, when i try to filter, sort or limit the amount of searches, I still get the basic list of every item returned to me. Right now, there's only 14 entries, so no big deal, but i still want to get this done. But whatever i do, it always returns the full list.
This is the ASP.net Controller in visual studio:
[Route("api/v1/MagicItem")]
public class MagicItemController : Controller
{
private ItemListContext context;
public MagicItemController(ItemListContext context)
{
this.context = context;
}
[Produces("application/json")]
[HttpGet]
//public List<MagicItem> GetAllItems(string name, string category, string rarity, int? page, string sort, int limit = 5, string dir = "desc")
public List<MagicItem> GetAllItems(
string name,
string category,
string rarity,
int? page,
string sort,
int limit = 5,
string dir = "desc")
{
IQueryable<MagicItem> query = context.MagicItems;
if (!string.IsNullOrWhiteSpace(name))
query = query.Where(d => d.Name.Contains(name));
if (!string.IsNullOrWhiteSpace(category))
query = query.Where(d => d.Category == category);
if (!string.IsNullOrWhiteSpace(rarity))
query = query.Where(d => d.Rarity == rarity);
if (!string.IsNullOrWhiteSpace(sort))
{
switch (sort)
{
case "Name":
if (dir == "asc")
query = query.OrderBy(d => d.Name);
else if (dir == "desc")
query = query.OrderByDescending(d => d.Name);
break;
case "Rarity":
if (dir == "asc")
query = query.OrderBy(d => d.Rarity);
else if (dir == "desc")
query = query.OrderByDescending(d => d.Rarity);
break;
case "Category":
if (dir == "asc")
query = query.OrderBy(d => d.Category);
else if (dir == "desc")
query = query.OrderByDescending(d => d.Category);
break;
}
}
query = query.Take(limit);
if (page.HasValue)
query = query.Skip(page.Value * limit);
return context.MagicItems.ToList();
}
}
Upvotes: 3
Views: 5546
Reputation: 260
Create method for filter: First, we convert filter codes to a string
string where =string.Empty;
if (!string.IsNullOrWhiteSpace(name))
where + = name;
if (!string.IsNullOrWhiteSpace(category))
where += category;
if (!string.IsNullOrWhiteSpace(rarity))
where += rarity;
var entity =
setFilter(context.MagicItems,where,order)
Return entity;
Your method:
Public IEnumerable setFilter(TEntity entity
,func<IQueryable<bool
out,TEntity)> where
=null , func<IQueryable<TEntity>
,IOrderedQueryable<TEntity>> order =null)
{
IQueryable query = entity;
If(whrer != null)
{
query =query.where(where);
}
If(order != null)
{
query =Order(query);
}
Return query.toList();
}
Upvotes: 0
Reputation: 293
In a previous version of the code, i wasn't using paging yet, so i created the list of items in the return-line itself. All i had to do was actually return the query i was working on.
Upvotes: 0
Reputation: 17658
You are almost there:
just use:
return query.ToList();
instead of:
return context.MagicItems.ToList();
Upvotes: 3