2

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();
    }
}
Phuc Thai
  • 718
  • 7
  • 17
MyNameIsGuzse
  • 199
  • 1
  • 3
  • 15
  • [Stefan](https://stackoverflow.com/a/50657643/296861) pretty much answered your question. [This](https://stackoverflow.com/a/34908081/296861) one might help you shorten the code. More usage [here](https://github.com/WinLwinOoNet/AspNetCoreActiveDirectoryStarterKit/blob/master/src/Libraries/Asp.Repositories/Users/UserRepository.cs#L35) – Win Jun 02 '18 at 14:01
  • There are few open source projects that implement this. For example sieve. – Konrad Jun 02 '18 at 16:05

3 Answers3

3

You are almost there:

just use:

return query.ToList();

instead of:

return context.MagicItems.ToList();
Stefan
  • 14,240
  • 9
  • 51
  • 69
0

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.

MyNameIsGuzse
  • 199
  • 1
  • 3
  • 15
0

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();
                  }
Ali Tooshmalani
  • 169
  • 1
  • 6