Doubt with Rest service and with put verb

Asked

Viewed 85 times

0

I just need to update two fields of my model. At get I pass a DTO to my App. The question is: When I go to do my update(put verb) do I need to load all the property or just the one I will update? Like this:

[Route("atualiza/{id}")]
        public void AtualizaLiberacao(LiberacaoDTO libera,int id)
        {
            var lista = contexto.Liberacoes
                .Where(l => l.IdOrcamento == id)
                .Select(s =>
                {
                    s.AutorizouReceberAtrazado = float.Parse(libera.AutorizouReceberAtrazado),
                    s.FlagLiberacao = 0
                });
        }

Only these fields will receive new values, too not. And how do I pass the text that will be updated? The id ok, but the text. I put a Liberacaodto only as an example, but I can pass the text as a string, that’s my question. In place of the object I can pass the text(string) to be modified. All this is only illustrative. You can pass two parameters through the URL instead of just id?

My service:

[AcceptVerbs("Put")]
 public void putItensLiberacao(int id, [FromBody]string value)
 {

 }

EDIT1

[Route("atualiza/{id}")]
        public void AtualizaLiberacao(LiberacaoDTO libera)
        {
                contexto.Liberacoes
                .Where(l => l.IdOrcamento == libera.IdOrcamento)
                .Select(s =>
                {
                    s.AutorizouReceberAtrazado = libera.AutorizouReceberAtrazado;
                    s.FlagLiberacao = libera.FlagLiberacao;
                });
        }

The form above is the correct one? And how is the route with the id? What will it serve?

I guess that’s it:

[Route("atualiza/{id}/{value}")]
    public void AtualizaLiberacao(int id, string value)
    {
        var lista = contexto.Liberacoes
                    .Where(l => l.IdOrcamento == id).ToList();

        lista.ForEach(f =>
        {
            f.FlagLiberacao = 0;
            f.AutorizouReceberAtrazado = value;
        });

        contexto.SaveChanges();

    }

1 answer

2

When I go to do my update(put verb) I need to load all the property or just the one I’ll update?

No, Voce can charge only what Voce will use.

[Route("atualiza/{id}/{value1}/{value2}")]
        public void AtualizaLiberacao(int value1, float value2, int id)
        {
            var lista = contexto.Liberacoes
                .Where(l => l.IdOrcamento == id)
                .Select(s =>
                {
                    s.AutorizouReceberAtrazado = value2,
                    s.FlagLiberacao = value1
                });
        }

You can pass two parameters through the URL instead of just id?

Yes, you can pass as many paramenters as you want. example: [Route("atualiza/{id}/{value}")]

That?

  • You’re making this mistake: The type arguments of the method "Queryable.Select<Tsource, Tresult>(Iqueryable<Tsource>, Expression<Func<Tsource, Tresult>>)" cannot be inferred based on use. Try to specify explicitly the type arguments. Autorizadorservice C: Projects Authorizationservice Authorizationservice Models Itensrelease.Cs in the lambda select

  • puts a breakpoint on var= list... and checks if the parametrics have any information

  • It does not rotate, it is giving dick exactly in the select of the lambda. Neither compiles. I have removed the var list, because it is not necessary, because the method is a void, and still with stick.

  • @pnet you need to bring the object and change only the fields, what you are doing does not work!

  • @Virgilionovic, what do you mean? How do I do it?

  • @pnet whether you are trying to change data with Select? that doesn’t work! that’s where you’re wrong, if you have to bring the object in full, or use another technique to update these fields.

  • I made an edit and see if that’s it, @Virgilionovic, what do you say? If that’s it, what’s the id parameter on the route? What’s it going to do?

  • @pnet no How do you change an entity’s data in the Entity framework is not bringing the object like this var a = db.Pessoa.Find(1); and then a.Nome = "novo nome"; and then db.SaveChanges();! what you are doing is to bring information and not to save information.

  • Got it. Select would be more for get verbs or not it? I really want an update. If it’s not Select, what would it be?

  • I recommend you take a look at this: http://www.entityframeworktutorial.net/crud-operation-in-connected-scenario-entity-framework.aspx

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.