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));
        }
        public async Task <IActionResult> Delete(long id)
        {
            // needs rework, buggy rn
            //TODO Create new order_status (cancel), reassign to order
            var order = (from o in _context.Order where o.Order_Id == id select o).First();

            if (order == null)
            {
                return(NotFound());
            }

            var cancel_status = (from o in _context.OrderStatus where o.Status.Equals("Cancelled") select o).First();

            if (cancel_status == null)
            {
                // create it
                cancel_status = new OrderStatus
                {
                    Status = "Cancelled"
                };
                _context.OrderStatus.Add(cancel_status);
            }

            order.OrderStatus = cancel_status;
            _context.Order.Update(order);

            await _context.SaveChangesAsync();

            return(View());
        }
예제 #3
0
        public async Task <IActionResult> MoveToCart(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var cartItem = await _context.CartItem.FindAsync(id);

            if (cartItem == null)
            {
                return(NotFound());
            }
            cartItem.WantToBuy = true;
            _context.Update(cartItem);
            await _context.SaveChangesAsync();

            return(RedirectToAction("WishListIndex"));
        }
예제 #4
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());
        }
예제 #5
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());
        }
        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.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";
            }


            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 customer 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;

                Address recentAddress = new Address();
                foreach (var add in address)
                {
                    //addressId = add.Address_Id;
                    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());
        }