Esempio n. 1
0
        public async Task <IActionResult> AddtoCartitem(long bookid, long priceid)
        {
            var  book     = _context.Book.Find(bookid);
            var  price    = _context.Price.Find(priceid);
            var  cartId   = HttpContext.Request.Cookies["CartId"];
            var  cart     = _context.Cart.Find(Convert.ToString(cartId));
            Guid gu_id    = Guid.NewGuid();
            var  cartItem = new CartItem()
            {
                Book = book, Price = price, Cart = cart, WantToBuy = true, CartItem_Id = gu_id.ToString()
            };

            _context.Add(cartItem);
            _context.SaveChanges();
            var stringBookid = Convert.ToString(bookid);

            return(RedirectToAction("Detail", "Search", new { @id = bookid }));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                //get user id
                var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

                var user = await _userManager.FindByIdAsync(userId);

                if (user == null)
                {
                    return(NotFound($"Unable to load user with ID '{userId}'."));
                }

                var result = await _userManager.ConfirmSignUpAsync(user, Input.Code, true);

                if (!result.Succeeded)
                {
                    throw new InvalidOperationException($"Error confirming account for user with ID '{userId}':");
                }
                else
                {
                    //this part is to add customer information into the DB
                    var userName = await _userManager.GetUserNameAsync(user);

                    var firstName   = user.Attributes[CognitoAttribute.GivenName.AttributeName];
                    var lastName    = user.Attributes[CognitoAttribute.FamilyName.AttributeName];
                    var email       = user.Attributes[CognitoAttribute.Email.AttributeName];
                    var dateOfBirth = user.Attributes[CognitoAttribute.BirthDate.AttributeName];
                    var phone       = user.Attributes[CognitoAttribute.PhoneNumber.AttributeName];
                    var customer_ID = user.Attributes[CognitoAttribute.Sub.AttributeName];
                    var customer    = new Customer()
                    {
                        Customer_Id = customer_ID,
                        //Customer_Id = 1111,
                        Username  = userName,
                        FirstName = firstName,
                        LastName  = lastName,
                        Email     = email,
                        Phone     = phone
                    };
                    _context.Add(customer);
                    _context.SaveChanges();

                    var currentCustomerID = _context.Customer.Find(user.Attributes[CognitoAttribute.Sub.AttributeName]);

                    //get cardid
                    var cartId     = HttpContext.Request.Cookies["CartId"];
                    var recentCart = await _context.Cart.FindAsync(Convert.ToString(cartId));

                    recentCart.Customer = currentCustomerID;
                    _context.Update(recentCart);
                    await _context.SaveChangesAsync();


                    return(returnUrl != null?LocalRedirect(returnUrl) : Page() as IActionResult);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Esempio n. 3
0
        //add book to cart
        //public async Task<IActionResult> AddtoCartitem(long bookid,long priceid)
        //{
        //    var book = _context.Book.Find(bookid);
        //    var price = _context.Price.Find(priceid);
        //    var cartId = HttpContext.Request.Cookies["CartId"];
        //    var cart = _context.Cart.Find(Convert.ToInt32(cartId));

        //    var cartItem = new CartItem() { Book = book, Price = price, Cart = cart,WantToBuy=true };

        //    _context.Add(cartItem);
        //    await _context.SaveChangesAsync();
        //    return RedirectToAction(nameof(Index));
        //}
        //public async Task<IActionResult> AddtoWishlist(long bookid, long priceid)
        //{
        //    var book = _context.Book.Find(bookid);
        //    var price = _context.Price.Find(priceid);
        //    var cartId = HttpContext.Request.Cookies["CartId"];
        //    var cart = _context.Cart.Find(Convert.ToInt32(cartId));

        //    var cartItem = new CartItem() { Book = book, Price = price, Cart = cart, WantToBuy = true };

        //    _context.Add(cartItem);
        //    await _context.SaveChangesAsync();
        //    return RedirectToAction(nameof(Index));
        //}
        public async Task <IActionResult> CheckOut(string[] fruits, string[] IDs, string[] quantity, string[] bookF, string[] priceF)
        {
            //set the origin value
            long statueId    = 1;
            var  orderStatue = _context.OrderStatus.Find(statueId);
            var  user        = await _userManager.GetUserAsync(User);

            var    userId     = user.Attributes[CognitoAttribute.Sub.AttributeName];
            var    customer   = _context.Customer.Find(userId);
            double subTotal   = 0.0;
            var    itemIdList = new List <CartItem>();

            //calculate the total price and put all book in a list
            for (int i = 0; i < IDs.Length; i++)
            {
                var cartItem = from c in _context.CartItem
                               where c.CartItem_Id == Convert.ToString(IDs[i])
                               select c;

                var item = new CartItem();
                foreach (var ii in cartItem)
                {
                    item = ii;
                }

                itemIdList.Add(item);
                subTotal += Convert.ToDouble(fruits[i]) * Convert.ToInt32(quantity[i]);
            }
            //creat a new order
            var recentOrder = new Order()
            {
                OrderStatus = orderStatue, Subtotal = subTotal, Tax = subTotal * 0.1, Customer = customer
            };

            _context.Add(recentOrder);
            _context.SaveChanges();
            var orderId = recentOrder.Order_Id;

            if (!HttpContext.Request.Cookies.ContainsKey("OrderId"))
            {
                CookieOptions options = new CookieOptions();

                HttpContext.Response.Cookies.Append("OrderId", Convert.ToString(orderId));
            }
            else
            {
                HttpContext.Response.Cookies.Delete("OrderId");
                HttpContext.Response.Cookies.Append("OrderId", Convert.ToString(orderId));
            }
            //add item to these order
            for (int i = 0; i < bookF.Length; i++)
            {
                var orderDetailBook = _context.Book.Find(Convert.ToInt64(bookF[i]));
                //var orderDetailPrice = itemIdList[i].Price;
                var orderDetailPrice = _context.Price.Find(Convert.ToInt64(priceF[i]));
                var newOrderDetail   = new OrderDetail()
                {
                };
                var OrderDetail = new OrderDetail()
                {
                    Book = orderDetailBook, Price = orderDetailPrice, price = Convert.ToDouble(fruits[i]), quantity = Convert.ToInt32(quantity[i]), Order = recentOrder, IsRemoved = false
                };
                orderDetailPrice.Quantity = orderDetailPrice.Quantity - Convert.ToInt32(quantity[i]);
                _context.Update(orderDetailPrice);
                _context.Add(OrderDetail);
                _context.SaveChanges();
            }
            //remove from cart
            for (int i = 0; i < IDs.Length; i++)
            {
                var cartItemD = _context.CartItem.Find(Convert.ToString(IDs[i]));
                _context.CartItem.Remove(cartItemD);
            }


            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(ConfirmCheckout), new { OrderId = orderId }));
            //return View();
        }