示例#1
0
        // AcceptFriend <username1> <username2>
        public string Execute(string[] data)
        {
            string         userName = data[0];
            UserFriendsDto userDTO  = userService.ByUsername <UserFriendsDto>(userName);

            if (userDTO == null || userDTO.IsDeleted == true)
            {
                throw new ObjectNotFoundException(typeof(User).Name, userName);
            }
            string         friendName = data[1];
            UserFriendsDto friendDTO  = userService.ByUsername <UserFriendsDto>(friendName);

            if (friendDTO == null || friendDTO.IsDeleted == true)
            {
                throw new ObjectNotFoundException(typeof(User).Name, friendName);
            }
            if (!friendDTO.Friends.Any(f => f.FriendId == userDTO.Id))
            {
                throw new FriendRequestMissingException(friendName, userName);
            }
            if (userDTO.Friends.Any(f => f.FriendId == friendDTO.Id))
            {
                throw new UsersAlreadyFriendsException(friendName, userName);
            }
            Friendship confirmation = userService.AcceptFriend(userDTO.Id, friendDTO.Id);

            return(String.Format(SuccessMessage, userName, friendName));
        }
        public string Execute(string[] data)
        {
            string userName   = data[0];
            string friendName = data[1];

            if (!userSessionService.IsLoggedIn())
            {
                throw new ArgumentException("You are not logged in!");
            }

            bool isUserExist   = userService.Exists(userName);
            bool isFriendExist = userService.Exists(friendName);

            if (!isUserExist)
            {
                throw new ArgumentException($"{userName} not found!");
            }

            if (!isFriendExist)
            {
                throw new ArgumentException($"{friendName} not found!");
            }

            UserFriendsDto userDto    = userService.ByUsername <UserFriendsDto>(userName);
            UserFriendsDto FriendsDto = userService.ByUsername <UserFriendsDto>(friendName);

            bool isUserRequestSend   = userDto.Friends.Any(x => x.Username == friendName);
            bool isFriendRequestSend = FriendsDto.Friends.Any(x => x.Username == userName);

            if (isUserRequestSend && isFriendRequestSend)
            {
                throw new InvalidOperationException($"{friendName} is already a friend to {userName}");
            }
            else if (!isFriendRequestSend)
            {
                throw new InvalidOperationException($"{friendName} has not added {userName} as a friend");
            }

            int userId   = userService.ByUsername <UserDto>(userName).Id;
            int friendId = userService.ByUsername <UserDto>(friendName).Id;

            userService.AcceptFriend(userId, friendId);

            return($"{userName} accepted {friendName} as a friend");
        }
示例#3
0
        // AcceptFriend <username1> <username2>
        public string Execute(string[] data)
        {
            var username       = data[0];
            var friendUsername = data[1];

            var userExists   = this.userService.Exists(username);
            var friendExists = this.userService.Exists(friendUsername);

            if (!userExists)
            {
                throw new ArgumentException($"{username} not found!");
            }
            else if (!friendExists)
            {
                throw new ArgumentException($"{friendUsername} not found!");
            }
            if (!userSessionService.IsLoggedIn() || userSessionService.User.Username != data[0])
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            var user   = this.userService.ByUsername <UserFriendsDto>(username);
            var friend = this.userService.ByUsername <UserFriendsDto>(friendUsername);

            bool isRequestSendFromUser   = user.Friends.Any(x => x.Username == friend.Username);
            bool isRequestSendFromFriend = friend.Friends.Any(x => x.Username == user.Username);

            if (isRequestSendFromUser && isRequestSendFromFriend)
            {
                throw new InvalidOperationException($"{user.Username} is already a friend to {friend.Username}");
            }

            else if (!isRequestSendFromUser && !isRequestSendFromFriend)
            {
                throw new InvalidOperationException($"{user.Username} has not added {friend.Username} as a friend");
            }

            userService.AcceptFriend(user.Id, friend.Id);

            return($"{user.Username} accepted {friend.Username} as a friend");
        }
        // AcceptFriend <username1> <username2>
        public string Execute(string[] data)
        {
            var username   = data[0];
            var friendName = data[1];

            if (username != Session.CurrentUser.Username)
            {
                return(OperationNotAllowed);
            }

            if (username == friendName)
            {
                return(String.Format(CannotBefriendYoursef, username));
            }

            var user   = userService.ByUsername <User>(username);
            var friend = userService.ByUsername <User>(friendName);

            if (user == null)
            {
                return(String.Format(UserNotFound, username));
            }

            if (friend == null)
            {
                return(String.Format(UserNotFound, friendName));
            }

            try
            {
                userService.AcceptFriend(user.Id, friend.Id);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return(String.Format(SuccessMessage, username, friendName));
        }
示例#5
0
 public async Task <bool> AcceptFriend(int id)
 {
     return(await _userService.AcceptFriend(id));
 }