How to read class data

Asked

Viewed 162 times

1

I have an api that when receiving the data fills a class, I want to do a while in this class tb_dados_api and tb_carrinho, take the data to record in the database, how could I do that? Thanks

Here I am getting the data

  [HttpPost]
        [Route("unidade/carrinho/ConsultaUnidadeAtendimento")]
        public HttpResponseMessage ConsultaUnidadeAtendimento(TB_DADOS_API consultaAtendimento)
        {

            try
            {

                string numeroCarrinho =   consultaAtendimento.NumeroCarrinho.ToString();
                string cep = consultaAtendimento.Cep;
                bool retiraLocal = consultaAtendimento.RetiraNoLocal;

                var tTabela = new ConsultaUnidadeEstoque();
                var listar = tTabela.SelecionaUnidadeAtendimento(cep);
                return Request.CreateResponse(HttpStatusCode.OK, new { dados = listar.ToArray() });
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }

        }

They fill the class:

public class TB_DADOS_API
    {
        [JsonProperty("numeroCarrinho")]
        public long NumeroCarrinho { get; set; }

        [JsonProperty("itens")]
        public List<TB_CARRINHO> Itens { get; set; }

        [JsonProperty("cep")]
        public string Cep { get; set; }

        [JsonProperty("retiraNoLocal")]
        public bool RetiraNoLocal { get; set; }
    }

    public class TB_CARRINHO
    {
        [JsonProperty("codigo")]
        public string Codigo { get; set; }
        [JsonProperty("qtd")]
        public int Qtd { get; set; }
    }
  • ta using Entity framework or other Orm ?

  • I am not using Entity framework, I have the class filled, I want to read the data and write to the database

  • can use foreach to access the objects and save all in a table put the example ai in the answer

  • I appreciate immensely the help, my idea was to make this foreach inside the method Consultationstock and not in this location, because there I have a routine that will select where will be recorded the items

1 answer

1


In your case you can use foreach

[HttpPost]
        [Route("unidade/carrinho/ConsultaUnidadeAtendimento")]
        public HttpResponseMessage ConsultaUnidadeAtendimento(TB_DADOS_API consultaAtendimento)
        {

            try
            {

                string numeroCarrinho =   consultaAtendimento.NumeroCarrinho.ToString();
                string cep = consultaAtendimento.Cep;
                bool retiraLocal = consultaAtendimento.RetiraNoLocal;
                var tTabela = new ConsultaUnidadeEstoque();
                var listar = tTabela.SelecionaUnidadeAtendimento(cep);

                foreach(var item in consultaAtendimento.itens){
                    //  chamar seu repositorio para gravar item 
                    //item aqui ja é objeto exemplo você ja pode pegar 
                    //item.codigo ou item.qtd
                }

                return Request.CreateResponse(HttpStatusCode.OK, new { dados = listar.ToArray() });
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }

        }
  • Eduardo, I was able to do as I wanted, I just had to send : var to list = tTable.Selectunitycontact(consultationAtendimento);

  • 1

    But your idea also worked.

Browser other questions tagged

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