Beispiel #1
0
        private Result <OrderQuery> MapOrderItems(dynamic result)
        {
            var order = new OrderQuery
            {
                Ordernumber = result[0].ordernumber,
                Date        = result[0].date,
                Status      = result[0].status,
                Description = result[0].description,
                Street      = result[0].street,
                City        = result[0].city,
                Zipcode     = result[0].zipcode,
                Country     = result[0].country,
                Orderitems  = new List <Orderitem>(),
                Total       = 0
            };

            foreach (dynamic item in result)
            {
                var orderitem = new Orderitem
                {
                    Productname = item.productname,
                    Units       = item.units,
                    Unitprice   = (double)item.unitprice,
                    Pictureurl  = item.pictureurl
                };

                order.Total += item.units * item.unitprice;
                order.Orderitems.Add(orderitem);
            }

            return(Result.Success(order));
        }
        private Order MapOrderItems(dynamic result)
        {
            var order = new Order
            {
                ordernumber = result[0].ordernumber,
                date        = result[0].date,
                status      = result[0].status,
                description = result[0].description,
                street      = result[0].street,
                city        = result[0].city,
                zipcode     = result[0].zipcode,
                country     = result[0].country,
                orderitems  = new List <Orderitem>(),
                total       = 0
            };

            foreach (dynamic item in result)
            {
                var orderitem = new Orderitem
                {
                    productname = item.productname,
                    units       = item.units,
                    unitprice   = (double)item.unitprice,
                    pictureurl  = item.pictureurl
                };

                order.total += item.units * item.unitprice;
                order.orderitems.Add(orderitem);
            }

            return(order);
        }
        public async Task <IActionResult> PutOrderitem(int id, Orderitem orderitem)
        {
            if (id != orderitem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(orderitem).State = EntityState.Modified;

            try {
                await _context.SaveChangesAsync();

                _context.Entry(orderitem).State = EntityState.Detached;
                _context.Orderitems.Find(orderitem.Id); // refresh the cache
                RecalculateTotel(orderitem.OrderId);
            } catch (DbUpdateConcurrencyException) {
                if (!OrderitemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #4
0
 public void Add(Orderitem orderitem)
 {
     using (var connection = new MySqlConnection(this.connectionString))
     {
         connection.Execute("INSERT INTO Orderitems (order_id, product_name, product_description, product_price) VALUES(@order_id, @product_name, @product_description, @product_price)", orderitem);
     }
 }
Beispiel #5
0
        public async Task <IActionResult> Create([Bind("City,Country,FirstName,LastName,Phone,PostalCode,State")] Order order)
        {
            ApplicationUser user = await _userManager.GetUserAsync(User);

            if (ModelState.IsValid)
            {
                ShoppingCart     cart    = ShoppingCart.GetCart(this.HttpContext);
                List <CartItem>  items   = cart.GetCartItems(_context);
                List <Orderitem> details = new List <Orderitem>();
                foreach (CartItem item in items)
                {
                    Orderitem detail = CreateOrderDetailForThisItem(item);
                    detail.Order = order;
                    details.Add(detail);
                    _context.Add(detail);
                }

                order.User       = user;
                order.OrderDate  = DateTime.Today;
                order.Total      = ShoppingCart.GetCart(this.HttpContext).GetTotal(_context);
                order.Orderitems = details;
                _context.SaveChanges();


                return(RedirectToAction("Purchased", new RouteValueDictionary(
                                            new { action = "Purchased", id = order.OrderId })));
            }

            return(View(order));
        }
Beispiel #6
0
        public IActionResult Post([FromBody] Orderitem orderitem)
        {
            var result = this.orderitemsService.Add(orderitem);

            if (!result)
            {
                return(BadRequest());
            }

            return(Ok());
        }
        public async Task <ActionResult <Orderitem> > PostOrderitem(Orderitem orderitem)
        {
            _context.Orderitems.Add(orderitem);
            await _context.SaveChangesAsync();

            _context.Entry(orderitem).State = EntityState.Detached;
            _context.Orderitems.Find(orderitem.Id); // refresh the cache
            RecalculateTotel(orderitem.OrderId);

            return(CreatedAtAction("GetOrderitem", new { id = orderitem.Id }, orderitem));
        }
Beispiel #8
0
        private Orderitem CreateOrderDetailForThisItem(CartItem item)
        {
            Orderitem detail = new Orderitem();


            detail.Quantity  = item.Count;
            detail.Hat       = item.Hat;
            detail.UnitPrice = item.Hat.Price;

            return(detail);
        }
Beispiel #9
0
        public ActionResult PlaceOrder()
        {
            ///////////////////
            // Place the order.
            ///////////////////

            /* Now an order is created here automatic when the button is clicked.
             * All this code has to be moved from here. Here the user should be
             * taken to checkout details page.
             */
            if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Order order = new Order();
                Admin admin = db.Admins.Find(1);
                order.Streetaddress = "Happy street 1";
                string sessionIdForOrder      = System.Web.HttpContext.Current.Session.SessionID;
                var    cartForOrderIQueriable = from c in db.Carts
                                                where c.SessionId == sessionIdForOrder && c.State == "active"
                                                select c;
                var cartForOrder = cartForOrderIQueriable.FirstOrDefault();
                admin.TotalIncome += (decimal)order.Total;
                order.UserId       = cartForOrder.UserId;
                order.SessionId    = cartForOrder.SessionId;
                //order.ShippingCost = 4.50;
                order.ShippingCost = admin.ShippingCost;
                order.Total        = cartForOrder.Total + admin.ShippingCost;
                order.State        = "New";
                db.Orders.Add(order);
                db.SaveChanges();
                var cartItemsForOrder = cartForOrder.Cartitems;
                ICollection <Orderitem> Orderitems = new List <Orderitem>();
                foreach (var item in cartItemsForOrder)
                {
                    Orderitem orderItem = new Orderitem();
                    orderItem.OrderId        = order.Id;
                    orderItem.OrderitemTotal = item.CartitemTotal;
                    orderItem.ProductId      = item.ProductId;
                    orderItem.Quantity       = item.Quantity;
                    db.Orderitems.Add(orderItem);
                    Orderitems.Add(orderItem);
                }
                cartForOrder.State = "ordered";
                db.SaveChanges();
                //return View("Index", "Orders");
                //return RedirectToAction("Index", "Orders");
                return(RedirectToAction("Details", "Orders", new { id = order.Id }));
                //return View();
            }
            else
            {
                return(RedirectToAction("CheckoutLogin", "Account"));
            }
        }
Beispiel #10
0
        //计算总价格
        public int getTotalPrice()
        {
            int totalPrice = 0;

            foreach (Orderitem item in items)
            {
                Orderitem orderitem = item;
                Book      book      = orderitem.Book;
                int       quantity  = orderitem.Quantity;
                totalPrice += book.Price * quantity;
            }
            return(totalPrice);
        }
Beispiel #11
0
        public Cart addToCart(string name, int quantity)
        {
            Book book = (new DBTask()).getBookbyName(name);//调用数据访问层犯法获得图书

            Orderitem orderitem = new Orderitem();

            orderitem.Book     = book;
            orderitem.Quantity = quantity;
            if (cart == null)
            {
                cart = new Cart();
            }
            cart.addBook(orderitem);    //调用购物车模型的addBook方法添加图书到购物车
            return(cart);
        }
Beispiel #12
0
        public static void Initialize(ApplicationDbContext context)
        {
            context.Database.EnsureCreated();
            // Look for any students.
            if (context.Customer.Any())
            {
                return; // DB has been seeded
            }
            var customers = new Customer[]
            {
                new Customer {
                    Name = "Carson", Home = "1234567", Work = "76543241", Mobile = "15498788", Email = "*****@*****.**", Address = "CarsonCity"
                },
                new Customer {
                    Name = "Barson", Home = "1234567", Work = "4314134", Mobile = "15498788", Email = "*****@*****.**", Address = "CarsonCity"
                },
                new Customer {
                    Name = "Darson", Home = "1234567", Work = "4134134", Mobile = "15498788", Email = "*****@*****.**", Address = "CarsonCity"
                },
                new Customer {
                    Name = "Earson", Home = "1234567", Work = "43534534", Mobile = "15498788", Email = "*****@*****.**", Address = "CarsonCity"
                },
                new Customer {
                    Name = "Larson", Home = "1234567", Work = "6542478", Mobile = "15498788", Email = "*****@*****.**", Address = "CarsonCity"
                },
            };

            foreach (Customer c in customers)
            {
                context.Customer.Add(c);
            }
            context.SaveChanges();
            var orders = new Order[]
            {
                new Order {
                    CustomerID = 3, Status = Status.deliver, Subtotal = 11.56, GST = 0.15, Grandtotal = 84.26
                },
                new Order {
                    CustomerID = 4, Status = Status.deliver, Subtotal = 12.56, GST = 0.15, Grandtotal = 84.26
                },
                new Order {
                    CustomerID = 1, Status = Status.deliver, Subtotal = 13.56, GST = 0.15, Grandtotal = 84.26
                },
                new Order {
                    CustomerID = 2, Status = Status.deliver, Subtotal = 14.56, GST = 0.15, Grandtotal = 84.26
                },
            };

            foreach (Order o in orders)
            {
                context.Order.Add(o);
            }
            context.SaveChanges();

            var categorys = new Category[]
            {
                new Category {
                    Name = "Male"
                },
                new Category {
                    Name = "Female"
                },
                new Category {
                    Name = "Children"
                },
            };

            foreach (Category ca in categorys)
            {
                context.Category.Add(ca);
            }
            context.SaveChanges();
            var suppliers = new Supplier[]
            {
                new Supplier {
                    Name = "ASD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
                new Supplier {
                    Name = "BSD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
                new Supplier {
                    Name = "CSD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
                new Supplier {
                    Name = "DSD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
                new Supplier {
                    Name = "ESD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
                new Supplier {
                    Name = "FSD", Home = "12448482", Work = "323656", Mobile = "2545223", Email = "*****@*****.**", Address = "Larson"
                },
            };

            foreach (Supplier su in suppliers)
            {
                context.Supplier.Add(su);
            }
            context.SaveChanges();
            var hats = new Hat[]
            {
                new Hat {
                    SupplierID = 1, CategoryID = 2, HatName = "KALSD", Price = 12.5, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
                new Hat {
                    SupplierID = 2, CategoryID = 2, HatName = "ALSD", Price = 8.5, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
                new Hat {
                    SupplierID = 3, CategoryID = 1, HatName = "DALSD", Price = 11, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
                new Hat {
                    SupplierID = 1, CategoryID = 2, HatName = "QALSD", Price = 16.2, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
                new Hat {
                    SupplierID = 2, CategoryID = 2, HatName = "CLSD", Price = 7.9, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
                new Hat {
                    SupplierID = 3, CategoryID = 1, HatName = "DALSD", Price = 14.3, Description = "FAFDFGGSAD", Image = "images/hat2.jpg"
                },
            };

            foreach (Hat h in hats)
            {
                context.Hat.Add(h);
            }
            context.SaveChanges();
            var orderitems = new Orderitem[]
            {
                new Orderitem {
                    HatID = 1, OrderID = 1, Quantity = 4
                },
                new Orderitem {
                    HatID = 3, OrderID = 2, Quantity = 15
                },
                new Orderitem {
                    HatID = 2, OrderID = 4, Quantity = 24
                },
            };

            foreach (Orderitem ord in orderitems)
            {
                context.Orderitem.Add(ord);
            }
            context.SaveChanges();
        }
Beispiel #13
0
 //添加图书到购物车
 public void addBook(Orderitem orderitem)
 {
     items.Add(orderitem);
 }
 public void AddItem(OrderItem item)
 {
     Orderitem.Add(item);
 }
 public void RemoveItem(OrderItem item)
 {
     Orderitem.Remove(item);
 }
 public bool Add(Orderitem orderitem)
 {
     this.orderitemsRepository.Add(orderitem);
     return(true);
 }
Beispiel #17
0
        // Handles cart.
        // Handles both updating of cart and placing of order.
        //public ActionResult HandleCart(int CartId, int CartitemId, int newQuantity)
        public ActionResult HandleCart(string submitButton, string returnUrl)
        {
            //System.Diagnostics.Debug.WriteLine("Inside HandleCart.");
            string cartId = Request.Form["item.Cart.Id"];
            //System.Diagnostics.Debug.WriteLine("CartId is: " + cartId[0]);
            //System.Diagnostics.Debug.WriteLine("CartId2 is: " + Request.Form["item.Cart.Id"]);

            var cartItemIds      = Request.Form["item.Id"];
            var cartItemIdsArray = cartItemIds.Split(',');

            System.Diagnostics.Debug.WriteLine("Number of cartitemids is: " + cartItemIdsArray.Length);
            System.Diagnostics.Debug.WriteLine("First cartitemids is: " + cartItemIdsArray[0]);

            var productIds      = Request.Form["item.Product.Id"];
            var productIdsArray = productIds.Split(',');

            System.Diagnostics.Debug.WriteLine("Number of productids is: " + productIdsArray.Length);

            var quantities      = Request.Form["item.Quantity"];
            var quantitiesArray = quantities.Split(',');

            System.Diagnostics.Debug.WriteLine("Number of productids is: " + quantitiesArray.Length);

            // You will have to get product price somehow.
            System.Diagnostics.Debug.WriteLine("Quantity is: " + Request.Form["item.Quantity"][0]);

            switch (submitButton)
            {
            case "Uppdatera":
                // Update the quantity and price for each cartitem
                //System.Diagnostics.Debug.WriteLine("Klicked button: Uppdatera");
                // The number of cartitem IDs is the same as there are cartitems in
                // the cart.
                double cartTotal = 0;
                for (int i = 0; i < cartItemIdsArray.Length; i++)
                {
                    //System.Diagnostics.Debug.WriteLine("i is: " + i);
                    string counter = cartItemIdsArray[i].ToString();
                    //System.Diagnostics.Debug.WriteLine("counter is: " + counter);
                    int    j            = Convert.ToInt32(counter);
                    var    cartItem     = db.Cartitems.Find(j);
                    double productPrice = db.Products.Find(Int32.Parse(productIdsArray[i].ToString())).Price;
                    // If the number in the input field is 0 the cartitem will be removed.
                    if (Int32.Parse(quantitiesArray[i].ToString()) < 1)
                    {
                        db.Cartitems.Remove(cartItem);
                    }
                    else
                    {
                        cartItem.Quantity      = Int32.Parse(quantitiesArray[i].ToString());
                        cartItem.CartitemTotal = Int32.Parse(quantitiesArray[i].ToString()) * db.Products.Find(Int32.Parse(productIdsArray[i].ToString())).Price;
                        cartTotal += cartItem.CartitemTotal;
                    }

                    db.SaveChanges();
                }
                string sId        = System.Web.HttpContext.Current.Session.SessionID;
                var    cartFromDb = from c in db.Carts
                                    where c.SessionId == sId && c.State == "active"
                                    select c;
                var cartForTotal = cartFromDb.FirstOrDefault();
                cartForTotal.Total = cartTotal;
                db.SaveChanges();

                ///////////////////////////////////////////////////////
                // Getting the entire cart and redirecting to cartindex
                ///////////////////////////////////////////////////////
                var    cartViewModel = new CartViewModel();
                Cart   cart          = new Cart();
                string sessionId     = System.Web.HttpContext.Current.Session.SessionID;
                var    existingCart  = from c in db.Carts
                                       where c.SessionId == sessionId && c.State == "active"
                                       select c;

                var cartItems = from c in db.Cartitems
                                //where c.Cart.Id == cart.Id
                                where c.Cart.Id == existingCart.FirstOrDefault().Id
                                select c;

                foreach (var item in existingCart)
                {
                    cart = item;
                }
                cartViewModel.Cart      = cart;
                cartViewModel.Cartitems = cartItems.ToList();
                return(View("CartIndex", cartViewModel));

            // Getting the entire cart and redirecting to cartindex ends here
            case "Checkoutdetaljer":

                return(RedirectToAction("CheckoutDetails"));

            case "Lägg order":
                ///////////////////
                // Place the order.
                ///////////////////

                /* Now an order is created here automatic when the button is clicked.
                 * All this code has to be moved from here. Here the user should be
                 * taken to checkout details page.
                 */
                if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    Order order = new Order();
                    Admin admin = db.Admins.Find(1);
                    order.Streetaddress = "Happy street 1";
                    string sessionIdForOrder      = System.Web.HttpContext.Current.Session.SessionID;
                    var    cartForOrderIQueriable = from c in db.Carts
                                                    where c.SessionId == sessionIdForOrder && c.State == "active"
                                                    select c;
                    var cartForOrder = cartForOrderIQueriable.FirstOrDefault();
                    order.Total        = cartForOrder.Total;
                    admin.TotalIncome += (decimal)order.Total;
                    order.UserId       = cartForOrder.UserId;
                    order.SessionId    = cartForOrder.SessionId;
                    order.ShippingCost = 4.50;
                    order.State        = "New";
                    db.Orders.Add(order);
                    db.SaveChanges();
                    var cartItemsForOrder = cartForOrder.Cartitems;
                    ICollection <Orderitem> Orderitems = new List <Orderitem>();
                    foreach (var item in cartItemsForOrder)
                    {
                        Orderitem orderItem = new Orderitem();
                        orderItem.OrderId        = order.Id;
                        orderItem.OrderitemTotal = item.CartitemTotal;
                        orderItem.ProductId      = item.ProductId;
                        orderItem.Quantity       = item.Quantity;
                        db.Orderitems.Add(orderItem);
                        Orderitems.Add(orderItem);
                    }
                    cartForOrder.State = "ordered";
                    db.SaveChanges();
                    //return View("Index", "Orders");
                    return(RedirectToAction("Index", "Orders"));
                }
                else
                {
                    return(RedirectToAction("CheckoutLogin", "Account"));
                }


            default:
                // If they've submitted the form without a submitButton,
                // just return the view again.
                return(View());
            }
            return(View("~/Views/Products/Index.cshtml"));
        }