diff options
Diffstat (limited to 'src')
5 files changed, 50 insertions, 0 deletions
diff --git a/src/Services/DevHive.Services/Interfaces/IUserService.cs b/src/Services/DevHive.Services/Interfaces/IUserService.cs index a55f9dd..47948f4 100644 --- a/src/Services/DevHive.Services/Interfaces/IUserService.cs +++ b/src/Services/DevHive.Services/Interfaces/IUserService.cs @@ -52,6 +52,8 @@ namespace DevHive.Services.Interfaces /// <returns>The new picture's URL</returns> Task<ProfilePictureServiceModel> UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); + Task<bool> AddFriend(Guid id, UpdateFriendServiceModel updateFriendServiceModel); + /// <summary> /// Deletes a user from the database and removes his data entirely /// Requires authenticated user diff --git a/src/Services/DevHive.Services/Services/UserService.cs b/src/Services/DevHive.Services/Services/UserService.cs index 4f74b06..ed8940e 100644 --- a/src/Services/DevHive.Services/Services/UserService.cs +++ b/src/Services/DevHive.Services/Services/UserService.cs @@ -143,6 +143,21 @@ namespace DevHive.Services.Services } #endregion + #region Friends + public async Task<bool> AddFriend(Guid id, UpdateFriendServiceModel updateFriendServiceModel) + { + User user = await this._userRepository.GetByIdAsync(id) ?? + throw new ArgumentException("Cannot add friend to user, because user does not exist!"); + + if (!await this._userRepository.DoesUserExistAsync(updateFriendServiceModel.Id)) + throw new ArgumentException("Cannot add friend to user, because friend does not exist!"); + + await this.CreateRelationToFriends(user, new List<UpdateFriendServiceModel> { updateFriendServiceModel }); + + return await this._userRepository.EditAsync(id, user); + } + #endregion + #region Delete public async Task<bool> DeleteUser(Guid id) { diff --git a/src/Web/DevHive.Web.Models/User/UpdateFriendWebModel.cs b/src/Web/DevHive.Web.Models/User/UpdateFriendWebModel.cs new file mode 100644 index 0000000..84821f9 --- /dev/null +++ b/src/Web/DevHive.Web.Models/User/UpdateFriendWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.User +{ + public class UpdateFriendWebModel + { + public Guid Id { get; set; } + public string UserName { get; set; } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs index 14aaa3a..1c193e8 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -28,6 +28,9 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<UpdateUserServiceModel, UpdateUserWebModel>(); CreateMap<FriendServiceModel, UsernameWebModel>(); + + // Friends + CreateMap<UpdateFriendWebModel, UpdateFriendServiceModel>(); } } } diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs index 4d01447..2ed8e54 100644 --- a/src/Web/DevHive.Web/Controllers/UserController.cs +++ b/src/Web/DevHive.Web/Controllers/UserController.cs @@ -133,6 +133,26 @@ namespace DevHive.Web.Controllers } #endregion + #region Friends + [HttpPost] + [Route("AddFriend")] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> AddFriend(Guid id, [FromBody] UpdateFriendWebModel updateFriendWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + UpdateFriendServiceModel updateFriendServiceModel = this._userMapper.Map<UpdateFriendServiceModel>(updateFriendWebModel); + + bool result = await this._userService.AddFriend(id, updateFriendServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not add User as a friend"); + + return new OkResult(); + } + #endregion + #region Delete /// <summary> /// Delete a User with his Id. A PUSTINQK can only delete his account. An Admin can delete all accounts |
