/// <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()); }
// have each cart item so we need to foreach over the cart itself // cart => cartitems => iterate over cart items and display one by one public async Task OnGet() { var user = User.Identity.Name; CartItems = await _cart.GetAllCartItems(user); }
/// <summary> /// OnPost - Upon posting to this page (user checkout) a number of actions take place. This method constructs all of the pieces necessary for the most important operation: the purchase. This method assembles all of the final details needed to make the purchase including the user's shopping cart, to total price of all of the items in the cart, the billing/order address, and the credit card being used. Then finally a call is made to the payment service, the card gets ran, and the order is placed. The user inputs their billing information into a form, selects from one of the three seeded credit cards and then makes their purchase. /// </summary> /// <param name="firstName">Billing First Name</param> /// <param name="lastName">Billing Last Name</param> /// <param name="address">Billing Address</param> /// <param name="city">Billing City</param> /// <param name="state">Billing State</param> /// <param name="zip">Billing ZIP</param> /// <param name="cardNumber">Credit Card</param> /// <returns>The task complete, a successful (or unsuccessful) transaction has taken place.</returns> public async Task <IActionResult> OnPost(string firstName, string lastName, string address, string city, string state, int zip) { Cart cart = await _cart.GetCartForUserByEmail(GetUserEmail()); if (cart == null) { await _cart.Create(GetUserEmail()); } decimal totalPrice = 0; foreach (var item in cart.CartItems) { totalPrice += item.Product.Price * item.Quantity; } cart.Total = totalPrice; Total = totalPrice; var user = await _signInManager.UserManager.GetUserAsync(User); customerAddressType billingAddress = new customerAddressType() { firstName = firstName, lastName = lastName, email = user.UserName, address = address, city = city, state = state, zip = zip.ToString() }; creditCardType creditCard = new creditCardType() { cardNumber = _config[$"{CardType}TestNumber"], expirationDate = _config["ExpDate"], cardCode = _config["CVV"] }; CurrentCartId = cart.Id; CurrentUserCart = cart; var paymentResult = _payment.Run(creditCard, billingAddress, cart); if (paymentResult.Successful) { // GET CART ITEMS HERE List <CartItems> cartItems = await _cartItems.GetAllCartItems(CurrentCartId); Order newOrder = new Order() { CartId = cart.Id, UserEmail = user.Email, FirstName = firstName, LastName = lastName, Address = address, City = city, State = state, Zip = zip, Total = cart.Total }; Order order = await _order.Create(newOrder); order.Date = DateTime.UtcNow; StringBuilder sb = new StringBuilder(); foreach (var item in cart.CartItems) { sb.Append($"<li>{item.Product.Name} (Quantity: {item.Quantity})</li>"); } ; string subject = "Your Order Summary"; string htmlMessage = $"<h1>Thank you for your purchase {user.FirstName}.</h1><p>Your summary: {sb.ToString()}</p><p>Total purchase cost: ${totalPrice}</p><p>Your order is now being processed and should be heading your way soon. We hope you're happy with your purchase!</p>"; await _emailSender.SendEmailAsync(user.UserName, subject, htmlMessage); cart.IsActive = false; await _cart.Update(cart); await _cart.Create(GetUserEmail()); return(RedirectToPage("Receipt")); } return(Page()); }
/// <summary> /// InvokeAsync - Method that allows the program to pull information about the user's current shopping cart items from the database in order to display them to the View Component. /// </summary> /// <param name="currentCartId">The Id number of the user's current cart.</param> /// <returns>The task complete, the user's current cart information (with all items currently contained), returned to the user.</returns> public async Task <IViewComponentResult> InvokeAsync(int currentCartId) { var cartItems = await _cartItems.GetAllCartItems(currentCartId); return(View(cartItems)); }