public async Task <ActionResult <Response> > PostProductExistence(ProductExistence productExistence) { Response response = new Response(); ProductExistence dbProductExistence = await _context.ProductExistences.Where(item => item.StoreId == productExistence.StoreId && item.ProductId == productExistence.ProductId).FirstOrDefaultAsync(); if (dbProductExistence != null) { response.ErrorCode = ResultBadRequest; response.ErrorMessage = "Existence for store and product already exists"; return(BadRequest(response)); } using (IDbContextTransaction dbTransaction = _context.Database.BeginTransaction()) { try { _context.ProductExistences.Add(productExistence); await _context.SaveChangesAsync(); dbTransaction.Commit(); } catch (DbUpdateException ex) { dbTransaction.Rollback(); response.ErrorCode = ResultDBError; response.ErrorMessage = ex.Message; return(BadRequest(response)); } catch (Exception ex) { dbTransaction.Rollback(); response.ErrorCode = ResultBadRequest; response.ErrorMessage = ex.Message; return(BadRequest(response)); } } return(GetProductExistence(productExistence.StoreId, productExistence.ProductId)); }
public async Task <ActionResult <Response> > PutProductExistence(int storeId, int productId, ProductExistence productExistence) { Response response = new Response(); if (storeId != productExistence.StoreId && productId != productExistence.ProductId) { response.ErrorCode = ResultBadRequest; response.ErrorMessage = "Id error"; return(BadRequest(response)); } _context.Entry(productExistence).State = EntityState.Modified; using (IDbContextTransaction dbTransaction = _context.Database.BeginTransaction()) { try { await _context.SaveChangesAsync(); dbTransaction.Commit(); } catch (DbUpdateException ex) { dbTransaction.Rollback(); response.ErrorCode = ResultDBError; response.ErrorMessage = ex.Message; return(BadRequest(response)); } catch (Exception ex) { dbTransaction.Rollback(); response.ErrorCode = ResultBadRequest; response.ErrorMessage = ex.Message; return(BadRequest(response)); } } return(GetProductExistence(productExistence.StoreId, productExistence.ProductId)); }
private async void m_btnOK_Click(object sender, EventArgs e) { // Check for valid data if (m_cbStores.SelectedIndex == 0) { MessageBox.Show("Seleccione una tienda", "AVISO"); m_cbStores.Focus(); return; } if (string.IsNullOrEmpty(m_tbTotalInShelf.Text)) { MessageBox.Show("Especifique el total en estante", "AVISO"); m_tbTotalInShelf.Focus(); return; } if (!decimal.TryParse(m_tbTotalInShelf.Text, out _)) { MessageBox.Show("Verifique que el total en estante sea un número válido", "AVISO"); m_tbTotalInShelf.Focus(); return; } if (string.IsNullOrEmpty(m_tbTotalInVault.Text)) { MessageBox.Show("Especifique el total en bodega", "AVISO"); m_tbTotalInVault.Focus(); return; } if (!decimal.TryParse(m_tbTotalInVault.Text, out _)) { MessageBox.Show("Verifique que el total en bodega sea un número válido", "AVISO"); m_tbTotalInVault.Focus(); return; } ProductExistence productExistence = new ProductExistence { StoreId = (int)m_cbStores.SelectedValue, ProductId = ProductId, TotalInShelf = Convert.ToInt32(m_tbTotalInShelf.Text), TotalInVault = Convert.ToInt32(m_tbTotalInVault.Text) }; try { Response response = null; if (IsNew) { response = await APIUtilities.Post("services/existences/", productExistence); } else { response = await APIUtilities.Put("services/existences/" + ProductExistence.StoreId + "/" + productExistence.ProductId, productExistence); } if (response != null) { if (response.Success) { ProductExistence = (ProductExistenceDTO)JsonConvert.DeserializeObject(response.Component.ToString(), (typeof(ProductExistenceDTO))); DialogResult = DialogResult.OK; Close(); } else { MessageBox.Show(response.ErrorMessage, "AVISO"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR DE SISTEMA"); } }