// AddFriend <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 (userDTO.Friends.Any(f => f.FriendId == friendDTO.Id)) { if (friendDTO.Friends.Any(f => f.FriendId == userDTO.Id)) { throw new UsersAlreadyFriendsException(friendName, userName); } else { throw new FriendRequestExistsException(userName, friendName); } } Friendship request = userService.AddFriend(userDTO.Id, friendDTO.Id); return(String.Format(SuccessMessage, userName, friendName)); }
public string Execute(string[] data) { string username = data[0]; bool exists = this.userService.Exists(username); if (!exists) { throw new ArgumentException(string.Format(USER_NOT_FOUND, username)); } UserFriendsDto userFriendsDto = this.userService.ByUsername<UserFriendsDto>(username); if (userFriendsDto.Friends.Count == 0) { return NO_FRIENDS; } StringBuilder builder = new StringBuilder(); builder.AppendLine("Friends:"); foreach (var friend in userFriendsDto.Friends) { builder.AppendLine(friend.Username); } return builder.ToString().TrimEnd(); }
public string Execute(string[] args) { if (args.Length != 1) { int commandNameLenght = this.GetType().Name.Length - "Command".Length; string commandName = this.GetType().Name.Substring(0, commandNameLenght); throw new ArgumentException($"Command {commandName} not valid"); } string username = args[0]; if (!this.userService.Exists(username)) { throw new ArgumentException($"User {username} not found!"); } UserFriendsDto user = this.userService.ByUsername <UserFriendsDto>(username); if (!user.Friends.Any()) { return("No friends for this user. :("); } StringBuilder sb = new StringBuilder(); sb.AppendLine("Friends:"); foreach (FriendDto friend in user.Friends) { sb.AppendLine($"-{friend.Username}"); } return(sb.ToString().TrimEnd()); }
private void CheckIfUserHaveFriends(UserFriendsDto userFriendsDto) { if (!userFriendsDto.Friends.Any()) { throw new ArgumentException(NO_FRIENDS); } }
private void CheckIfThereIfSuchFriendRequest(UserFriendsDto user, UserFriendsDto friend) { var friendHasSentRequest = friend.Friends.Any(f => f.Username.Equals(user.Username)); if (!friendHasSentRequest) { throw new InvalidOperationException(string.Format(NO_SUCH_REQUEST, friend.Username, user.Username)); } }
// AcceptFriend <username1> <username2> public string Execute(string[] data) { string username = data[0]; string friendName = data[1]; bool isLogged = this.userSessionService.IsLoggedIn(); if (!isLogged) { throw new InvalidCredentialsException(); } UserDto user = this.userService.ByUsername <UserDto>(username); int currentlyLoggedUserId = this.userSessionService.User.Id; if (user == null || currentlyLoggedUserId != user.Id) { throw new InvalidCredentialsException(); } bool userExists = this.userService.Exists(username); if (!userExists || user.IsDeleted == true) { throw new ArgumentException(string.Format(USER_NOT_FOUND, username)); } UserDto friend = this.userService.ByUsername <UserDto>(friendName); bool friendExists = this.userService.Exists(friendName); if (!friendExists || friend.IsDeleted == true) { throw new ArgumentException(string.Format(USER_NOT_FOUND, friendName)); } UserFriendsDto userFriendsDto = this.userService.ByUsername <UserFriendsDto>(username); UserFriendsDto friedFriendsDto = this.userService.ByUsername <UserFriendsDto>(friendName); bool userHasSentrequest = userFriendsDto.Friends.Any(f => f.Username == friedFriendsDto.Username); bool friendHasSentRequest = friedFriendsDto.Friends.Any(f => f.Username == userFriendsDto.Username); if (userHasSentrequest && friendHasSentRequest) { throw new InvalidOperationException(string.Format(ALREADY_FRIENDS, friendName, username)); } else if (!friendHasSentRequest) { throw new InvalidOperationException(string.Format(REQUEST_NOT_SENT, friendName, username)); } int userId = user.Id; int friendId = friend.Id; this.userService.AcceptFriend(userId, friendId); return(string.Format(SUCCESSFULLY_ACCEPTED, username, friendName)); }
private void CheckIfAlreadyFriends(UserFriendsDto user, UserFriendsDto friend) { var userHasSentRequest = user.Friends.Any(f => f.Id.Equals(friend.Id)); var friendHasSentRequest = friend.Friends.Any(f => f.Id.Equals(user.Id)); if (userHasSentRequest && friendHasSentRequest) { throw new InvalidOperationException(string.Format(ALREADY_FRIENDS, friend.Username, user.Username)); } }
private void CheckIfAlreadyFriends(UserFriendsDto userFrindsDto, UserFriendsDto friendFriendsDto) { var userHasSentRequest = userFrindsDto.Friends.Any(f => f.Id.Equals(friendFriendsDto.Id)); var friendHasSentRequest = friendFriendsDto.Friends.Any(f => f.Id.Equals(userFrindsDto.Id)); if (userHasSentRequest && friendHasSentRequest) { throw new InvalidOperationException(string.Format(AlreadyFriends, friendHasSentRequest, userHasSentRequest)); } if (userHasSentRequest && !friendHasSentRequest || !userHasSentRequest && friendHasSentRequest) { throw new InvalidOperationException(AlreadySentRequest); } }
// AcceptFriend <username1> <username2> public string Execute(string[] args) { if (args.Length < 2) { throw new ArgumentException("Command AcceptFriend not valid!"); } string username = args[0]; string friendUsername = args[1]; bool userExists = this.userService.Exists(username); if (!userExists) { throw new ArgumentException($"{username} not found!"); } bool friendExists = this.userService.Exists(friendUsername); if (!friendExists) { throw new ArgumentException($"{friendUsername} not found!"); } if (!this.userSessionService.IsLoggedIn || this.userSessionService.GetUsername != username) { throw new InvalidOperationException("Invalid credentials!"); } UserFriendsDto user = this.userService.ByUsername <UserFriendsDto>(username); UserFriendsDto friend = this.userService.ByUsername <UserFriendsDto>(friendUsername); bool isRequestSentFromUser = user.Friends.Any(f => f.Username == friendUsername); bool isRequestSentFromFriend = friend.Friends.Any(f => f.Username == username); if (isRequestSentFromUser && isRequestSentFromFriend) { throw new InvalidOperationException($"{friendUsername} is already a friend to {username}"); } if (!isRequestSentFromFriend) { throw new InvalidOperationException($"{friendUsername} has not added {username} as a friend"); } this.userService.AcceptFriend(user.Id, friend.Id); return($"Friend {username} accepted {friendUsername} as a friend"); }
// AddFriend <username1> <username2> public string Execute(string[] data) { if (data.Length != 2) { int commandNameLenght = this.GetType().Name.Length - "Command".Length; string commandName = this.GetType().Name.Substring(0, commandNameLenght); throw new ArgumentException($"Command {commandName} not valid"); } string username = data[0]; string friendUsername = data[1]; if (!this.userService.Exists(username)) { throw new ArgumentException($"User {username} not found!"); } if (!this.userService.Exists(friendUsername)) { throw new ArgumentException($"User {friendUsername} not found!"); } UserFriendsDto user = this.userService.ByUsername <UserFriendsDto>(username); UserFriendsDto friend = this.userService.ByUsername <UserFriendsDto>(friendUsername); bool isSentRequestFromUser = user.Friends.Any(f => f.Username == friend.Username); bool isSentRequestFromFriend = friend.Friends.Any(f => f.Username == user.Username); if (isSentRequestFromUser && isSentRequestFromFriend) { throw new InvalidOperationException($"{friendUsername} is already a friend to {username}"); } else if (isSentRequestFromUser && !isSentRequestFromFriend) { throw new InvalidOperationException("Request is already sent!"); } else if (!isSentRequestFromUser && isSentRequestFromFriend) { throw new InvalidOperationException("Request is already sent!"); } this.userService.AddFriend(user.Id, friend.Id); return($"Friend {friendUsername} added to {username}"); }
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"); }
// ListFriends <username> 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); } if (userDTO.Friends?.Count == 0) { throw new UserHasNoFriendsException(username); } StringBuilder friends = new StringBuilder(Environment.NewLine); foreach (var friend in userDTO.Friends) { friends.AppendLine($"-{friend.FriendName}"); } return(String.Format(SuccessMessage, friends.ToString().TrimEnd())); }
// AddFriend <username1> <username2> public string Execute(string[] data) { var requestSender = data[0]; var requestReciever = data[1]; if (userSessionService.User.Username != requestSender) { throw new InvalidOperationException("Invalid credentials!"); } if (!userService.Exists(requestSender)) { throw new ArgumentException($"{requestSender} not found!"); } if (!userService.Exists(requestReciever)) { throw new ArgumentException($"{requestReciever} not found!"); } UserFriendsDto userFriendsDto = userService.ByUsername <UserFriendsDto>(requestSender); UserFriendsDto addFriendsDto = userService.ByUsername <UserFriendsDto>(requestReciever); bool areFriends = userFriendsDto.Friends.Any(x => x.Username == addFriendsDto.Username) && addFriendsDto.Friends.Any(x => x.Username == userFriendsDto.Username); if (areFriends) { throw new InvalidOperationException($"{requestReciever} is already a friend to {requestSender}"); } if (userFriendsDto.Friends.Any(x => x.Username == requestSender)) { throw new InvalidOperationException($"{requestSender} already has invitation from {requestSender}"); } var senderId = userService.ByUsername <UserDto>(requestSender).Id; var recieverId = userService.ByUsername <UserDto>(requestReciever).Id; var friendShip = userService.AddFriend(senderId, recieverId); return($"Friend {requestReciever} added to {requestSender}"); }
public string Execute(string[] data) { string userName = data[0]; string friendName = data[1]; bool isUserExist = userService.Exists(userName); bool isFriendExist = userService.Exists(friendName); if (!userSessionService.IsLoggedIn()) { throw new ArgumentException("You are not logged in!"); } if (!isUserExist) { throw new ArgumentException($"{userName} not found!"); } if (!isFriendExist) { throw new ArgumentException($"{friendName} not found!"); } int userId = userService.ByUsername <UserDto>(userName).Id; int friendId = userService.ByUsername <UserDto>(friendName).Id; UserFriendsDto userFriendsDto = userService.ById <UserFriendsDto>(userId); bool isFriendAdded = userFriendsDto.Friends.Any(x => x.Username == friendName); if (isFriendAdded) { throw new InvalidOperationException($"{friendName} is already a friend to {userName}"); } userService.AddFriend(userId, friendId); return($"Friend {friendName} added to {userName}"); }
// AddFriend <username1> <username2> public string Execute(string[] data) { string username = data[0]; string friendUsername = data[1]; bool userExists = this.userService.Exists(username); bool friendExists = this.userService.Exists(friendUsername); if (!userExists) { throw new ArgumentException($"{username} not found!"); } if (!friendExists) { throw new ArgumentException($"{friendUsername} not found!"); } UserFriendsDto user = this.userService.ByUsername <UserFriendsDto>(username); UserFriendsDto friend = this.userService.ByUsername <UserFriendsDto>(friendUsername); bool isSendRequestFromUser = user.Friends.Any(u => u.Username == friend.Username); bool isSendRequestFromFriend = friend.Friends.Any(f => f.Username == user.Username); if (isSendRequestFromUser && isSendRequestFromFriend) { throw new InvalidOperationException($"{username} is already friend to {friendUsername}!"); } else if (isSendRequestFromUser && !isSendRequestFromUser) { throw new InvalidOperationException("Request is already sent!"); } this.userService.AddFriend(user.Id, friend.Id); return($"Friend {friend.Username} added to {user.Username}"); }
public string Execute(string[] args) { if (args.Length < 1) { throw new ArgumentException("Command PrintFriendsList not valid!"); } string username = args[0]; bool userExists = this.userService.Exists(username); if (!userExists) { throw new ArgumentException($"User {username} not found!"); } UserFriendsDto user = this.userService.ByUsername <UserFriendsDto>(username); var sb = new StringBuilder(); if (user.Friends.Count == 0) { sb.AppendLine("No friends for this user. :("); } else { sb.AppendLine("Friends:"); foreach (FriendDto friend in user.Friends.OrderBy(f => f.Username)) { sb.AppendLine($"-{friend.Username}"); } } return(sb.ToString().TrimEnd()); }