Esempio n. 1
0
        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");
        }
Esempio n. 2
0
        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);
        }