コード例 #1
0
        public void AddProductAsyncShouldReturnProductCorrectly()
        {
            var options = new DbContextOptionsBuilder <IntillegioContext>()
                          .UseInMemoryDatabase(databaseName: "Add_Product_Db")
                          .Options;

            var dbContext = new IntillegioContext(options);

            var productBindingModel = new AdminProductBindingModel
            {
                Name         = "The Empire Strikes Back",
                Image255X325 = "https://pictures.abebooks.com/isbn/9780345400789-uk.jpg",
                Price        = 12.3M,
                Image135X135 = "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1328866630l/555314.jpg"
            };

            var mapper = new Mock <IMapper>();

            mapper.Setup(m => m.Map <Product>(productBindingModel))
            .Returns(new Product
            {
                Name         = "The Empire Strikes Back",
                Image255X325 = "https://pictures.abebooks.com/isbn/9780345400789-uk.jpg",
                Price        = 12.3M,
                Image135X135 = "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1328866630l/555314.jpg"
            });

            var service = new ShopService(dbContext, mapper.Object);

            service.AddProductAsync(productBindingModel);
            Assert.True(dbContext.Products.Any(n => n.Name == productBindingModel.Name));
            Assert.True(dbContext.Products.Any(a => a.Image135X135 == productBindingModel.Image135X135));
            Assert.True(dbContext.Products.Any(b => b.Price == productBindingModel.Price));
            Assert.True(dbContext.Products.Any(c => c.Image135X135 == productBindingModel.Image135X135));
        }
コード例 #2
0
        public async Task AddProductAsync(AdminProductBindingModel product)
        {
            var model = Mapper.Map <Product>(product);

            model.CategoryId = DbContext.Categories.FirstOrDefault(c => c.CategoryName == product.Category).Id;
            await DbContext.Products.AddAsync(model);

            await DbContext.SaveChangesAsync();
        }
コード例 #3
0
        public async Task <IActionResult> AddProduct(AdminProductBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            await _productsService.AddProductAsync(model);

            return(RedirectToAction("ProductsAdmin"));
        }