From 84961acf2520bf7df3dab8c38de287a62313253d Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 19:34:38 +0200 Subject: Implemented HttpPatch --- src/DevHive.Services/Interfaces/IUserService.cs | 3 + src/DevHive.Services/Services/UserService.cs | 85 +++++++++++++++++-------- src/DevHive.Web/Controllers/UserController.cs | 13 ++++ 3 files changed, 74 insertions(+), 27 deletions(-) diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index ef22000..121fec3 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -1,7 +1,9 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Identity; +using DevHive.Data.Models; using DevHive.Services.Models.Identity.User; +using Microsoft.AspNetCore.JsonPatch; namespace DevHive.Services.Interfaces { @@ -16,6 +18,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); + Task PatchUser(Guid id, JsonPatchDocument jsonPatch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index bf18007..a8b9ef9 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -13,7 +13,8 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; -using DevHive.Services.Models.Language; +using Microsoft.AspNetCore.JsonPatch; +using System.Linq; namespace DevHive.Services.Services { @@ -142,18 +143,20 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - List properLanguages = new(); + List languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - properLanguages.Add(await this._languageRepository.GetByNameAsync(lang.Name)); + languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? + throw new ArgumentException("Invalid language name!")); - List properTechnologies = new(); + List technologies = new(); foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - properTechnologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name)); + technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? + throw new ArgumentException("Invalid technology name!")); User user = this._userMapper.Map(updateUserServiceModel); - user.Languages = properLanguages; - user.Technologies = properTechnologies; + user.Languages = languages; + user.Technologies = technologies; bool success = await this._userRepository.EditAsync(user); @@ -163,34 +166,32 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); ; } - private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) + public async Task PatchUser(Guid id, JsonPatchDocument jsonPatch) { - // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) - { - User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); + User user = await this._userRepository.GetByIdAsync(id) ?? + throw new ArgumentException("User does not exist!"); - if (returnedFriend == null) - throw new ArgumentException($"User {friend.Name} does not exist!"); - } + var password = jsonPatch.Operations + .Where(x => x.path == "/password") + .Select(x => x.value) + .FirstOrDefault(); - // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + if(password != null) { - Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - - if (default(Language) == returnedLanguage) - throw new ArgumentException($"Language {language.Name} does not exist!"); + string passwordHash = this.GeneratePasswordHash(password.ToString()); + user.PasswordHash = passwordHash; } + else + jsonPatch.ApplyTo(user); - // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + bool success = await this._userRepository.EditAsync(user); + if (success) { - Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - - if (default(Technology) == returnedTechnology) - throw new ArgumentException($"Technology {technology.Name} does not exist!"); + user = await this._userRepository.GetByIdAsync(id); + return this._userMapper.Map(user); } + else + return null; } #endregion @@ -275,6 +276,36 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) + { + // Friends + foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + { + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); + + if (returnedFriend == null) + throw new ArgumentException($"User {friend.Name} does not exist!"); + } + + // Languages + foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + { + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); + + if (default(Language) == returnedLanguage) + throw new ArgumentException($"Language {language.Name} does not exist!"); + } + + // Technology + foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + { + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); + + if (default(Technology) == returnedTechnology) + throw new ArgumentException($"Technology {technology.Name} does not exist!"); + } + } + private string WriteJWTSecurityToken(Guid userId, IList roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index dc27cbf..7f4e80b 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -12,6 +12,8 @@ using DevHive.Services.Models.Language; using DevHive.Web.Models.Technology; using DevHive.Services.Models.Technology; using DevHive.Services.Interfaces; +using DevHive.Data.Models; +using Microsoft.AspNetCore.JsonPatch; namespace DevHive.Web.Controllers { @@ -101,6 +103,17 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } + + [HttpPatch] + public async Task Patch(Guid id, [FromBody] JsonPatchDocument jsonPatch) + { + UserServiceModel userServiceModel = await this._userService.PatchUser(id, jsonPatch); + + if (userServiceModel == null) + return new BadRequestObjectResult("Wrong patch properties"); + else + return new OkObjectResult(this._userMapper.Map(userServiceModel)); + } #endregion #region Delete -- cgit v1.2.3