diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-19 15:47:01 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-19 15:47:01 +0200 |
| commit | fa09c0cc6cf99034dcc1301692ccb4d019087213 (patch) | |
| tree | a5d453243884a315e061ef7e3723fbf7bb68d43f /src/DevHive.Services/Services/UserService.cs | |
| parent | 3fc676497f4a4b1671e31cc3b8cd3e4c6ac96920 (diff) | |
| download | DevHive-fa09c0cc6cf99034dcc1301692ccb4d019087213.tar DevHive-fa09c0cc6cf99034dcc1301692ccb4d019087213.tar.gz DevHive-fa09c0cc6cf99034dcc1301692ccb4d019087213.zip | |
Moved Friends to User(you no longer have friends)
Diffstat (limited to 'src/DevHive.Services/Services/UserService.cs')
| -rw-r--r-- | src/DevHive.Services/Services/UserService.cs | 73 |
1 files changed, 62 insertions, 11 deletions
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 99a8d9f..7fd0682 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -67,6 +67,19 @@ namespace DevHive.Services.Services return new TokenModel(WriteJWTSecurityToken(user.UserName, user.Roles)); } + public async Task<bool> AddFriend(Guid userId, Guid friendId) + { + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + if (DoesUserHaveThisFriend(user, friend)) + throw new ArgumentException("Friend already exists in your friends list."); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriendAsync(user, friend) : + throw new ArgumentException("Invalid user!"); + } + public async Task<UserServiceModel> GetUserById(Guid id) { User user = await this._userRepository.GetByIdAsync(id) @@ -75,6 +88,16 @@ namespace DevHive.Services.Services return this._userMapper.Map<UserServiceModel>(user); } + public async Task<UserServiceModel> GetFriendById(Guid friendId) + { + if(!_userRepository.DoesUserExist(friendId)) + throw new ArgumentException("User does not exist!"); + + User friend = await this._userRepository.GetByIdAsync(friendId); + + return this._userMapper.Map<UserServiceModel>(friend); + } + public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel) { if (!this._userRepository.DoesUserExist(updateModel.Id)) @@ -105,6 +128,24 @@ namespace DevHive.Services.Services throw new InvalidOperationException("Unable to delete user!"); } + public async Task<bool> RemoveFriend(Guid userId, Guid friendId) + { + if(!this._userRepository.DoesUserExist(userId) && + !this._userRepository.DoesUserExist(friendId)) + throw new ArgumentException("Invalid user!"); + + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + if(!this.DoesUserHaveFriends(user)) + throw new ArgumentException("User does not have any friends."); + + if (!DoesUserHaveThisFriend(user, friend)) + throw new ArgumentException("This ain't your friend, amigo."); + + return await this.RemoveFriend(user.Id, friendId); + } + public async Task<bool> ValidJWT(Guid id, string rawTokenData) { // There is authorization name in the beginning, i.e. "Bearer eyJh..." @@ -143,6 +184,27 @@ namespace DevHive.Services.Services return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); } + private bool DoesUserHaveThisFriend(User user, User friend) + { + return user.Friends.Contains(friend); + } + + private bool DoesUserHaveFriends(User user) + { + return user.Friends.Count >= 1; + } + + private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) + { + List<string> toReturn = new(); + + foreach(var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + private string WriteJWTSecurityToken(string userName, IList<Role> roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -170,16 +232,5 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) - { - List<string> toReturn = new(); - - foreach(var claim in claims) - if (claim.Type == type) - toReturn.Add(claim.Value); - - return toReturn; - } } } |
