Ejemplo n.º 1
0
        public Friend ChangeFriendStatus(int userId, FriendStatus status)
        {
            // TODO: Write code to test all the new functions in UsersManager and FriendsRepository
            return(Operation(() =>
            {
                CheckIsUserAuthenticated();
                CheckDoesUserExist(userId);

                if (!serverManager.DoesFriendExist(LoggedUser.Id, userId))
                {
                    throw new Exception("No such friendship exists");
                }

                var friend = serverManager.GetExistingFriend(LoggedUser.Id, userId);

                if (!friend.IsChangePossible(status, LoggedUser.Id))
                {
                    throw new Exception("Friend status change is not possible");
                }

                // Updating the database with the changed status
                serverManager.ChangeFriendStatus(friend, status);

                // Notifying the other user the friend status has changed
                var friendUserId = friend.UserId1 != LoggedUser.Id ? friend.UserId1 : friend.UserId2;
                NotifyUserFriendStatusChanged(friendUserId, friend, LoggedUser);

                return friend;
            }));
        }