Exemple #1
0
        /// <summary>
        /// OnGet - This method performs all of the logic necessary to query the databases and then have information available to display to the page upon loading. In this case that includes the items present in the user's shopping cart near the time of purchase as well as seeded credit card details to be used for Sandbox most test purchases (no real credit cards can be used to make purchases on this site).
        /// </summary>
        /// <returns>The completed task, all necessary information is presented to the page upon page load.</returns>
        public async Task <IActionResult> OnGet()
        {
            Cart cart = await _cart.GetCartForUserByEmail(GetUserEmail());

            // logic here
            if (cart == null)
            {
                await _cart.Create(GetUserEmail());

                return(RedirectToPagePermanent("Index", "Products"));
            }

            decimal totalPrice = 0;

            foreach (var item in cart.CartItems)
            {
                totalPrice += item.Product.Price * item.Quantity;
            }

            Total = totalPrice;

            CurrentCartId   = cart.Id;
            CurrentUserCart = cart;

            var VISA       = _config["VISATestNumber"];
            var Mastercard = _config["MastercardTestNumber"];
            var Discover   = _config["DiscoverTestNumber"];

            return(Page());
        }
Exemple #2
0
        /// <summary>
        /// OnGet - This method performs the logic necessary to render all of the user's shopping cart information (with contents included) to the front end upon page load.
        /// </summary>
        /// <returns>The task complete, user's complete shopping cart data is available to be used on the front end. If for some reason a user made it this far without being assigned a shopping cart, one is instantiated in this method.</returns>
        public async Task <IActionResult> OnGet()
        {
            Cart cart = await _cart.GetCartForUserByEmail(GetUserEmail());

            if (cart == null)
            {
                await _cart.Create(GetUserEmail());
            }

            decimal totalPrice = 0;

            if (cart != null)
            {
                foreach (var item in cart.CartItems)
                {
                    totalPrice += item.Product.Price * item.Quantity;
                }
                cart.Total = totalPrice;
                await _cart.Update(cart);

                Total = totalPrice;

                CurrentCartId   = cart.Id;
                CurrentUserCart = cart;
            }

            return(Page());
        }
        public CartCreateResponse CreateCart(CartCreateRequest request)
        {
            var response         = new CartCreateResponse();
            var validationErrors = _cartValidation.CartCreateRequestValidation.Validate(request);
            var dbErrors         = new List <DatabaseErrors>();

            if (validationErrors.Count != 0)
            {
                response.ValidationErrors = validationErrors;
            }
            else
            {
                try
                {
                    var cart = new Cart
                    {
                        Id     = request.CartId,
                        UserId = request.UserId
                    };

                    response.Cart = _cartRepository.Create(cart);
                }
                catch (SqlException)
                {
                    dbErrors.Add(DatabaseErrors.DB_CONNECTION_FAILED);
                }
                response.DBErrors = dbErrors;
            }
            return(response);
        }
Exemple #4
0
        /// <summary>
        /// OnGet - This page displays information about the contents of a user's shopping cart upon page load.
        /// </summary>
        /// <param name="Id">The inputted Id of the cart associated with the specified user.</param>
        /// <returns>The completed task, the user's shopping cart contents displayed along with products info.</returns>
        public async Task <IActionResult> OnGet(int Id)
        {
            Product = await _product.GetProduct(Id);

            var user = await _signInManager.UserManager.GetUserAsync(User);

            // TODO: Handle exception for if there is no user logged in
            if (user != null)
            {
                Cart cart = await _cart.GetCartForUserByEmail(GetUserEmail());

                // logic here
                if (cart == null)
                {
                    cart = await _cart.Create(GetUserEmail());
                }

                var items = await _cartItems.GetAllCartItems(cart.Id);

                CurrentCartId = cart.Id;
            }

            return(Page());
        }
        /// <summary>
        /// Gets the flummery's for the cart
        /// </summary>
        /// <param name="itemId">
        /// int: the item ID of the flummery to be updated
        /// </param>
        /// <param name="userId">
        /// string: the current user's ID
        /// </param>
        /// <param name="qty">
        /// int: the new quantity, defaults to 1 if no value is passed
        /// </param>
        /// <returns>
        /// Task<IActionResult>: redirects back to the Cart
        /// </returns>
        public async Task <IActionResult> OnGet(int itemId, string userId, int qty = 1)
        {
            if (qty < 1)
            {
                qty = 1;
            }
            else if (qty > 99)
            {
                qty = 99;
            }
            var currCart = await _cart.GetUserCart(userId);

            if (currCart == null)
            {
                var newCart = new Models.Cart
                {
                    UserId = userId
                };
                currCart = await _cart.Create(newCart);
            }
            await UpdateCartItems(currCart, itemId, qty);

            return(RedirectToPage("/Cart/View"));
        }
        public async Task <IActionResult> Register(RegisterViewModel rvm)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName       = rvm.Email,
                    Email          = rvm.Email,
                    FirstName      = rvm.FirstName,
                    LastName       = rvm.LastName,
                    Birthdate      = rvm.Birthdate,
                    Address        = rvm.Address,
                    RegisteredDate = DateTime.Now
                };

                var result = await _userManager.CreateAsync(user, rvm.Password);

                if (result.Succeeded)
                {
                    Cart cart = new Cart();
                    cart.UserID = user.Id;
                    await _cart.Create(cart);

                    Claim fullNameClaim = new Claim("FullName", $"{user.FirstName} {user.LastName}");

                    Claim birthdayClaim = new Claim(ClaimTypes.DateOfBirth, new DateTime(user.Birthdate.Year, user.Birthdate.Month, user.Birthdate.Day).ToString("u"),
                                                    ClaimValueTypes.DateTime);

                    Claim emailClaim = new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.Email);

                    Claim addressClaim = new Claim(ClaimTypes.StreetAddress, $"{ user.Address }");

                    Claim registerDateClaim = new Claim("RegisteredDate", $"{ user.RegisteredDate }");

                    List <Claim> claims = new List <Claim> {
                        fullNameClaim, birthdayClaim, emailClaim, addressClaim, registerDateClaim
                    };

                    await _userManager.AddClaimsAsync(user, claims);

                    if (user.Email == "*****@*****.**" || user.Email == "*****@*****.**" || user.Email == "*****@*****.**" || user.Email == "*****@*****.**")
                    {
                        await _userManager.AddToRoleAsync(user, ApplicationRoles.Admin);

                        await _userManager.AddToRoleAsync(user, ApplicationRoles.Member);
                    }
                    else
                    {
                        await _userManager.AddToRoleAsync(user, ApplicationRoles.Member);
                    }

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    //send user email after successfully registered with us
                    await _emailSender.SendEmailAsync(rvm.Email, "Successfully registered with us!", "<p>Thank you for registering</p>");

                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View(rvm));
        }
        public async Task <ActionResult <CreateCart> > Create(CreateCart cart)
        {
            await _cart.Create(cart);

            return(CreatedAtAction("GetCartItems", new { id = cart.Id }, cart));
        }