public ActionResult SearchFor(HomepageModel searchfor) { var proDAL = new ProductDAL(); List<Products> pl = proDAL.Products.Where( entity => entity.Name.Contains(searchfor.SingleProduct.Name) && (entity.Quantity >=1)).ToList<Products>(); //! Creating the Model which will be used later on. searchfor = new HomepageModel(); searchfor.ProductsCollection = new List<Products>(); searchfor.ProductsCollection = pl; return View("HomePage", searchfor); }
// // GET: /HomePage/ public ActionResult Index(HomepageModel hpm) { if (hpm.ProductsCollection != null) return View("HomePage", hpm); /* * Creating the DB Connection for the homepage products view. * */ var proDAL = new ProductDAL(); List<Products> pl = proDAL.Products.Where(item => item.Quantity >= 1).ToList<Products>(); //! Creating the Model which will be used later on. HomepageModel homepageModel = new HomepageModel(); homepageModel.ProductsCollection = new List<Products>(); homepageModel.ProductsCollection = pl; return View("HomePage", homepageModel); }
public ActionResult FilterByPrice(HomepageModel hpm) { /* * Filtering by price is an option to filter the products at the homepage * by the lower and upper price bound. * */ var proDAL = new ProductDAL(); //! Getting the products which stands with the conditions. List<Products> pl = proDAL.Products.Where( entity => (entity.Price >= hpm.Pricing.lower) && (entity.Price <= hpm.Pricing.upper) && (entity.Quantity >= 1)).ToList<Products>(); //! Creating the Model which will be used later on. hpm = new HomepageModel(); hpm.ProductsCollection = new List<Products>(); hpm.ProductsCollection = pl; return View("HomePage", hpm); }
public ActionResult PlaceOrder(HomepageModel hpm) { /* Checking if the model isn't fake * or empty. and placeing the order, * and setting up the customer as well. * */ if (hpm.SingleProduct != null && hpm.CustomerEntity != null) { try { //! Lets Insert the Customer details first: CustomerDAL customerDAL = new CustomerDAL(); CustomerEntity customer = new CustomerEntity(); customer.Name = hpm.CustomerEntity.Name; customer.Address = hpm.CustomerEntity.Address; customer.Email = hpm.CustomerEntity.Email; /** * CheckIfExist variable holding the current customer data. * and take place to ensure there isn't duplicated customers. * * We assusme that uniqe customer has uniqe Name, * and uniqe Email Addr. on any other mismatches we'll add the * customer as a new customer record. * */ var checkIfExistAlready = customerDAL.Customers.FirstOrDefault( cust => (cust.Name.Equals(customer.Name)) && (cust.Address.Equals(customer.Address)) && (cust.Email.Equals(customer.Email)) ); //! Case customer not exist in customers data. if (checkIfExistAlready == null) { /* This step above and below is about avoiding redundency * and keep only exact one Customer recored. * */ customerDAL.Customers.Add(customer); customerDAL.SaveChanges(); } //! Now Lets bind the customer Id to the Order CustomerId var exactCustomer = customerDAL.Customers.FirstOrDefault( cust=> (cust.Name.Equals(hpm.CustomerEntity.Name)) && (cust.Address.Equals(hpm.CustomerEntity.Address)) && (cust.Email.Equals(hpm.CustomerEntity.Email)) ); //! Now that we have the current customer details lets make an order OrderDAL orderDAL = new OrderDAL(); Order newOrder = new Order(); newOrder.Date = DateTime.Now; newOrder.CustomerId = exactCustomer.Id; newOrder.Product_Id = hpm.SingleProduct.Id; orderDAL.Order.Add(newOrder); orderDAL.SaveChanges(); /* * Quantity handaling for the current product **/ ProductDAL productDAL = new ProductDAL(); var currentProduct = productDAL.Products.FirstOrDefault( prod=>prod.Id == hpm.SingleProduct.Id); if (currentProduct.Quantity >= 0) currentProduct.Quantity--; productDAL.Entry(currentProduct).CurrentValues.SetValues(currentProduct); productDAL.SaveChanges(); return View("OrderCompleted"); } catch (Exception) { //! If there's an error, yield err. return View("OrderFaild"); } } return View("OrderCompleted"); }
public ActionResult GetJsonOrders() { /* * This function intended to show all of the order details that has been commited. * This function passes an Json presentation to caller as requested. **/ var orderDAL = new OrderDAL(); var productDAL = new ProductDAL(); var customerDAL = new CustomerDAL(); ShowOrdersModel model = new ShowOrdersModel(); model.Orders = new List<DetailedOrder>(); List<Order> orders = orderDAL.Order.ToList<Order>(); foreach (var order in orders) { /* * Gathering all realtions that stands with the right conditions * */ var currentCustomer = customerDAL.Customers.FirstOrDefault( cust => cust.Id == order.CustomerId); var currentProduct = productDAL.Products.FirstOrDefault( prod => prod.Id == order.Product_Id); var viewOrder = new DetailedOrder(); /** * Setting the viewOrder list to be viewed. **/ viewOrder.Id = order.Id; viewOrder.Date = order.Date; viewOrder.CustomerName = currentCustomer.Name; viewOrder.ProductName = currentProduct.Name; viewOrder.ProductPrice = currentProduct.Price; model.Orders.Add(viewOrder); } return Json(model.Orders, JsonRequestBehavior.AllowGet); }
public ActionResult SubmitProductsValues(ProductModel productModel) { //! Pulling DATA from db using DbContext var proDAL = new ProductDAL(); //! Loop through model.ProductsCollection foreach (var p in productModel.ProductsCollection) { //! Matching the current product var query = proDAL.Products.FirstOrDefault(q => q.Id == p.Id); if (query != null) { if (p.pExist != true) { /* * pExist it's an Indicator about the product lifetime. * */ query.Quantity = p.Quantity; proDAL.SaveChanges(); } else { //! pExist True; means it was mark to be removed query.pExist = false; proDAL.Products.Remove(query); proDAL.SaveChanges(); } } } //! All-set redirecting view. return View("SubmitProductsValues"); }
public ActionResult EditProducts() { //! Pulling DATA from db using DbContext var proDAL = new ProductDAL(); List<Products> pl = proDAL.Products.ToList<Products>(); ProductModel productModel = new ProductModel(); productModel.ProductsCollection = new List<Products>(); productModel.ProductsCollection = pl; return View("EditProducts", productModel); }
public ActionResult AddProductForm(ProductUploadModel obj) { //! Lets make a connection to products inside DB ProductDAL proDAL = new ProductDAL(); /* * Lets test if the current SKU is uniqe; * if not we would like to tell the admin * to change the SKU * */ var uniqeSKU = proDAL.Products.FirstOrDefault(item=>item.SKU == obj.pr.SKU); if (uniqeSKU != null) return View("ProductSKUError"); /* * Files uploading is-in the block below * */ if (obj.pic != null && obj.pic.ContentLength > 0) { var path = "~/PicData/"; var fname = "pic_" + obj.pr.Id + "_" + obj.pic.FileName; obj.pic.SaveAs( Path.Combine( Server.MapPath(path), fname)); //! Setting picURL to be used farther obj.pr.PicURL = fname; } try { //! Lets store the product inside the Products DB instance. proDAL.Products.Add(obj.pr); proDAL.SaveChanges(); //! Case all fine, open user message. return View("ProductSuccessfulAdded"); } catch (Exception) { //! Case not: return View("ProductProccessError"); } }