Example #1
0
        public async Task <IActionResult> PutStockItemAsync(int id, [FromBody] PutStockItemsRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked",
                             nameof(PutStockItemAsync));
            var response = new Response();

            try
            {
                // Get stock item by id
                var entity = await DbContext.GetStockItemsAsync(new
                                                                StockItem(id));

                // Validate if entity exists
                if (entity == null)
                {
                    return(NotFound());
                }
                // Set changes to entity
                entity.StockItemName = request.StockItemName;
                entity.SupplierID    = request.SupplierID;
                entity.ColorID       = request.ColorID;
                entity.UnitPrice     = request.UnitPrice;
                // Update entity in repository
                DbContext.Update(entity);
                // Save entity in database
                await DbContext.SaveChangesAsync();
            }
            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}'", nameof(PutStockItemAsync), ex);
            }
            return(response.ToHttpResponse());
        }
Example #2
0
        public async Task TestPutStockItemAsyncRequired()
        {
            // Arrange
            var dbContext  = DbContextMocker.GetWideWorldImportersDbContext(nameof(TestPutStockItemAsyncRequired));
            var controller = new WarehouseController(null, dbContext);
            var id         = 12;
            var request    = new PutStockItemsRequest
            {
                StockItemName = "USB food flash drive (Update)",
                SupplierID    = null,
                ColorID       = 12,
                UnitPrice     = 3
            };

            // Act
            var response = await controller.PutStockItemAsync(id, request) as ObjectResult;

            var value = response.Value as IResponse;

            dbContext.Dispose();

            // Assert
            Assert.False(value.DidError);
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(value);

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }
        public async Task TestPutStockItemAsync()
        {
            // Arrange
            var repository   = RepositoryMocker.GetWarehouseRepository(nameof(TestPutStockItemAsync));
            var controller   = new WarehouseController(null, repository);
            var id           = 12;
            var requestModel = new PutStockItemsRequest
            {
                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 IResponse;

            repository.Dispose();

            // Assert
            Assert.False(value.DidError);
        }
        public async Task TestPutStockItemAsync()
        {
            // Arrange
            var dbContext  = DbContextMocker.GetWideWorldImportersDbContext(nameof(TestPutStockItemAsync));
            var controller = new WebAPIController(null, dbContext);
            var id         = 12;
            var request    = new PutStockItemsRequest
            {
                StockItemName = "USB food flash drive (Update)",
                SupplierID    = 12,
                ColorID       = 3
            };

            // Act
            var response = await controller.PutStockItemAsync(id, request) as ObjectResult;

            var value = response.Value as IResponse;

            dbContext.Dispose();

            // Assert
            Assert.False(value.DidError);
        }
        public async Task <IActionResult> PutStockItemAsync(int id, [FromBody] PutStockItemsRequest request)
        {
            Logger?.LogDebug("'{0}' foi chamada", nameof(PutStockItemAsync));

            var response = new Response();

            try
            {
                // Get do produto pelo id
                var entity = await _context.GetStockItemsByStockItemNameAsync(new StockItem(id));

                // Valida existente
                if (entity == null)
                {
                    return(NotFound());
                }

                // Set do update
                entity.StockItemName = request.StockItemName;
                entity.FornecedorID  = request.FornecedorID;
                entity.ColorID       = request.ColorID;
                entity.PrecoUnidade  = request.PrecoUnidade;

                // Update e save no BD
                _context.Update(entity);
                await _context.SaveChangesAsync();
            }
            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(PutStockItemAsync), ex);
            }

            return(response.ToHttpResponse());
        }