Beispiel #1
0
        public async Task <IActionResult> OnGetAcceptAsync(Guid reviewId)
        {
            var user = await _userManager.GetUserAsync(User);

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

            var review = await _context.Reviews.FirstOrDefaultAsync(x => x.Id == reviewId);

            if (review == null)
            {
                StatusMessage = $"Error: Unable to find review.";
                return(RedirectToPage());
            }

            if (review.ReviewStatus == ReviewStatus.Accepted)
            {
                StatusMessage = $"Error: You have already accepted the '{_context.Reviews.Include(x => x.Game).FirstOrDefault(x => x.Id == reviewId).Game.Title}' review.";
                return(RedirectToPage());
            }

            try
            {
                review.ReviewStatus = ReviewStatus.Accepted;
                _context.Update(review);

                await _context.SaveChangesAsync();

                StatusMessage = $"You have accepted the '{_context.Reviews.Include(x => x.Game).FirstOrDefault(x => x.Id == reviewId).Game.Title}' review.";
                return(RedirectToPage());
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }

            return(RedirectToPage());
        }
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());
        }