Get method with where customizable

1

I made WebAPI ( ASPNET with EF6 ) with support for Get() , Post() and Delete() verbs. At some point, I need to access certain values that are not returned by default by verbs, for example:

The Request Controller has a Get() method that will return the entire listing and a Get(int id) method that will bring the request through the ID, but if I want a request for the Id of the related establishment I would have to take all Orders and filter in the application. This stinks with me and makes the request very cumbersome.

I would like to make a Get() that receives, for example, a parameter where and a parameter with the value that I expect from where, for example id_estabelecimento/3 . Is it possible to do this? is there a better way to handle this type of demand?

    
asked by anonymous 12.03.2018 / 20:25

1 answer

3

Thank you very much for the comments of your colleagues in the community. I talked to a colleague yesterday about the problem and saw that the best way (for security and usability reasons) is to create the endpoints according to what my application will need. I used some of the ViewModel pattern and created a new endpoint to return only what I need. Here are some comments:

    [HttpGet]
    [Route("order/estabelecimento/{id}")] //Rota definida a partir do estabelecimento/ para não conflitar com outro endpoint
    public async Task<List<ordemViewModel>> GetByEstablishmentId(string id)
    {
        new OrdemController();
        List <ordemViewModel> orders = await (from itens in db.ordem where itens.id_estabelecimento == id select new ordemViewModel
         //Cria um ViewModel do tipo ordem e solicita apenas os dados que preciso
        { 
          id = itens.id,
          preco = itens.preco
          dataCriacao = itens.dataCriacao
          //Dados que eu preciso
        }).ToListAsync();
        if (!orders.Any())
        {
            HttpResponseMessage error = new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(string.Format("Nenhuma ordem encontrada")),
                StatusCode = HttpStatusCode.NotFound,
                ReasonPhrase = "Order Not Found"
            };
            throw new HttpResponseException(error);
        }
        return orders;
    }
    
13.03.2018 / 13:44