Example #1
0
        public async Task <ActionResult <WineDto> > CreateWineAsync(WineForCreationDto wineForCreationDto)
        {
            // TODO: Setup "REQUIRED" attributes on your entities to replace this code.
            // Maybe implement some custom "Validation Attributes" just for practice.
            //if (string.IsNullOrEmpty(wineForCreationDto.Name))
            //{
            //    var msg = "No Wine name found in request.";
            //    _logger.LogError(msg);
            //    return BadRequest(new { status = "Error:", message = msg });
            //}

            var wineEntity = _mapper.Map <Wine>(wineForCreationDto);

            var wineAdded = await _service.CreateWineAsync(wineEntity).ConfigureAwait(false);

            // Map the new wine to a "presentation wine" or DTO wine
            // Yeah, yeah, I could just update the Id on the wineForCreationDto that was passed
            // in and return that.  But, we should be consistent with the wine that is
            // presented to the client, always a WineDto.
            // And besides we  do all the mapping in one place, in the MAPPER.
            var wineDtoToReturn = _mapper.Map <WineDto>(wineAdded);

            wineDtoToReturn.Links = CreateLinksForWine(wineDtoToReturn);

            return(CreatedAtRoute(nameof(GetWineAsync), new { id = wineDtoToReturn.Id }, wineDtoToReturn));
        }
Example #2
0
        public async Task CreateWineAsync_WhenCalled_CreatesAWineDtoToReturnToClient()
        {
            // Arrange
            IWineRepository         wineService         = GetWineServiceStub();
            IWinePurchaseRepository winePurchaseService = GetWinePurchaseServiceStub();

            var response = Substitute.For <HttpResponse>();

            var httpContext = Substitute.For <HttpContext>();

            httpContext.Items.Add("response", response);

            var factory = new LoggerFactory();
            var logger  = factory.CreateLogger <WineController>();

            _controller = new WineController(logger, wineService, winePurchaseService, _mapper);
            _controller.ControllerContext.HttpContext = httpContext;
            _controller.Url = Substitute.For <IUrlHelper>();

            // Act
            var wineForCreationDto = new WineForCreationDto()
            {
                Name     = "Red Wine",
                Vineyard = "My Vineyard"
            };
            var actionResult = await _controller.CreateWineAsync(wineForCreationDto).ConfigureAwait(false);

            // Assert
            var result = Assert.IsType <CreatedAtRouteResult>(actionResult.Result);
            var wineDtoReturnedToClient = Assert.IsType <WineDto>(result.Value);

            Assert.Equal("Red Wine", wineDtoReturnedToClient.Name);
        }