//This action deletes product from the Products table in the database. public ActionResult DeleteFromDatabse() { //Initializes new Product object in order to get PID from form. Product product = new Product(); //Inserts PID of the product that user entered in the form of delete product. product.PID = Request.Form["product.PID"]; //Checks if the form in the view(html page) is valid: required fields, product id(such as A123). if (ModelState.IsValid) { //Checks if the products exist in the store(enable mode in the pid of product that user entered). List <Product> products = dalp.Products.Where(p => p.PID == product.PID && p.mode == true).ToList(); //Checks if have products in the Products table in the database. if (products.Count != 0) { //Gets the specific product that user entered in the form by PID(key in the Products table in the database). Product producttod = dalp.Products.Find(product.PID); //Gets the orders of the product by name of the product. List <Order> orderstod = (from order in dalo.Orders where order.namep == producttod.namep select order).ToList(); //Checks if exist orders of product that user interested to delete from the store if (orderstod != null) { //Deletes the orders of the specific product that user interested to delete foreach (Order order in orderstod) { //Change mode to disabled each order of sprecific product that user interested to delete(delete from the store). order.mode = false; } } //Changes mode of product to disabled(delete from the store). producttod.mode = false; //Saves changes of delete product in the Products table. dalp.SaveChanges(); // //Saves changes of delete orders of product in the Orders table. dalo.SaveChanges(); //Initializes new Product Model in ProductViewModel Object to display the user new Delete form to delete other product from the store. pvm.product = new Product(); } //If the user try to delete product that not exist in the store. else { //Initializes Product Model with data of the user entered(invalid data) to view to user the invalid data that typed in the form. pvm.product = product; //Saves the message that will displayed to the user in the view(in the case of that product not available in the store). TempData["msg"] = "The product not exist in the store"; } } //If the form not passed validation( in the case of the user not run javascript in your webrowser). else { //Initializes Product Model with the data that user entered in the form in previous request. pvm.product = product; } //Initializes list of products on order to view to user the products that available in the store. pvm.products = dalp.Products.ToList(); // ////Returns page of delete product from the store. return(View("Delete", pvm)); }
public ActionResult payment() { int line = Int32.Parse(Request.Form["line"]); int chair = Int32.Parse(Request.Form["chair"]); OrdersDal odal = new OrdersDal(); HallAndMovie obj = (HallAndMovie)Session["hmvm"]; obj.order.LineSeat = line; obj.order.RowSeat = chair; obj.order.HallName = obj.Movie.HallName; obj.order.MovieName = obj.Movie.MovieName; obj.order.Price = obj.Movie.Price; obj.order.Date = obj.Movie.Date; obj.order.OrderID = 0; odal.Orders.Add(obj.order); odal.SaveChanges(); return(View(obj)); }
//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")); }