//_____________________________________________________________________
 /// <summary>
 /// This method creates a new product in the database. It can be accessed only by an administrator.
 /// </summary>
 /// <param name="prod"></param>
 /// <returns></returns>
 public static string AddProductToDB(Product prod)
 {
     try
     {
         Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
         DbCommand CmdObj = db.GetStoredProcCommand("CreateProduct");
         db.AddInParameter(CmdObj, "@ProductName", DbType.String, prod.ProductName);
         db.AddInParameter(CmdObj, "@ProductDescription", DbType.String, prod.ProductDescription);
         db.AddInParameter(CmdObj, "@CategoryId", DbType.Int32, prod.CategoryId);
         db.AddInParameter(CmdObj, "@SubCategoryId", DbType.Int32, prod.SubCategoryId);
         db.AddInParameter(CmdObj, "@Price", DbType.Decimal, prod.Price);
         db.AddInParameter(CmdObj, "@UnitsInStock", DbType.Double, prod.UnitsInStock);
         db.AddInParameter(CmdObj, "@StockAvailability", DbType.Int32, prod.StockAvailability);
         db.AddInParameter(CmdObj, "@Colour", DbType.String, prod.Colour);
         db.AddInParameter(CmdObj, "@Size", DbType.String, prod.Size);
         db.AddInParameter(CmdObj, "@Picture", DbType.String, prod.Picture);
         db.AddOutParameter(CmdObj, "@strMessage", DbType.String, 255);
         db.ExecuteNonQuery(CmdObj);
         return db.GetParameterValue(CmdObj, "@strMessage").ToString();
     }
     catch (Exception ex)
     {
         bool rethrow = ExceptionPolicy.HandleException(ex, "Database Policy");
         if (rethrow) throw;
         string result = " Error in adding product to database.";
         return result;
     }
 }
        //______________________________________________________________________________________
        /// <summary>
        /// This method is used to get all the products details present in the cart of a particular user.
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List<AddToCartModel> GetFromCart(string userid)
        {
            List<AddToCartModel> listCartModel = new List<AddToCartModel>();

            Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
            DbCommand cmdObj = _db.GetStoredProcCommand("GetFromCart");
            _db.AddInParameter(cmdObj, "@UserId", DbType.String, userid);

            using (IDataReader dataReader = _db.ExecuteReader(cmdObj))
            {
                while (dataReader.Read())
                {
                    AddToCartModel cartmodel = new AddToCartModel();
                    Product prod = new Product();
                    Cart cart = new Cart();
                    prod.ProductId = Convert.ToInt32(dataReader["ProductId"]);
                    prod.ProductName = dataReader["ProductName"].ToString();
                    prod.ProductDescription = dataReader["ProductDescription"].ToString();
                    prod.Price = Double.Parse(dataReader["Price"].ToString());
                    cart.Quantity = Convert.ToInt32(dataReader["Quantity"]);
                    cartmodel.Product = prod;
                    cartmodel.Cart = cart;
                    listCartModel.Add(cartmodel);

                }
            }
            return listCartModel;
        }
 public void CreateProductTest()
 {
     Product product = new Product()
     {
         ProductName = "Fridge",
         ProductDescription = "home appliance",
         SubCategoryId = 3,
         Price = 6000,
         Colour = "red",
         Size = "165 lts",
         UnitsInStock = 5,
         StockAvailability = 1,
         Picture = null
     };
     string expected = "Product Created Successfully";
     string actual;
     actual = LetsShopImplementation.CreateProduct(product);
     Assert.AreEqual(expected, actual);
     //Assert.Inconclusive("Verify the correctness of this test method.");
 }
 public bool Save(Product target)
 {
     throw new System.NotImplementedException();
 }
        public ActionResult Delete(int id, Product product)
        {
            try
            {
                LetsShopImplementation.DeleteProduct(id);

                cache.Add("ProductData", LetsShopImplementation.GetProducts(),
                                   CacheItemPriority.High, null, absolutetime);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
                if (rethrow) throw;
                return View();
            }
        }
 //_____________________________________________________________________
 /// <summary>
 /// An administrator uses this method to update the details of an existing product in the database.
 /// </summary>
 /// <param name="product"></param>
 /// <returns></returns>
 public static string UpdateProduct(Product product)
 {
     try
     {
         string result;
         Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
         DbCommand cmdObj = _db.GetStoredProcCommand("UpdateProduct");
         _db.AddInParameter(cmdObj, "@ProductId", DbType.Int32, Convert.ToInt32(product.ProductId));
         _db.AddInParameter(cmdObj, "@ProductName", DbType.String, product.ProductName);
         _db.AddInParameter(cmdObj, "@ProductDescription", DbType.String, product.ProductDescription);
         _db.AddInParameter(cmdObj, "@CategoryId", DbType.Int32, product.CategoryId);
         _db.AddInParameter(cmdObj, "@SubCategoryId", DbType.Int32, product.SubCategoryId);
         _db.AddInParameter(cmdObj, "@Price", DbType.Double, product.Price);
         _db.AddInParameter(cmdObj, "@Colour", DbType.String, product.Colour);
         _db.AddInParameter(cmdObj, "@Size", DbType.String, product.Size);
         _db.AddInParameter(cmdObj, "@UnitsInStock", DbType.Double, product.UnitsInStock);
         _db.AddInParameter(cmdObj, "@StockAvailability", DbType.Double, product.StockAvailability);
         _db.AddInParameter(cmdObj, "@Picture", DbType.String, product.Picture);
         _db.AddOutParameter(cmdObj, "@strMessage", DbType.String, 255);
         _db.ExecuteNonQuery(cmdObj);
         result = _db.GetParameterValue(cmdObj, "@strMessage").ToString();
         return result;
     }
     catch (Exception ex)
     {
         bool rethrow = ExceptionPolicy.HandleException(ex, "Database Policy");
         if (rethrow) throw;
         string result = " Error in Updating Product.";
         return result;
     }
 }
 //_____________________________________________________________________
 /// <summary>
 /// This method is used to search for an existing product in the database by its name. (Can be accessed by all users.)
 /// </summary>
 /// <param name="ProductName"></param>
 /// <returns></returns>
 public static List<Product> ProductSearch(string ProductName)
 {
     var prodlist = new List<Product>();
     Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
     DbCommand cmdObj = _db.GetStoredProcCommand("SearchProduct");
     _db.AddInParameter(cmdObj, "@ProductName", DbType.String, ProductName);
     using (IDataReader dataReader = _db.ExecuteReader(cmdObj))
     {
         while (dataReader.Read())
         {
             var prod = new Product();
             prod.ProductId = Convert.ToInt32(dataReader["ProductId"].ToString());
             prod.ProductName = dataReader["ProductName"].ToString();
             prod.ProductDescription = dataReader["ProductDescription"].ToString();
             prod.Price = Convert.ToDouble(dataReader["Price"]);
             prod.UnitsInStock = Convert.ToInt32(dataReader["UnitsInStock"]);
             prod.StockAvailability = Convert.ToInt32(dataReader["StockAvailability"]);
             prod.Colour = dataReader["Colour"].ToString();
             prod.Size = dataReader["Size"].ToString();
             prod.Picture = dataReader["Picture"].ToString();
             prodlist.Add(prod);
         }
         return prodlist;
     }
 }
        //_____________________________________________________________________
        /// <summary>
        /// This method filters the price of the items between a specified range.
        /// </summary>
        /// <param name="CategoryId"></param>
        /// <param name="Max"></param>
        /// <param name="Min"></param>
        /// <returns></returns>
        public static List<Product> PriceFilter(string CategoryName, int Max, int Min)
        {
            var prodlist = new List<Product>();
            Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
            DbCommand cmdObj = _db.GetStoredProcCommand("PriceFilter");
            _db.AddInParameter(cmdObj, "@CategoryName", DbType.String, CategoryName);
            _db.AddInParameter(cmdObj, "@Max", DbType.Int32, Convert.ToInt32(Max));
            _db.AddInParameter(cmdObj, "@Min", DbType.Int32, Convert.ToInt32(Min));
            _db.AddOutParameter(cmdObj, "@Message", DbType.String, 255);

            using (IDataReader dataReader = _db.ExecuteReader(cmdObj))
            {
                while (dataReader.Read())
                {
                    var prod = new Product();
                    prod.ProductId = Convert.ToInt32(dataReader["ProductId"]);
                    prod.ProductName = dataReader["ProductName"].ToString();
                    prod.ProductDescription = dataReader["ProductDescription"].ToString();
                    prod.CategoryId = Convert.ToInt32(dataReader["CategoryId"]);
                    prod.SubCategoryId = Convert.ToInt32(dataReader["SubCategoryId"]);
                    prod.Price = Convert.ToInt64(dataReader["Price"]);
                    prod.UnitsInStock = Convert.ToInt32(dataReader["UnitsInStock"]);
                    prod.StockAvailability = Convert.ToInt32(dataReader["StockAvailability"]);
                    prod.Colour = dataReader["Colour"].ToString();
                    prod.Size = dataReader["Size"].ToString();
                    prod.Picture = dataReader["Picture"].ToString();

                    prodlist.Add(prod);
                }
                return prodlist;
            }
        }
        //_____________________________________________________________________
        /// <summary>
        /// Gets the details of the particular product through its ProductId.
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public static ProductCategoryRelation GetProduct(int productid)
        {
            ProductCategoryRelation pc = new ProductCategoryRelation();
            Product prod = new Product();
            Categories cat = new Categories();

            prod.ProductId = productid;
            Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
            DbCommand cmdObj = _db.GetStoredProcCommand("GetProductById");
            _db.AddInParameter(cmdObj, "@ProductId", DbType.Int32, Convert.ToInt32(productid));

            using (IDataReader dataReader = _db.ExecuteReader(cmdObj))
            {
                while (dataReader.Read())
                {
                    prod.ProductId = Convert.ToInt32(dataReader["ProductId"]);
                    prod.ProductName = dataReader["ProductName"].ToString();
                    prod.ProductDescription = dataReader["ProductDescription"].ToString();
                    prod.CategoryId = Convert.ToInt32(dataReader["CategoryId"]);
                    prod.SubCategoryId = Convert.ToInt32(dataReader["SubCategoryId"]);
                    prod.Price = Double.Parse(dataReader["Price"].ToString());
                    prod.UnitsInStock = Convert.ToInt32(dataReader["UnitsInStock"]);
                    prod.StockAvailability = Convert.ToInt32(dataReader["StockAvailability"]);
                    prod.Colour = dataReader["Colour"].ToString();
                    prod.Size = dataReader["Size"].ToString();
                    prod.Picture = dataReader["Picture"].ToString();
                }
            }

            DbCommand cmdObj2 = _db.GetStoredProcCommand("GetCategoryNameById");
            _db.AddInParameter(cmdObj2, "@CategoryId", DbType.Int32, Convert.ToInt32(prod.CategoryId));
            _db.AddInParameter(cmdObj2, "@SubCategoryId", DbType.Int32, Convert.ToInt32(prod.SubCategoryId));
            using (IDataReader dataReader = _db.ExecuteReader(cmdObj2))
            {
                while (dataReader.Read())
                {
                    cat.CategoryName = dataReader["CategoryName"].ToString();
                    cat.SubCategoryName = dataReader["SubCategoryName"].ToString();
                }
            }
            pc.product = prod;
            pc.categories = cat;

            return pc;
        }
        //_____________________________________________________________________
        /// <summary>
        /// This method is used to get full information of a particular order, and helps an user track his order.
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public static OrderInformation GetOrderDetails(int orderid)
        {
            Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString");
            DbCommand cmdObj = _db.GetStoredProcCommand("GetOrderDetails1");  // for the list of products
            _db.AddInParameter(cmdObj, "@OrderId", DbType.Int32, orderid);

            var orderslist = new List<OrderDetails>();

            using (IDataReader dataReader = _db.ExecuteReader(cmdObj))
            {
                while (dataReader.Read())
                {
                    OrderDetails od = new OrderDetails();
                    Product prod = new Product();
                    od.quantity = Convert.ToInt32(dataReader["Quantity"]);
                    prod.ProductId = Convert.ToInt32(dataReader["ProductId"]);

                    DbCommand cmdObj2 = _db.GetStoredProcCommand("GetProductById");
                    _db.AddInParameter(cmdObj2, "@ProductId", DbType.Int32, Convert.ToInt32(prod.ProductId));

                    using (IDataReader dataReader2 = _db.ExecuteReader(cmdObj2))
                    {
                        while (dataReader2.Read())
                        {
                            prod.ProductName = dataReader2["ProductName"].ToString();
                            prod.Price = Double.Parse(dataReader2["Price"].ToString());
                        }
                    }
                    od.product = prod;
                    orderslist.Add(od);
                }
            }

            OrderInformation oi = new OrderInformation();
            Orders ord = new Orders();

            DbCommand cmdObj3 = _db.GetStoredProcCommand("GetOrderDetails2");  // for the user's information
            _db.AddInParameter(cmdObj3, "@OrderId", DbType.Int32, orderid);

            using (IDataReader dataReader3 = _db.ExecuteReader(cmdObj3))
            {
                while (dataReader3.Read())
                {
                    ord.OrderId = Convert.ToInt32(dataReader3["OrderId"].ToString());
                    ord.UserId = dataReader3["UserId"].ToString();
                    ord.OrderDate = Convert.ToDateTime(dataReader3["OrderDate"]);
                    ord.ShippingDate = Convert.ToDateTime(dataReader3["ShippingDate"]);
                    ord.TransactionStatus = dataReader3["TransactionStatus"].ToString();
                    ord.PaymentDate = Convert.ToDateTime(dataReader3["PaymentDate"]);
                    ord.TotalAmount = Convert.ToDouble(dataReader3["TotalAmount"]);
                    ord.BillingAddress = dataReader3["BillingAddress"].ToString();
                    ord.ShippingAddress = dataReader3["ShippingAddress"].ToString();
                    ord.PhoneNumber = Convert.ToInt64(dataReader3["PhoneNumber"]);
                    ord.EmailId = dataReader3["EmailId"].ToString();

                }
            }

            oi.orderproduct = orderslist;
            oi.orderuser = ord;

            return oi;
        }
 //______________________________________________________________________________________
 /// <summary>
 /// An administrator uses this method to update the details of an existing product in the database.
 /// </summary>
 /// <param name="product"></param>
 /// <returns></returns>
 public static string UpdateProduct(Product product)
 {
     return DataProvider.UpdateProduct(product);
 }
 //______________________________________________________________________________________
 /// <summary>
 /// This method creates a new product in the database. It can be accessed only by an administrator.
 /// </summary>
 /// <param name="product"></param>
 /// <returns></returns>
 public static string CreateProduct(Product product)
 {
     return DataProvider.AddProductToDB(product);
 }
 public void UpdateProductTest()
 {
     Product product = new Product()
     {
         ProductId = 8,
         ProductName = "Fan",
         ProductDescription = "home appliance",
         SubCategoryId = 3,
         Price = 6000,
         //QuantityPerUnit = 4,
         Colour = "red",
         Size = "165 lts",
         //Discount = 500,
         UnitsInStock = 5,
         StockAvailability = 1,
         Picture = null
     }; // TODO: Initialize to an appropriate value
     string expected = "Product Updated Successfully"; // TODO: Initialize to an appropriate value
     string actual;
     actual = LetsShopImplementation.UpdateProduct(product);
     Assert.AreEqual(expected, actual);
     //Assert.Inconclusive("Verify the correctness of this test method.");
 }
        public void NegativeDeleteProductTest()
        {
            Product product = new Product(); // TODO: Initialize to an appropriate value

            int id = 56;

            string expected = "Product Id doesnot exists"; // TODO: Initialize to an appropriate value
            string actual;
            actual = LetsShopImplementation.DeleteProduct(id);
            Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }
        public void CanInsertProduct()
        {
            // Create a new product, not I do not supply an id
            Product newProduct = new Product { ProductName = "asp.net MVC3", ProductDescription = "Book", Price = 399.99 };

            int productCount = this.MockProductsRepository.FindAll().Count;
            Assert.AreEqual(3, productCount); // Verify the expected Number pre-insert

            // try saving our new product
            this.MockProductsRepository.Save(newProduct);

            // demand a recount
            productCount = this.MockProductsRepository.FindAll().Count;
            Assert.AreEqual(4, productCount); // Verify the expected Number post-insert

            // verify that our new product has been saved
            Product testProduct = this.MockProductsRepository.FindByName("asp.net MVC3");
            Assert.IsNotNull(testProduct); // Test if null
            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type
            Assert.AreEqual(5, testProduct.ProductId); // Verify it has the expected productid
        }