Exemplo n.º 1
0
        public async Task Put_Should_Return_BadRequest_On_Exception()
        {
            // Arrange
            UpdateCellarItem updateRequest = null;

            // Act
            var result = await _controller.Put(updateRequest);

            // Assert
            result.Should()
            .NotBeNull();

            result.Should()
            .BeOfType <BadRequestResult>();
        }
        public async Task <IActionResult> Put(UpdateCellarItem updateCellarItemRequest)
        {
            IActionResult result = null;

            try
            {
                var existingItem = await _context.CellarItems.FindAsync(updateCellarItemRequest.CellarItemId);

                if (existingItem != null)
                {
                    existingItem.DateModified    = DateTime.UtcNow;
                    existingItem.BeerContainerId = updateCellarItemRequest.BeerContainerId;
                    existingItem.BeerStyleId     = updateCellarItemRequest.BeerStyleId;
                    existingItem.ItemName        = updateCellarItemRequest.ItemName;
                    existingItem.Quantity        = updateCellarItemRequest.Quantity;
                    existingItem.YearProduced    = updateCellarItemRequest.YearProduced;
                    await _context.SaveChangesAsync();

                    var updatedItem = await _context.CellarItems
                                      .Include(_ => _.Style)
                                      .Include(_ => _.Container)
                                      .FirstAsync(_ => _.CellarItemId == updateCellarItemRequest.CellarItemId);

                    result = Ok(updatedItem);
                }
                else
                {
                    result = NotFound();
                }
            }
            catch (Exception e)
            {
                result = BadRequest();
            }

            return(result);
        }