private async void Consulta_Producto() { this.Cursor = Cursors.WaitCursor; using (var client = new HttpClient()) { using (var response = await client.GetAsync(Globales.Url_API + "productos/consultaproductoid/" + _productoID.ToString())) { if (response.IsSuccessStatusCode) { var jsonString = await response.Content.ReadAsStringAsync(); var productoJson = JsonConvert.DeserializeObject <typeProducto[]>(jsonString); typeProducto producto = productoJson.FirstOrDefault(); Txt_Codigo.Text = producto.Codigo; Txt_Nombre.Text = producto.Nombre; Txt_Precio.Value = producto.Precio; } else { this.Cursor = Cursors.Default; var contents = await response.Content.ReadAsStringAsync(); MessageBox.Show(contents); } } } this.Cursor = Cursors.Default; }
private async void Agrega_Producto() { this.Cursor = Cursors.WaitCursor; typeProducto producto = new typeProducto(); producto.Codigo = Txt_Codigo.Text.Trim().ToUpper(); producto.Nombre = Txt_Nombre.Text.Trim().ToUpper(); producto.Precio = Txt_Precio.Value; using (var client = new HttpClient()) { var serializedProduct = JsonConvert.SerializeObject(producto); var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json"); using (var response = await client.PostAsync(Globales.Url_API + "productos/agregaproducto", content)) { if (response.IsSuccessStatusCode) { this.Cursor = Cursors.Default; _existenCambios = true; MessageBox.Show("Producto agregado"); this.Close(); } else { this.Cursor = Cursors.Default; var contents = JsonConvert.DeserializeObject <typeResponse>(response.Content.ReadAsStringAsync().Result); MessageBox.Show(contents.Message); } } } }
private async void Buscar_Producto_Codigo() { Boolean encontroProducto = false; int indexProducto = 0; if (Txt_Codigo.Text.Trim() == "") { return; } this.Cursor = Cursors.WaitCursor; using (var client = new HttpClient()) { using (var response = await client.GetAsync(Globales.Url_API + "productos/consultaproductocodigo/" + Txt_Codigo.Text.Trim())) { if (response.IsSuccessStatusCode) { var jsonString = await response.Content.ReadAsStringAsync(); var productoJson = JsonConvert.DeserializeObject <typeProducto[]>(jsonString); typeProducto producto = productoJson.FirstOrDefault(); if (producto != null) { foreach (var item in detallesProductos) { if (item.Producto_ID == producto.Producto_ID) { encontroProducto = true; break; } indexProducto += 1; } if (!encontroProducto) { detallesProductos.Add(new typeDetalles { Producto_ID = producto.Producto_ID, Codigo = producto.Codigo, Nombre = producto.Nombre, Precio = producto.Precio, Cantidad = 1, Importe = producto.Precio }); } else { var item = detallesProductos[indexProducto]; item.Cantidad += 1; item.Importe = item.Cantidad * item.Precio; } Grid_Productos.DataSource = null; Grid_Productos.DataSource = detallesProductos; Calcula_Totales(); } else { this.Cursor = Cursors.Default; MessageBox.Show("Producto no encontrado"); } Txt_Codigo.Text = ""; Txt_Codigo.Focus(); } else { this.Cursor = Cursors.Default; var contents = await response.Content.ReadAsStringAsync(); MessageBox.Show(contents); } } } this.Cursor = Cursors.Default; }