Beispiel #1
0
        public async Task AddNewProductFromInputModel(AddNewProductBindingModel input)
        {
            var productToAdd = this.mapper.Map <Product>(input);

            await this.context.Products.AddAsync(productToAdd);

            await this.context.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task <IActionResult> ProductAddNew(AddNewProductBindingModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(await this.ProductAddNew());
            }

            await this.productService.AddNewProductFromInputModel(input);

            return(this.Redirect("/Admin/ProductOrder"));
        }
        public async Task AddNewProductFromInputModelTests()
        {
            //Arrange
            var mapperConfig = new MapperConfiguration(x => x.AddProfile(new MappingProfile()));
            var mapper       = mapperConfig.CreateMapper();

            var productService = new ProductService(this.context, mapper);

            var firstProduct = new AddNewProductBindingModel
            {
                CategoryIdString = "CategoryId",
                Description      = "Some Description",
                Name             = "FirstProductName",
                Price            = 12.2m,
                ProductUrl       = "URL"
            };

            var secondProduct = new AddNewProductBindingModel
            {
                CategoryIdString = "CategoryId",
                Description      = "Some Description",
                Name             = "SecondProductName",
                Price            = 22.2m,
                ProductUrl       = "URL"
            };

            //Act
            Assert.Equal(0, this.context.Products.Count());

            await productService.AddNewProductFromInputModel(firstProduct);

            Assert.Equal(1, this.context.Products.Count());

            Assert.Equal("FirstProductName", this.context.Products.First().Name);

            await productService.AddNewProductFromInputModel(secondProduct);

            //Assert
            Assert.Equal(2, this.context.Products.Count());
        }