Exemplo 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> Create([Bind("Address_Id,AddressLine1,AddressLine2,City,State,Country,ZipCode")] Address address)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                var id       = user.Attributes[CognitoAttribute.Sub.AttributeName];
                var customer = _context.Customer.Find(id);
                address.Customer = customer;
                _context.Add(address);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(address));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Index()
        {
            string ip;

            if (_SignInManager.IsSignedIn(User))
            {
                //get customer
                var user = await _userManager.GetUserAsync(User);

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

                //get cart
                var cart = from c in _context.Cart
                           select c;
                cart = cart.Where(s => s.Customer == currentCustomer);
                Cart currentCart = new Cart();
                foreach (var ca in cart)
                {
                    currentCart = ca;
                }
                //put cartid in cookie
                if (!HttpContext.Request.Cookies.ContainsKey("CartId"))
                {
                    CookieOptions options = new CookieOptions();

                    HttpContext.Response.Cookies.Append("CartId", Convert.ToString(currentCart.Cart_Id));
                }
                else
                {
                    HttpContext.Response.Cookies.Delete("CartId");
                    HttpContext.Response.Cookies.Append("CartId", Convert.ToString(currentCart.Cart_Id));
                }
                //put cart item in user cart
                var id       = Convert.ToString(HttpContext.Request.Cookies["CartId"]);
                var cartC    = _context.Cart.Find(id);
                var cartItem = from c in _context.CartItem
                               where c.Cart == cartC && c.WantToBuy == true
                               select c;
                var userC = await _userManager.GetUserAsync(User);

                var UserId = userC.Attributes[CognitoAttribute.Sub.AttributeName];

                var recentcustomer = _context.Customer.Find(UserId);
                var customerCart   = from c in _context.Cart
                                     where c.Customer == recentcustomer
                                     select c;
                Cart recentCart = new Cart();
                foreach (var item in customerCart)
                {
                    recentCart = item;
                }
                //foreach (var item in cartItem)
                //{
                //    item.Cart = recentCart;
                //    _context.Update(item);
                //}
                await _context.SaveChangesAsync();
            }
            else
            {
                if (!HttpContext.Request.Cookies.ContainsKey("CartIp"))
                {
                    CookieOptions options = new CookieOptions();
                    Guid          gu_id   = Guid.NewGuid();
                    ip = gu_id.ToString();
                    HttpContext.Response.Cookies.Append("CartIp", gu_id.ToString());
                    Guid   gu_id1 = Guid.NewGuid();
                    string id     = gu_id1.ToString();
                    var    IP     = new Cart()
                    {
                        IP = ip, Cart_Id = id
                    };
                    _context.Add(IP);

                    await _context.SaveChangesAsync();
                }
                else
                {
                    string CartIp = HttpContext.Request.Cookies["CartIp"];
                    ip = CartIp;
                }
                //put cart id in cookie
                var cart = from c in _context.Cart
                           select c;
                cart = cart.Where(s => s.IP == ip);
                Cart currentCart = new Cart();
                foreach (var ca in cart)
                {
                    currentCart = ca;
                }

                if (!HttpContext.Request.Cookies.ContainsKey("CartId"))
                {
                    CookieOptions options = new CookieOptions();
                    HttpContext.Response.Cookies.Append("CartId", Convert.ToString(currentCart.Cart_Id));
                }
                else
                {
                    HttpContext.Response.Cookies.Delete("CartId");
                    HttpContext.Response.Cookies.Append("CartId", Convert.ToString(currentCart.Cart_Id));
                }
            }
            return(View());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);


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

            if (!ModelState.IsValid)
            {
                await LoadAsync(user);

                return(Page());
            }

            if (string.IsNullOrWhiteSpace(Input.FamilyName))
            {
                Input.FamilyName = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.Address))
            {
                Input.Address = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.Gender))
            {
                Input.Gender = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.GivenName))
            {
                Input.GivenName = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.BirthDate))
            {
                Input.BirthDate = "0000-00-00";
            }
            if (string.IsNullOrWhiteSpace(Input.PhoneNumber))
            {
                Input.PhoneNumber = "+01234567890";
            }
            if (string.IsNullOrWhiteSpace(Input.NickName))
            {
                Input.NickName = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.AddressLine1))
            {
                Input.AddressLine1 = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.AddressLine2))
            {
                Input.AddressLine2 = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.City))
            {
                Input.City = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.State))
            {
                Input.State = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.Country))
            {
                Input.Country = "default";
            }
            if (string.IsNullOrWhiteSpace(Input.ZipCode))
            {
                Input.ZipCode = "default";
            }

            //update cognito
            user.Attributes[CognitoAttribute.Address.AttributeName]     = Input.Address;
            user.Attributes[CognitoAttribute.BirthDate.AttributeName]   = Input.BirthDate;
            user.Attributes[CognitoAttribute.Gender.AttributeName]      = Input.Gender;
            user.Attributes[CognitoAttribute.NickName.AttributeName]    = Input.NickName;
            user.Attributes[CognitoAttribute.PhoneNumber.AttributeName] = Input.PhoneNumber;
            user.Attributes[CognitoAttribute.FamilyName.AttributeName]  = Input.FamilyName;
            user.Attributes[CognitoAttribute.GivenName.AttributeName]   = Input.GivenName;
            user.Attributes["custom:AddressLine1"] = Input.AddressLine1;
            user.Attributes["custom:AddressLine2"] = Input.AddressLine2;
            user.Attributes["custom:City"]         = Input.City;
            user.Attributes["custom:State"]        = Input.State;
            user.Attributes["custom:Country"]      = Input.Country;

            user.Attributes["custom:ZipCode"] = Input.ZipCode;

            var result = await _userManager.UpdateAsync(user);

            if (!result.Succeeded)

            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            //input and update customer address information into the DB
            else
            {
                var id = user.Attributes[CognitoAttribute.Sub.AttributeName];
                //get customer information
                var recentCustomer = await _context.Customer.FindAsync(id);

                var address = from m in _context.Address
                              where m.Customer == recentCustomer && m.IsPrimary == true
                              select m;
                if (address == null)
                {
                    var newAddress = new Address {
                        IsPrimary    = true,
                        AddressLine1 = Input.AddressLine1,
                        AddressLine2 = Input.AddressLine2,
                        City         = Input.City,
                        Country      = Input.Country,
                        State        = Input.State,
                        Customer     = recentCustomer,
                        ZipCode      = Convert.ToInt32(Input.ZipCode)
                    };
                    _context.Add(newAddress);
                }
                else
                {
                    Address recentAddress = new Address();
                    foreach (var add in address)
                    {
                        recentAddress = add;
                    }
                    //var recentAddress =await  _context.Address.FindAsync(addressId);
                    recentAddress.AddressLine1 = Input.AddressLine1;
                    recentAddress.AddressLine2 = Input.AddressLine2;
                    recentAddress.City         = Input.City;
                    recentAddress.Country      = Input.Country;

                    recentAddress.ZipCode   = Convert.ToInt32(Input.ZipCode);
                    recentAddress.State     = Input.State;
                    recentAddress.Customer  = recentCustomer;
                    recentAddress.IsPrimary = true;
                    _context.Update(recentAddress);
                }
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage());
        }
        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());
        }
Exemplo n.º 6
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();
        }