public bool Edit(Guid productId, [FromBody] ProductInputData editData)
 {
     if (editData.Quantity >= 0 && editData.ProductName != null)
     {
         return(product.EditProduct(productId, editData));
     }
     throw new Exception("Please enter the Product Name and Quantity should be greater than or equal to 0");
 }
 public bool Create([FromBody] ProductInputData inputData)
 {
     if (inputData.Quantity >= 0 && inputData.ProductName != null)
     {
         return(product.AddProduct(inputData));
     }
     throw new Exception("Please enter the Product Name and Quantity should be greater than or equal to 0");
 }
Example #3
0
        public void TestDeleteProductExpectException()
        {
            var product = new ProductInputData()
            {
                ProductId   = Guid.NewGuid(),
                ProductName = "Product for test",
                Quantity    = 10
            };

            Assert.Throws <Exception>(() => objProductService.DeleteProduct(product.ProductId));
        }
        public bool AddProduct(ProductInputData productInputData)
        {
            var pro = dbContext.Products.FirstOrDefault(x => x.ProductName == productInputData.ProductName);

            if (pro != null)
            {
                throw new Exception("Product name already exists");
            }
            var product = new Product()
            {
                ProductId   = Guid.NewGuid(),
                ProductName = productInputData.ProductName,
                Quantity    = productInputData.Quantity
            };

            dbContext.Products.Add(product);
            dbContext.SaveChanges();
            return(true);
        }
        public bool EditProduct(Guid productId, ProductInputData updatedInputData)
        {
            var product = dbContext.Products.FirstOrDefault(x => x.ProductId == productId);

            if (product == null)
            {
                throw new Exception("Invalid Product Id");
            }

            var pro = dbContext.Products.FirstOrDefault(x => x.ProductName == updatedInputData.ProductName);

            if (pro != null)
            {
                throw new Exception("Product name already exists");
            }

            product.Quantity    = (updatedInputData.Quantity != 0) ? updatedInputData.Quantity : product.Quantity;
            product.ProductName = (updatedInputData.ProductName != null) ? updatedInputData.ProductName : product.ProductName;
            dbContext.Products.Update(product);
            dbContext.SaveChanges();
            return(true);
        }