Ejemplo n.º 1
0
        public ActionResult AddProduct(Product p)
        {
            ViewBag.ProductsActive = "active";

            if (ModelState.IsValid)
            {
                ProductRepository.SaveProduct(p);
                return(RedirectToAction("Products", "Admin"));
            }
            else
            {
                return(View(p));
            }
        }
        public void Editing_Product_Exist()
        {
            //arrange
            //var products = new Product[]
            //{
            //    new Product {Id = 1 , Name = "P1"},
            //    new Product {Id = 2 , Name = "P2"},
            //    new Product {Id = 3 , Name = "P3"},
            //}.AsQueryable<Product>();
            //var productsMock = new Mock<DbSet<Product>>();
            //productsMock.Setup(x => x.AddRange(products));
            //var options = new Mock<DbContextOptions<AppDbContext>>();


            //var appContextMock = new Mock<AppDbContext>(options.Object) ;
            //appContextMock.Setup(x => x.Set<Product>()).Returns(productsMock.Object);


            //appContextMock.Setup(x => x.Products).Returns(productsMock.Object);
            //var eFProductRepository = new EFProductRepository(appContextMock.Object);

            //eFProductRepository.SaveProduct(new Product { Id = 1, Name = "zmienione" });
            //var result = eFProductRepository.Products.FirstOrDefault(m => m.Id == 1);
            //Assert.Equal("zmienione", result.Name);


            var builder = new DbContextOptionsBuilder <AppDbContext>().UseInMemoryDatabase(databaseName: "C:/USERS/DYMION/SOURCE/REPOS/SPORT STORE/SPORT STORE.TEST/TESTSDATABASE.MDF").Options;

            using (var context = new AppDbContext(builder))
            {
                var ef       = new EFProductRepository(context);
                var products = new Product[]
                {
                    new Product {
                        Id = 1, Name = "P1"
                    },
                    new Product {
                        Id = 2, Name = "P2"
                    },
                    new Product {
                        Id = 3, Name = "P3"
                    },
                }.AsQueryable <Product>();

                context.Products.AddRange(products);
                context.SaveChanges();
                ef.SaveProduct(new Product {
                    Id = 1, Name = "zmienione"
                });
                var result = ef.Products.FirstOrDefault(m => m.Id == 1);
                Assert.Equal("zmienione", result.Name);
            }
            //var mock = MockDbSetup.MockAppDbContext();
            //var ef = new EFProductRepository(mock.Object);
            //ef.SaveProduct(new Product { Id = 1, Name = "zmienione" });
            //var result = ef.Products.FirstOrDefault(m => m.Id == 1);
            //Assert.Equal("zmienione", result.Name);
        }
        public void SaveProductTest()
        {
            // Arrange - create a repository
            EFProductRepository target = new EFProductRepository();

            // Act - change the category of the first product
            Product prod = target.Products.First();

            prod.Category = "Test";
            target.SaveProduct(prod);
        }
Ejemplo n.º 4
0
 public ActionResult Edit(Product product, HttpPostedFileBase image = null)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             product.ImageMimeType = image.ContentType;
             product.ImageData     = new byte[image.ContentLength];
             image.InputStream.Read(product.ImageData, 0, image.ContentLength);
         }
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0}. Дані успішно збережено", product.Name);
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View(product));
     }
 }
Ejemplo n.º 5
0
 public ActionResult Edit(Product product, HttpPostedFileBase image = null)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             product.ImageMimeType = image.ContentType;
             product.ImageData     = new byte[image.ContentLength];
             image.InputStream.Read(product.ImageData, 0, image.ContentLength);
         }
         repository.SaveProduct(product);
         TempData["message"] = string.Format("{0} has been saved", product.Name);
         return(RedirectToAction("Index"));
     }
     else
     {
         // there is something wrong with the data values
         return(View(product));
     }
 }
Ejemplo n.º 6
0
 public Product Post(Product product)
 {
     _EFProductRepository.SaveProduct(product);
     return(product);
 }