public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToPage("/Account/Login"));
            }

            if (!await _context.HasCartAsync(user))
            {
                return(RedirectToPage("./Index"));
            }

            if (BillingAddress.Id != null && BillingAddress.Id != Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                BillingAddress = await _context.Addresses.FirstOrDefaultAsync(a => a.Id == BillingAddress.Id);
            }

            if (ShippingAddress.Id != null && ShippingAddress.Id != Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                ShippingAddress = await _context.Addresses.FirstOrDefaultAsync(a => a.Id == ShippingAddress.Id);
            }

            if (Payment.Id != null && Payment.Id != Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                Payment = await _context.Payments.FirstOrDefaultAsync(a => a.Id == Payment.Id);
            }

            if (IsSameAddress)
            {
                ShippingAddress = BillingAddress;
            }

            ModelState.Clear();

            if (!TryValidateModel(BillingAddress))
            {
                Debug.WriteLine("BillingAddress fail");
            }

            if (!TryValidateModel(ShippingAddress))
            {
                Debug.WriteLine("ShippingAddress fail");
            }

            if (!TryValidateModel(Payment))
            {
                Debug.WriteLine("Payment fail");
            }

            if (!ModelState.IsValid)
            {
                StatusMessage = $"Error: Unable to purchase game(s). Please check your information and try again.";
                return(RedirectToPage());
            }

            var cart = await _context.GetCartAsync(user);

            CartCount = await _context.GetCartItemCount(cart);

            if (CartCount == 0)
            {
                return(RedirectToPage("./Index"));
            }

            var cartGames = _context.CartGames.Include(x => x.Game).Where(x => x.CartId == cart.Id);

            if (cartGames == null)
            {
                return(RedirectToPage("./Index"));
            }

            try
            {
                cart.IsCheckedOut = true;
                _context.Carts.Update(cart);

                foreach (var item in cartGames)
                {
                    await _context.AddAsync(new UserGame
                    {
                        GameId = item.Game.Id,
                        UserId = user.Id
                    });
                }

                await _context.SaveChangesAsync();

                return(RedirectToPage("./Confirmation", new { cartId = cart.Id }));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }

            return(Page());
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAddFriendAsync(string username)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToPage("/Account/Login"));
            }

            var friendships = _context.Friendships
                              .Include(x => x.Receiver)
                              .Include(x => x.Sender)
                              .Where(x => x.ReceiverId == user.Id || x.SenderId == user.Id);

            if (friendships.Any(x => (x.Sender.UserName == username || x.Receiver.UserName == username) && x.RequestStatus == FriendStatusCode.Pending))
            {
                StatusMessage = $"Error: There is already a pending friend request for that user.";

                return(RedirectToPage("./Index"));
            }
            else if (friendships.Any(x => (x.Sender.UserName == username || x.Receiver.UserName == username) && x.RequestStatus == FriendStatusCode.Accepted))
            {
                StatusMessage = $"Error: You already have this user added as a friend.";

                return(RedirectToPage("./Index"));
            }

            var receiver = await _userManager.FindByNameAsync(username);

            if (friendships.Any(x => (x.Sender.UserName == username || x.Receiver.UserName == username) && x.RequestStatus == FriendStatusCode.Rejected))
            {
                var friendship = await friendships.FirstOrDefaultAsync(x => (x.Sender.UserName == username || x.Receiver.UserName == username) && x.RequestStatus == FriendStatusCode.Rejected);

                friendship.RequestStatus = FriendStatusCode.Pending;
                friendship.IsFamily      = false;

                try
                {
                    _context.Update(friendship);
                    await _context.SaveChangesAsync();

                    StatusMessage = $"You have sent them a friend request. You must wait for them to accept it.";
                    return(RedirectToPage("./Index"));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.InnerException);
                }
            }

            var newFriendship = new Friendship
            {
                SenderId   = user.Id,
                ReceiverId = receiver.Id,
                IsFamily   = false
            };

            try
            {
                await _context.AddAsync(newFriendship);

                await _context.SaveChangesAsync();

                StatusMessage = $"You have sent them a friend request. You must wait for them to accept it.";
                return(RedirectToPage("./Index"));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }

            return(Page());
        }