Ejemplo n.º 1
0
        public async Task <IActionResult> CreateProductOption(string Id, [FromBody] ProductOptionForCreationDto productOptionForCreation)
        {
            Guid newGuid;
            var  isGuid = Guid.TryParse(Id, out newGuid);

            if (!isGuid)
            {
                return(BadRequest("Invalid Id provided."));
            }

            if (productOptionForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var product = await repository.FindProductById(newGuid);

            if (product == null)
            {
                return(NotFound());
            }

            var productOption = Mapper.Map <Entities.ProductOption>(productOptionForCreation);

            productOption.Id        = Guid.NewGuid();
            productOption.ProductId = newGuid;

            repository.AddProductOption(productOption);
            if (!await repository.Save())
            {
                logger.LogCritical($"Error when creating Product Option. Product.Id:{product.Id} | Product.Name: {product.Name} | Product.OptionName:{productOption.Name}");
                return(StatusCode(500, $"Error when creating Product Option. Product.Id:{product.Id} | Product.Name: {product.Name} | Product.OptionName:{productOption.Name}"));
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        public async void CreateProductOption_Correct_Product_And_Correct_ProductOptions_Returns_No_Content()
        {
            // Arrange
            Product product = TestProduct;
            string  guid    = Guid.NewGuid().ToString();

            repository.Setup(r => r.FindProductById(It.IsAny <Guid>()))
            .ReturnsAsync(product);
            repository.Setup(r => r.Save())
            .ReturnsAsync(true);

            var productOptionCreation = new ProductOptionForCreationDto
            {
                Name        = "New Product Option",
                Description = "New Product Description"
            };

            // Act
            var result = await sut.CreateProductOption(guid, productOptionCreation);

            // Assert
            Assert.IsType <NoContentResult>(result);
        }