public async Task <IActionResult> PutStockItemAsync(int id, [FromBody] PutStockItemsRequestModel requestModel) { Logger?.LogDebug("'{0}' has been invoked", nameof(PutStockItemAsync)); var response = new SingleResponse <StockItem>(); try { // Get stock item by id var entity = await Repository.GetStockItemsAsync(new StockItem(id)); // Validate if entity exists if (entity == null) { return(response.ToHttpResponse()); } // Set changes to entity entity.StockItemName = requestModel.StockItemName; entity.SupplierID = requestModel.SupplierID; entity.ColorID = requestModel.ColorID; entity.UnitPrice = requestModel.UnitPrice; // Update entity in repository Repository.Update(entity); // Save entity in database await Repository.CommitChangesAsync(); // 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(PutStockItemAsync), ex); } return(response.ToHttpResponse()); }
public async Task TestPutStockItemAsync() { // Arrange var repository = RepositoryMocker.GetWarehouseRepository(nameof(TestPutStockItemAsync)); var controller = new WarehouseController(null, repository); var id = 12; var requestModel = new PutStockItemsRequestModel { StockItemName = "USB food flash drive (Update)", SupplierID = 12, ColorID = 3 }; // Act var response = await controller.PutStockItemAsync(id, requestModel) as ObjectResult; var value = response.Value as ISingleResponse <StockItem>; repository.Dispose(); // Assert Assert.False(value.DidError); }