public ActionResult Create(CreateProductViewModel productViewModel)
        {
            try
            {
                if (productViewModel.File != null && productViewModel.File.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(productViewModel.File.FileName);
                    var path     = Path.Combine(Server.MapPath("~/Images"), fileName);
                    productViewModel.File.SaveAs(path);
                    productViewModel.Photo      = fileName;
                    createProduct.InputArgument = new Product {
                        Code = productViewModel.Code, Name = productViewModel.Name, Price = productViewModel.Price, Photo = productViewModel.File.FileName
                    };
                    createProduct.Execute();
                    CommitDatabaseChanges.Commit();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("File", "Please upload a photo for the product.");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return(View());
        }
 public ActionResult Delete(int id, Product prodoct)
 {
     getProduct.InputArgument = p => p.Id == id;
     getProduct.Execute();
     removeProduct.InputArgument = getProduct.OutputArgument.Single();
     removeProduct.Execute();
     CommitDatabaseChanges.Commit();
     return(RedirectToAction("Index"));
 }
Пример #3
0
        public void ShouldRemoveProduct()
        {
            var createdProduct = CreateNewProduct();

            CommitDatabaseChanges.Commit();
            RemoveProduct removeProdct = new RemoveProduct();

            removeProdct.InputArgument = createdProduct;
            removeProdct.Execute();
            CommitDatabaseChanges.Commit();
            Assert.IsTrue(removeProdct.OutputArgument);
        }
        // DELETE api/product/5
        public void Delete(int id)
        {
            this.getProduct.InputArgument = p => p.Id == id;
            this.getProduct.Execute();
            var retrievedProduct = getProduct.OutputArgument.Single();

            if (retrievedProduct != null)
            {
                this.removeProduct.InputArgument = retrievedProduct;
                this.removeProduct.Execute();
                CommitDatabaseChanges.Commit();
            }
        }
Пример #5
0
        public void ShouldUpdateExistentProduct()
        {
            var           createdProduct = CreateNewProduct();
            UpdateProduct updateProduct  = new UpdateProduct();

            createdProduct.Name         = "Updated";
            updateProduct.InputArgument = createdProduct;
            updateProduct.Execute();
            CommitDatabaseChanges.Commit();
            GetProduct getProduct = new GetProduct();

            getProduct.InputArgument = p => p.Name == "Updated";
            getProduct.Execute();
            Assert.IsTrue(getProduct.OutputArgument.Count > 0);
        }
Пример #6
0
        public void CleanChanges()
        {
            GetProduct getProduct = new GetProduct();

            getProduct.InputArgument = p => p.Code.Contains("product code");
            getProduct.Execute();
            if (getProduct.OutputArgument != null)
            {
                foreach (var item in getProduct.OutputArgument)
                {
                    RemoveProduct removeProduct = new RemoveProduct();
                    removeProduct.InputArgument = item;
                    removeProduct.Execute();
                }
            }
            CommitDatabaseChanges.Commit();
        }
        // PUT api/product/5
        public void Put(int id, [FromBody] Product product)
        {
            this.getProduct.InputArgument = p => p.Id == id;
            this.getProduct.Execute();
            var retrievedProduct = getProduct.OutputArgument.Single();

            if (retrievedProduct != null)
            {
                retrievedProduct.Code            = product.Code;
                retrievedProduct.Name            = product.Name;
                retrievedProduct.Photo           = product.Photo;
                retrievedProduct.Price           = product.Price;
                this.updateProduct.InputArgument = retrievedProduct;
                this.updateProduct.Execute();
                CommitDatabaseChanges.Commit();
            }
        }
Пример #8
0
        public void ShouldPreventUpdateWithInconvenientPrice()
        {
            var createdProduct = CreateNewProduct("product code 1");

            CommitDatabaseChanges.Commit();
            UpdateProduct updateProduct = new UpdateProduct();

            createdProduct.Price        = 0;
            updateProduct.InputArgument = createdProduct;
            try
            {
                updateProduct.Execute();
                Assert.Fail("Product price should be greater than zero.");
            }
            catch (ProductPriceValidationException)
            {
                Assert.Pass();
            }
        }
 public ActionResult Edit(int id, CreateProductViewModel product)
 {
     try
     {
         getProduct.InputArgument = p => p.Id == id;
         getProduct.Execute();
         var retrievedProduct = getProduct.OutputArgument.Single();
         retrievedProduct.Code       = product.Code;
         retrievedProduct.Name       = product.Name;
         retrievedProduct.Price      = product.Price;
         updateProduct.InputArgument = retrievedProduct;
         updateProduct.Execute();
         CommitDatabaseChanges.Commit();
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message);
     }
     return(View());
 }
Пример #10
0
        public void ShouldPreventUpdateWithSameProductCode()
        {
            var createdProduct = CreateNewProduct();

            CommitDatabaseChanges.Commit();
            var createAnotherProduct = CreateNewProduct("product code 2");

            CommitDatabaseChanges.Commit();
            createAnotherProduct.Code = "product code 1";
            UpdateProduct updateProduct = new UpdateProduct();

            updateProduct.InputArgument = createAnotherProduct;
            try
            {
                updateProduct.Execute();
                Assert.Fail("Existed product code cannot be updated.");
            }
            catch (DuplicateProductException)
            {
                Assert.Pass();
            }
        }
Пример #11
0
 public void ShouldPreventCreateProductWithSameProductCode()
 {
     try
     {
         CreateProduct createProduct = new CreateProduct();
         Product       product1      = new Product {
             Code = "product code 1", Name = "product name 1", Price = 111
         };
         createProduct.InputArgument = product1;
         createProduct.Execute();
         CommitDatabaseChanges.Commit();
         Product product2 = new Product {
             Code = "product code 1", Name = "Should be prevented", Price = 34
         };
         createProduct.InputArgument = product2;
         createProduct.Execute();
         Assert.Fail("There is something wrong! It shouldn't create a product with same product code.");
     }
     catch (DuplicateProductException)
     {
         Assert.Pass();
     }
 }
 public void Create([FromBody] Product product)
 {
     this.createProduct.InputArgument = product;
     this.createProduct.Execute();
     CommitDatabaseChanges.Commit();
 }