public async Task <IActionResult> PostStockItemAsync([FromBody] PostStockItemsRequest request) { Logger?.LogDebug("'{0}' foi chamada", nameof(PostStockItemAsync)); var response = new SingleResponse <StockItem>(); try { var existingEntity = await _context.GetStockItemsByStockItemNameAsync(new StockItem { StockItemName = request.StockItemName }); if (existingEntity != null) { ModelState.AddModelError("StockItemName", "Stock Item Name já existe!"); } if (!ModelState.IsValid) { return(BadRequest()); } // Setando a entidade pelo request da model var entity = request.ToEntity(); // Add no BD _context.Add(entity); // Salva await _context.SaveChangesAsync(); // Seta a entidade para o model de resposta response.Model = entity; } catch (Exception ex) { response.DidError = true; response.ErrorMessage = "Houve um erro interno, por favor contate o suporte."; Logger?.LogCritical("Houve um erro na chamada '{0}' invocando: {1} ", nameof(PostStockItemAsync), ex); } return(response.ToHttpResponse()); }
public async Task <IActionResult> PostStockItemAsync([FromBody] PostStockItemsRequest request) { Logger?.LogDebug("'{0}' has been invoked", nameof(PostStockItemAsync)); var response = new SingleResponse <StockItem>(); try { var existingEntity = await DbContext .GetStockItemsByStockItemNameAsync(new StockItem { StockItemName = request.StockItemName }); if (existingEntity != null) { ModelState.AddModelError("StockItemName", "Stock item name already exists"); } if (!ModelState.IsValid) { return(BadRequest()); } // Create entity from request model var entity = request.ToEntity(); // Add entity to repository DbContext.Add(entity); // Save entity in database await DbContext.SaveChangesAsync(); // Set the entity to response model response.Model = entity; } catch (Exception ex) { response.DidError = true; response.ErrorMessage = "There was an internal error, please contact to technical support."; Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostStockItemAsync), ex); } return(response.ToHttpResponse()); }