コード例 #1
0
ファイル: HomePageController.cs プロジェクト: unitx/ASP.NET
        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");
        }
コード例 #2
0
        //Post api/orders/
        public HttpResponseMessage PostOrders(ICollection<SingleOrder> orders, [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
             () =>
             {
                 using (var context = new StoreContext())
                 {
                     this.ValidateSessionKey(sessionKey);

                     var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                     if (user == null)
                     {
                         throw new ArgumentException("Invalid SessionKey or user is already logouted");
                     }

                     //Adding main order
                     var order = new Order()
                     {
                         User = user,
                         Status = Pending
                     };
                     context.Orders.Add(order);
                     context.SaveChanges();

                     //Adding orders for individual products
                     foreach (var singleOrder in orders)
                     {
                         var product = context.Products.FirstOrDefault(p => p.Name == singleOrder.Product.Name);
                         if (product.Quantity == 0)
                         {
                             throw new InvalidOperationException("Current product is out of stock!");
                         }
                         product.Quantity--;

                         var newSingleOrder = new SingleOrder
                         {
                             Quantity = 1,
                             Product = product,
                             Order = order,
                         };

                         context.SingleOrders.Add(newSingleOrder);
                         context.SaveChanges();
                     }

                     var response = this.Request.CreateResponse(HttpStatusCode.OK, JsonMediaTypeFormatter.DefaultMediaType);
                     return response;
                 }
             });

            return responseMsg;
        }