コード例 #1
0
        private async Task UpdateVinho() //método que atualiza vinho no banco
        {
            Vinhos vinho = new Vinhos();

            vinho.cod_vinho   = Convert.ToInt32(this.textCodigo.Text);
            vinho.nome_vinho  = this.textNome.Text;
            vinho.idade_vinho = Convert.ToInt32(this.textIdade.Text);
            vinho.preco_vinho = Convert.ToDecimal(this.textPreco.Text);


            using (var client = new HttpClient())
            {
                HttpResponseMessage response = await client.PutAsJsonAsync(this.URI + "/" + vinho.cod_vinho, vinho); //url e cabeçalho

                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("Vinho atualizado!");
                    await getAllVinhos();
                }
                else
                {
                    MessageBox.Show("Falha na atualização:" + response.StatusCode);
                }
            }
        }
コード例 #2
0
        private async Task AddVinho() //método que adiciona o objeto
        {
            Vinhos vinho = new Vinhos();

            vinho.nome_vinho  = this.textNome.Text;
            vinho.idade_vinho = Convert.ToInt32(this.textIdade.Text);
            vinho.preco_vinho = Convert.ToDecimal(this.textPreco.Text);

            using (var client = new HttpClient())
            {
                var vinhoSerialized = JsonConvert.SerializeObject(vinho); //transforma objeto em Json
                var content         = new StringContent(vinhoSerialized, Encoding.UTF8, "application/json");
                var result          = await client.PostAsync(this.URI, content);
            }

            await this.getAllVinhos();
        }