diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-04-08 09:29:49 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-04-08 09:29:49 +0300 |
| commit | ffc6b7cd6e454627c95044e88037b37d31dcfce3 (patch) | |
| tree | b2d1b5d1882f022d5420f8227010d025ba8ee5fd /src | |
| parent | 0a58b0c36f113b41984629aff45a9162305300a4 (diff) | |
| download | DevHive-ffc6b7cd6e454627c95044e88037b37d31dcfce3.tar DevHive-ffc6b7cd6e454627c95044e88037b37d31dcfce3.tar.gz DevHive-ffc6b7cd6e454627c95044e88037b37d31dcfce3.zip | |
Updated adding and removing friends to work with friend username, instead of friend id (temporary solution?)
Diffstat (limited to 'src')
3 files changed, 12 insertions, 12 deletions
diff --git a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs index e337793..52f23f3 100644 --- a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs +++ b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs @@ -5,7 +5,7 @@ namespace DevHive.Services.Interfaces { public interface IFriendsService { - Task<bool> AddFriend(Guid userId, Guid friendId); - Task<bool> RemoveFriend(Guid userId, Guid friendId); + Task<bool> AddFriend(Guid userId, string friendUsername); + Task<bool> RemoveFriend(Guid userId, string friendUsername); } } diff --git a/src/Services/DevHive.Services/Services/FriendsService.cs b/src/Services/DevHive.Services/Services/FriendsService.cs index 4e3e355..98f654b 100644 --- a/src/Services/DevHive.Services/Services/FriendsService.cs +++ b/src/Services/DevHive.Services/Services/FriendsService.cs @@ -16,29 +16,29 @@ namespace DevHive.Services.Services this._friendRepository = friendRepository; } - public async Task<bool> AddFriend(Guid userId, Guid friendId) + public async Task<bool> AddFriend(Guid userId, string friendUsername) { User user = await this._friendRepository.GetByIdAsync(userId) ?? throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); - User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + User friend = await this._friendRepository.GetByUsernameAsync(friendUsername) ?? throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); bool addedToUser = user.Friends.Add(friend) && await this._friendRepository.EditAsync(userId, user); - bool addedToFriend = friend.Friends.Add(user) && await this._friendRepository.EditAsync(friendId, friend); + bool addedToFriend = friend.Friends.Add(user) && await this._friendRepository.EditAsync(friend.Id, friend); return addedToUser && addedToFriend; } - public async Task<bool> RemoveFriend(Guid userId, Guid friendId) + public async Task<bool> RemoveFriend(Guid userId, string friendUsername) { User user = await this._friendRepository.GetByIdAsync(userId) ?? throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); - User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + User friend = await this._friendRepository.GetByUsernameAsync(friendUsername) ?? throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); bool addedToUser = user.Friends.Remove(friend) && await this._friendRepository.EditAsync(userId, user); - bool addedToFriend = friend.Friends.Remove(user) && await this._friendRepository.EditAsync(friendId, friend); + bool addedToFriend = friend.Friends.Remove(user) && await this._friendRepository.EditAsync(friend.Id, friend); return addedToUser && addedToFriend; } } diff --git a/src/Web/DevHive.Web/Controllers/FriendsController.cs b/src/Web/DevHive.Web/Controllers/FriendsController.cs index 9f3ec13..318ae64 100644 --- a/src/Web/DevHive.Web/Controllers/FriendsController.cs +++ b/src/Web/DevHive.Web/Controllers/FriendsController.cs @@ -29,24 +29,24 @@ namespace DevHive.Web.Controllers [HttpPost] [Authorize(Roles = "User,Admin")] - public async Task<IActionResult> AddFriend(Guid userId, Guid friendId, [FromHeader] string authorization) + public async Task<IActionResult> AddFriend(Guid userId, string friendUsername, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); - return (await this._friendsService.AddFriend(userId, friendId)) ? + return (await this._friendsService.AddFriend(userId, friendUsername)) ? new OkResult() : new BadRequestResult(); } [HttpDelete] [Authorize(Roles = "User,Admin")] - public async Task<IActionResult> RemoveFriend(Guid userId, Guid friendId, [FromHeader] string authorization) + public async Task<IActionResult> RemoveFriend(Guid userId, string friendUsername, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); - return (await this._friendsService.RemoveFriend(userId, friendId)) ? + return (await this._friendsService.RemoveFriend(userId, friendUsername)) ? new OkResult() : new BadRequestResult(); } |
