Example #1
0
 //This action add to product to the Products table in the databse in the case it is not available in the store(disabled mode in the Products table in the database or not exist in the database, else(enabled mode in the Products table in the database or exist in this table will a message appear to the user and will redirected to the managament page.
 public ActionResult AddToDatabase(Product product)
 {
     //Checks if the form in the view(html page) is valid: required fields, product id(such as A123), price(float or int such as: 1.4,4) and amount(only integer numbers).
     if (ModelState.IsValid)
     {
         //Checks if the products exist in the store(enable mode in the pid of product that user entered).
         bool exist = dalp.Products.Any(p => p.PID == product.PID && p.mode == true);
         //If the product not exist in the Products table of database or mode of product is false.
         if (!exist.Equals(true))
         {
             //Gets the list of products that their mode are false(deleted from the store).
             List <Product> notactive = (from p in dalp.Products where p.PID == product.PID && p.mode == false select p).ToList();
             //If the mode of specific product that user entered is false.
             if (notactive.Count != 0)
             {
                 //Gets the specific product that user entered in the form by PID(key in the Products table in the database).
                 Product producttou = dalp.Products.Find(product.PID);
                 //Changes mode of product to enable(Product is active in the store).
                 producttou.mode = true;
                 //Changes the name of disabled product to the new name of active product(identify by PID).
                 producttou.namep = product.namep;
                 //Changes the price of disabled product to the new name of active product(identify by PID).
                 producttou.price = product.price;
                 // //Changes the amount of disabled product to the new amount of active product(identify by PID)
                 producttou.amount = product.amount;
             }
             //If the product not available in the database add new product and mark it as enabled(active in the store).
             else
             {
                 //Changes the mode of product that user entered to enable(active in store)
                 product.mode = true;
                 //Adds the product to the database.
                 dalp.Products.Add(product);
             }
             //Saves the new Product object in the databse as enabled(active in the store).
             dalp.SaveChanges();
             //Saves the message that will displayed to the user in the view(in the case of the adding product is perforemed successfully).
             TempData["msg"] = "The product added successfully";
             //Returns page of adding product to the store.
             return(View("AddProduct"));
         }
         //If the product available in the store(marked as enabled)
         else
         {
             //Saves the message that will displayed to the user in the view(in the case of the adding product is failed).
             TempData["msg"] = "The product already exist in the store";
             return(View("AddProduct"));
         }
     }
     //If the validation is field( in the case of server validation,for clients that cannot run java script in their webrowser )
     return(View("Management"));
 }
Example #2
0
        //This action Add order to the Orders table
        public ActionResult AddOrderToDatabase(Order order)
        {
            //Checks if the data that user send to the server are valid: amount(whole number) and name of the customer(valid by regular expression).
            if (ModelState.IsValidField("namec") && ModelState.IsValidField("amount"))
            {
                //Gets list of all the product from the Orders Table
                List <Product> products = dalp.Products.ToList();
                foreach (Product product in products)
                {
                    //Check if PID that user entered same to the PID of available in the Orders table in the database
                    if (product.PID == order.PID)
                    {
                        //Checks if have enough items accorsding to the request of the user.
                        if (product.amount - order.amount < 0)
                        {
                            //Saves the message that will displayed to the user in the view(in the case of not have enought items from the specific product that user request in the store.)
                            TempData["msg"] = "There are not enough products in the stock";
                            //Returns view of buying products in the store.
                            return(View("Buy"));
                        }
                        //Updates the amount in the store(updates the amount of specific product in the store).
                        product.amount = product.amount - order.amount;
                        //Updates the current date of purchase the product by the user.
                        order.date = DateTime.Today;
                        //Marks the order as active in the store.
                        order.mode = true;
                        break;
                    }
                }

                //Add order with details of the user to the Orders table
                dalo.Orders.Add(order);
                //Save update of the amount of specific product that user ordered.
                dalp.SaveChanges();
                //Save the adding order according the requst of the user to the Orders table.
                dalo.SaveChanges();
                //Saves the message that will displayed to the user in the view(in the case of  purchase product performed successfully).
                TempData["msg"] = "Buying is successful";
                //Returns view of buying products.
                return(View("Buy"));
            }
            //If the validation is field( in the case of server validation,for clients that cannot run java script in their webrowser )
            return(View("Buy"));
        }
        public ActionResult updateProduct(Product p)
        {
            //Update product into the data base (no need to make more checks cause of data annotation)
            ProductsDal    pDal        = new ProductsDal();
            List <Product> objProducts = pDal.Products.ToList();

            p.pid = 1000;
            try
            {
                pDal.Products.Add(p);
                pDal.SaveChanges();
            }
            catch
            {
            }

            Session["Error"] = "The product have been added successfuly!";
            return(RedirectToAction("ViewProducts"));
        }
Example #4
0
        //Action that Searches products and returns a list of requested products.
        public ActionResult ProductAction()
        {
            TempData["ProductErr"] = null;
            ProductsDal      dal = new ProductsDal();
            ProductViewModel pvm = new ProductViewModel();

            pvm.product  = new Product();
            pvm.products = dal.products.ToList <Product>();
            if (Request.Form["name"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            string newName = Request.Form["name"].ToString();

            if (newName == "")
            {
                TempData["ProductErr"] = "Please fill product name.";
                return(View(pvm));
            }
            double  newPrice   = double.Parse(Request.Form["price"].ToString());
            Product objProduct = new Product();

            objProduct.name  = newName;
            objProduct.price = newPrice;
            pvm.product      = objProduct;
            try
            {
                dal.products.Add(objProduct);
                dal.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                TempData["ProductErr"] = "Product already exists.";
                return(View(pvm));
            }

            List <Product> objProducts = (from x in dal.products
                                          select x).ToList <Product>();

            pvm.product  = objProduct;
            pvm.products = objProducts;
            return(View(pvm));
        }
Example #5
0
        public ActionResult Submit()//action result for submiiting an add product form (only admin)
        {
            Products    productobj = new Products();
            ProductsDal dal        = new ProductsDal();

            productobj.product_name = Request.Form["product.product_name"];
            productobj.img_url      = Request.Form["product.img_url"];
            productobj.price        = Convert.ToInt32(Request.Form["product.price"]);
            productobj.description  = Request.Form["product.description"];
            productobj.type         = Request.Form["product.type"];
            if (ModelState.IsValid)
            {
                dal.products.Add(productobj);
                try
                {
                    dal.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    //handle exception
                }
            }
            return(Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri));// returns the calling view (add products view)
        }