From c43a7665b9febe890eb6dac143b62c36464cc94d Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 12:24:17 +0200 Subject: Moved Lang&Tech CRUD to Update in User Layer --- src/DevHive.Services/Interfaces/IUserService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 5ef141f..0f834e9 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -16,7 +16,7 @@ namespace DevHive.Services.Interfaces Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel); Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel); - Task GetFriendById(Guid friendId); + Task GetFriend(string username); Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); -- cgit v1.2.3 From 797d034dce057cf2aaec1574ee0b640b1d570416 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 12:44:46 +0200 Subject: Made Lang&Tech layers consistant; Merged C**D methods to Update --- .../Interfaces/ILanguageService.cs | 4 +- src/DevHive.Services/Interfaces/IUserService.cs | 4 - src/DevHive.Services/Services/LanguageService.cs | 10 +-- src/DevHive.Services/Services/TechnologyService.cs | 10 +-- src/DevHive.Services/Services/UserService.cs | 93 ++-------------------- src/DevHive.Web/Controllers/LanguageController.cs | 12 +-- .../Controllers/TechnologyController.cs | 12 +-- 7 files changed, 32 insertions(+), 113 deletions(-) (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index 08b3812..1b39dfb 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -8,10 +8,10 @@ namespace DevHive.Services.Interfaces { Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); - Task GetLanguageById(Guid languageId); + Task GetLanguageById(Guid id); Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); - Task DeleteLanguage(Guid languageId); + Task DeleteLanguage(Guid id); } } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 0f834e9..19bb939 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -13,8 +13,6 @@ namespace DevHive.Services.Interfaces Task RegisterUser(RegisterServiceModel registerModel); Task AddFriend(Guid userId, Guid friendId); - Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel); - Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel); Task GetFriend(string username); Task GetUserById(Guid id); @@ -23,8 +21,6 @@ namespace DevHive.Services.Interfaces Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); - Task RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel); - Task RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel); Task ValidJWT(Guid id, string rawTokenData); } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 5b697cd..12e230e 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -35,9 +35,9 @@ namespace DevHive.Services.Services #region Read - public async Task GetLanguageById(Guid languageId) + public async Task GetLanguageById(Guid id) { - Language language = await this._languageRepository.GetByIdAsync(languageId); + Language language = await this._languageRepository.GetByIdAsync(id); if (language == null) throw new ArgumentException("The language does not exist"); @@ -66,12 +66,12 @@ namespace DevHive.Services.Services #region Delete - public async Task DeleteLanguage(Guid languageId) + public async Task DeleteLanguage(Guid id) { - if (!await this._languageRepository.DoesLanguageExistAsync(languageId)) + if (!await this._languageRepository.DoesLanguageExistAsync(id)) throw new ArgumentException("Language does not exist!"); - Language language = await this._languageRepository.GetByIdAsync(languageId); + Language language = await this._languageRepository.GetByIdAsync(id); return await this._languageRepository.DeleteAsync(language); } #endregion diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index c088281..4e74c83 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -35,9 +35,9 @@ namespace DevHive.Services.Services #region Read - public async Task GetTechnologyById(Guid technologyId) + public async Task GetTechnologyById(Guid id) { - Technology technology = await this._technologyRepository.GetByIdAsync(technologyId); + Technology technology = await this._technologyRepository.GetByIdAsync(id); if (technology == null) throw new ArgumentException("The technology does not exist"); @@ -64,12 +64,12 @@ namespace DevHive.Services.Services #endregion #region Delete - public async Task DeleteTechnology(Guid technologyId) + public async Task DeleteTechnology(Guid id) { - if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId)) + if (!await this._technologyRepository.DoesTechnologyExistAsync(id)) throw new ArgumentException("Technology does not exist!"); - Technology technology = await this._technologyRepository.GetByIdAsync(technologyId); + Technology technology = await this._technologyRepository.GetByIdAsync(id); bool result = await this._technologyRepository.DeleteAsync(technology); return result; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index d9e87e0..cbcb6b3 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -108,46 +108,6 @@ namespace DevHive.Services.Services await this._userRepository.AddFriendToUserAsync(user, friend) : throw new ArgumentException("Invalid user!"); } - - public async Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel) - { - bool userExists = await this._userRepository.DoesUserExistAsync(userId); - bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); - - if (!userExists) - throw new ArgumentException("User does not exist!"); - - if (!languageExists) - throw new ArgumentException("Language does noy exist!"); - - User user = await this._userRepository.GetByIdAsync(userId); - Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id); - - if (this._userRepository.DoesUserHaveThisLanguage(user, language)) - throw new ArgumentException("User already has this language!"); - - return await this._userRepository.AddLanguageToUserAsync(user, language); - } - - public async Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel) - { - bool userExists = await this._userRepository.DoesUserExistAsync(userId); - bool technologyExists = await this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id); - - if (!userExists) - throw new ArgumentException("User does not exist!"); - - if (!technologyExists) - throw new ArgumentException("Technology does not exist!"); - - Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id); - User user = await this._userRepository.GetByIdAsync(userId); - - if (this._userRepository.DoesUserHaveThisTechnology(user, technology)) - throw new ArgumentException("User already has this language!"); - - return await this._userRepository.AddTechnologyToUserAsync(user, technology); - } #endregion #region Read @@ -182,6 +142,8 @@ namespace DevHive.Services.Services && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) throw new ArgumentException("Username already exists!"); + //Add validations for everything else + User user = this._userMapper.Map(updateModel); bool result = await this._userRepository.EditAsync(user); @@ -208,61 +170,22 @@ namespace DevHive.Services.Services public async Task RemoveFriend(Guid userId, Guid friendId) { - Task userExists = this._userRepository.DoesUserExistAsync(userId); - Task friendExists = this._userRepository.DoesUserExistAsync(friendId); - - await Task.WhenAll(userExists, friendExists); + bool userExists = await this._userRepository.DoesUserExistAsync(userId); + bool friendExists = await this._userRepository.DoesUserExistAsync(friendId); - if (!userExists.Result) + if (!userExists) throw new ArgumentException("User doesn't exist!"); - if (!friendExists.Result) + if (!friendExists) throw new ArgumentException("Friend doesn't exist!"); if (!await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) throw new ArgumentException("This ain't your friend, amigo."); - return await this.RemoveFriend(userId, friendId); - } - - public async Task RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel) - { - bool userExists = await this._userRepository.DoesUserExistAsync(userId); - bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); - - if (!userExists) - throw new ArgumentException("User does not exist!"); - - if (!languageExists) - throw new ArgumentException("Language does not exist!"); - User user = await this._userRepository.GetByIdAsync(userId); - Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id); - - if (!this._userRepository.DoesUserHaveThisLanguage(user, language)) - throw new ArgumentException("User does not have this language!"); - - return await this._userRepository.RemoveLanguageFromUserAsync(user, language); - } - - public async Task RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel) - { - bool userExists = await this._userRepository.DoesUserExistAsync(userId); - bool technologyExists = await this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id); - - if (!userExists) - throw new ArgumentException("User does not exist!"); - - if (!technologyExists) - throw new ArgumentException("Language does not exist!"); - - User user = await this._userRepository.GetByIdAsync(userId); - Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id); - - if (!this._userRepository.DoesUserHaveThisTechnology(user, technology)) - throw new ArgumentException("User does not have this technology!"); + User homie = await this._userRepository.GetByIdAsync(friendId); - return await this._userRepository.RemoveTechnologyFromUserAsync(user, technology); + return await this._userRepository.RemoveFriendAsync(user, homie); } #endregion diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index c503a47..784e535 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -35,19 +35,19 @@ namespace DevHive.Web.Controllers } [HttpGet] - public async Task GetById(Guid languageId) + public async Task GetById(Guid id) { - LanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(languageId); + LanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); LanguageWebModel languageWebModel = this._languageMapper.Map(languageServiceModel); return new OkObjectResult(languageWebModel); } [HttpPut] - public async Task Update(Guid languageId, [FromBody] UpdateLanguageWebModel updateModel) + public async Task Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) { UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map(updateModel); - updatelanguageServiceModel.Id = languageId; + updatelanguageServiceModel.Id = id; bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); @@ -58,9 +58,9 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task Delete(Guid languageId) + public async Task Delete(Guid id) { - bool result = await this._languageService.DeleteLanguage(languageId); + bool result = await this._languageService.DeleteLanguage(id); if (!result) return new BadRequestObjectResult("Could not delete Language"); diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 7f2f400..104b96e 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -35,19 +35,19 @@ namespace DevHive.Web.Controllers } [HttpGet] - public async Task GetById(Guid technologyId) + public async Task GetById(Guid id) { - CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(technologyId); + CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); CreateTechnologyWebModel createTechnologyWebModel = this._technologyMapper.Map(createTechnologyServiceModel); return new OkObjectResult(createTechnologyWebModel); } [HttpPut] - public async Task Update(Guid technologyId, [FromBody] UpdateTechnologyWebModel updateModel) + public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) { UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map(updateModel); - updateTechnologyServiceModel.Id = technologyId; + updateTechnologyServiceModel.Id = id; bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); @@ -58,9 +58,9 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task Delete(Guid technologyId) + public async Task Delete(Guid id) { - bool result = await this._technologyService.DeleteTechnology(technologyId); + bool result = await this._technologyService.DeleteTechnology(id); if (!result) return new BadRequestObjectResult("Could not delete Technology"); -- cgit v1.2.3 From 4836e2f4cc3e1eb53f415d26771c76c84d29c280 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 13:13:03 +0200 Subject: Configured launch.json to be workspace wide; Fixed GetFriend to GetUser --- src/DevHive.Services/Interfaces/IUserService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 2 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- src/DevHive.Web/Controllers/UserController.cs | 6 +-- src/DevHive.Web/Properties/launchSettings.json | 59 ++++++++++++------------- src/DevHive.Web/appsettings.json | 30 ++++++------- src/DevHive.code-workspace | 20 +++++---- 7 files changed, 61 insertions(+), 62 deletions(-) (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 19bb939..ef22000 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -2,8 +2,6 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Identity; using DevHive.Services.Models.Identity.User; -using DevHive.Services.Models.Language; -using DevHive.Services.Models.Technology; namespace DevHive.Services.Interfaces { @@ -14,7 +12,7 @@ namespace DevHive.Services.Interfaces Task AddFriend(Guid userId, Guid friendId); - Task GetFriend(string username); + Task GetUserByUsername(string username); Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index d4c6f81..bf18007 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -118,7 +118,7 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); } - public async Task GetFriend(string username) + public async Task GetUserByUsername(string username) { User friend = await this._userRepository.GetByUsernameAsync(username); diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 5b3dca5..f173ea4 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -8,7 +8,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index a306007..dc27cbf 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -72,11 +72,11 @@ namespace DevHive.Web.Controllers } [HttpGet] - [Route("GetFriend")] + [Route("GetUser")] [AllowAnonymous] - public async Task GetAFriend(string username) + public async Task GetUser(string username) { - UserServiceModel friendServiceModel = await this._userService.GetFriend(username); + UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); UserWebModel friend = this._userMapper.Map(friendServiceModel); return new OkObjectResult(friend); diff --git a/src/DevHive.Web/Properties/launchSettings.json b/src/DevHive.Web/Properties/launchSettings.json index 44d86fc..5deaadb 100644 --- a/src/DevHive.Web/Properties/launchSettings.json +++ b/src/DevHive.Web/Properties/launchSettings.json @@ -1,31 +1,28 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:1955", - "sslPort": 44326 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "DevHive.Web": { - "commandName": "Project", - "dotnetRunMessages": "true", - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "http://localhost:5000", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:1955", + "sslPort": 44326 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/DevHive.Web/appsettings.json b/src/DevHive.Web/appsettings.json index a460532..83932a7 100644 --- a/src/DevHive.Web/appsettings.json +++ b/src/DevHive.Web/appsettings.json @@ -1,15 +1,15 @@ -{ - "AppSettings": { - "Secret": "gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm" - }, - "ConnectionStrings": { - "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" - }, - "Logging" : { - "LogLevel" : { - "Default" : "Information", - "Microsoft" : "Warning", - "Microsoft.Hosting.Lifetime" : "Information" - } - } -} +{ + "AppSettings": { + "Secret": "gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm" + }, + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace index 28b1e3c..10c4276 100644 --- a/src/DevHive.code-workspace +++ b/src/DevHive.code-workspace @@ -46,7 +46,7 @@ "launch": { "configurations": [ { - "name": ".NET Core Launch (web)", + "name": "Launch API", "type": "coreclr", "request": "launch", "preLaunchTask": "workspace-build", @@ -54,16 +54,20 @@ "args": [], "cwd": "${workspaceFolder:DevHive.Web}", "stopAtEntry": false, - "serverReadyAction": { - "action": "openExternally", - "pattern": "\\bNow listening on:\\s+(https?://\\S+)" - }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "launchBrowser": { - "enabled": false - } + }, + { + "name": "Launch Data Tests", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "workspace-build", + "program": "${workspaceFolder:DevHive.Tests}/DevHive.Data.Tests/bin/Debug/net5.0/DevHive.Data.Tests.dll", + "args": [], + "cwd": "${workspaceFolder:DevHive.Tests}/DevHive.Data.Tests", + "console": "internalConsole", + "stopAtEntry": false }, ], "compounds": [] -- cgit v1.2.3 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(-) (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') 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 From 009e01dc3dc2f78db6a660c65bf0d20bae702348 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 09:23:06 +0200 Subject: Tryed implementing the http patch --- .../Models/Misc/PasswordModifications.cs | 13 ++++ src/DevHive.Common/Models/Misc/Patch.cs | 9 +++ src/DevHive.Data/Repositories/UserRepository.cs | 7 -- .../Configurations/Mapping/RoleMapings.cs | 3 + .../Mapping/UserCollectionMappings.cs | 17 ++--- .../Configurations/Mapping/UserMappings.cs | 6 +- src/DevHive.Services/Interfaces/IUserService.cs | 6 +- .../Models/Identity/User/BaseUserServiceModel.cs | 1 + .../Technology/ReadTechnologyServiceModel.cs | 7 ++ src/DevHive.Services/Services/UserService.cs | 78 ++++++++++++---------- .../Configurations/Mapping/TechnologyMappings.cs | 2 + src/DevHive.Web/Controllers/UserController.cs | 19 ++---- .../Models/Technology/ReadTechnologyWebModel.cs | 14 ++++ 13 files changed, 113 insertions(+), 69 deletions(-) create mode 100644 src/DevHive.Common/Models/Misc/PasswordModifications.cs create mode 100644 src/DevHive.Common/Models/Misc/Patch.cs create mode 100644 src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs create mode 100644 src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Common/Models/Misc/PasswordModifications.cs b/src/DevHive.Common/Models/Misc/PasswordModifications.cs new file mode 100644 index 0000000..f10a334 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/PasswordModifications.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; +using System.Text; + +namespace DevHive.Common.Models.Misc +{ + public static class PasswordModifications + { + public static string GeneratePasswordHash(string password) + { + return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + } + } +} diff --git a/src/DevHive.Common/Models/Misc/Patch.cs b/src/DevHive.Common/Models/Misc/Patch.cs new file mode 100644 index 0000000..ea5a4f1 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/Patch.cs @@ -0,0 +1,9 @@ +namespace DevHive.Common.Models.Misc +{ + public class Patch + { + public string Name { get; set; } + public object Value { get; set; } + public string Action { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 492d46b..3f9af70 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -109,13 +109,6 @@ namespace DevHive.Data.Repositories public async Task EditAsync(User newEntity) { - // User user = await this.GetByIdAsync(newEntity.Id); - - // this._context - // .Entry(user) - // .CurrentValues - // .SetValues(newEntity); - this._context.Update(newEntity); return await this.SaveChangesAsync(this._context); diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 4ddd253..b5541f9 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,9 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); + + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs index ee505a2..7a773e8 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -8,14 +8,15 @@ namespace DevHive.Services.Configurations.Mapping { public UserCollectionMappings() { - CreateMap() - .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 541e16e..5d9e41c 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Identity.User; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -10,10 +11,13 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); CreateMap(); CreateMap(); + CreateMap() + .ForMember(x => x.Password, opt => opt.Ignore()); CreateMap(); } } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 121fec3..88be0c8 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -1,9 +1,9 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Common.Models.Identity; -using DevHive.Data.Models; +using DevHive.Common.Models.Misc; using DevHive.Services.Models.Identity.User; -using Microsoft.AspNetCore.JsonPatch; namespace DevHive.Services.Interfaces { @@ -18,7 +18,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, JsonPatchDocument jsonPatch); + Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs index 514f82a..7a160f8 100644 --- a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs @@ -6,5 +6,6 @@ namespace DevHive.Services.Models.Identity.User public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } + public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs new file mode 100644 index 0000000..cbfdc7d --- /dev/null +++ b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Technology +{ + public class ReadTechnologyServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 51c4432..629b489 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -7,15 +7,14 @@ using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using Microsoft.IdentityModel.Tokens; -using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; -using Microsoft.AspNetCore.JsonPatch; using System.Linq; -using Newtonsoft.Json; +using DevHive.Common.Models.Misc; +using System.Reflection; namespace DevHive.Services.Services { @@ -52,7 +51,7 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByUsernameAsync(loginModel.UserName); - if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) + if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password)) throw new ArgumentException("Incorrect password!"); return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles)); @@ -67,7 +66,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); - user.PasswordHash = GeneratePasswordHash(registerModel.Password); + user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); // Make sure the default role exists if (!await this._roleRepository.DoesNameExist(Role.DefaultRole)) @@ -135,6 +134,7 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { + //Method: ValidateUserOnUpdate if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) throw new ArgumentException("User does not exist!"); @@ -144,6 +144,7 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); + //Method: Insert collections to user HashSet languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? @@ -159,51 +160,35 @@ namespace DevHive.Services.Services user.Languages = languages; user.Technologies = technologies; - bool success = await this._userRepository.EditAsync(user); + bool successful = await this._userRepository.EditAsync(user); - if (!success) + if (!successful) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; } - public async Task PatchUser(Guid id, JsonPatchDocument jsonPatch) + public async Task PatchUser(Guid id, List patchList) { User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - object password = jsonPatch.Operations - .Where(x => x.path == "/password") - .Select(x => x.value) - .FirstOrDefault(); - - IEnumerable friends = jsonPatch.Operations - .Where(x => x.path == "/friends") - .Select(x => x.value); - - if(password != null) - { - string passwordHash = this.GeneratePasswordHash(password.ToString()); - user.PasswordHash = passwordHash; - } + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); - if (friends != null) + foreach (Patch patch in patchList) { - foreach (object friendObj in friends) + bool successful = patch.Action switch { - FriendServiceModel friendServiceModel = - JsonConvert.DeserializeObject(friendObj.ToString()); - - User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName) - ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!"); - - user.Friends.Add(amigo); - } + "replace" => ReplacePatch(updateUserServiceModel, patch), + "add" => AddPatch(updateUserServiceModel, patch), + "remove" => RemovePatch(updateUserServiceModel, patch), + _ => throw new ArgumentException("Invalid patch operation!"), + }; + + if (!successful) + throw new ArgumentException("A problem occurred while applying patch"); } - //Remove password and friends peace from the request patch before applying the rest - // jsonPatch.ApplyTo(user); - bool success = await this._userRepository.EditAsync(user); if (success) { @@ -326,6 +311,11 @@ namespace DevHive.Services.Services } } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + + } + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -354,9 +344,23 @@ namespace DevHive.Services.Services return tokenHandler.WriteToken(token); } - private string GeneratePasswordHash(string password) + private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) + { + // Type type = typeof(UpdateUserServiceModel); + // PropertyInfo property = type.GetProperty(patch.Name); + + // property.SetValue(updateUserServiceModel, patch.Value); + throw new NotImplementedException(); + } + + private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) + { + throw new NotImplementedException(); + } + + private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) { - return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + throw new NotImplementedException(); } #endregion } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 828dac1..4ecd5f3 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -9,10 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public TechnologyMappings() { CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 471d2bb..7121ac8 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -6,14 +6,10 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Misc; -using DevHive.Web.Models.Language; -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; +using DevHive.Common.Models.Misc; +using System.Collections.Generic; namespace DevHive.Web.Controllers { @@ -87,15 +83,12 @@ namespace DevHive.Web.Controllers #region Update [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization) + public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - // if (!ModelState.IsValid) - // return BadRequest("Not a valid model!"); - - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateModel); + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateUserWebModel); updateUserServiceModel.Id = id; UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); @@ -105,12 +98,12 @@ namespace DevHive.Web.Controllers } [HttpPatch] - public async Task Patch(Guid id, [FromBody] JsonPatchDocument jsonPatch, [FromHeader] string authorization) + public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - UserServiceModel userServiceModel = await this._userService.PatchUser(id, jsonPatch); + UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); if (userServiceModel == null) return new BadRequestObjectResult("Wrong patch properties"); diff --git a/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs new file mode 100644 index 0000000..edaaaef --- /dev/null +++ b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Technology +{ + public class ReadTechnologyWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index b5541f9..d6c8511 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -12,7 +12,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 079ec3e..0103ccf 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs deleted file mode 100644 index 7a773e8..0000000 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.User; - -namespace DevHive.Services.Configurations.Mapping -{ - public class UserCollectionMappings : Profile - { - public UserCollectionMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 88be0c8..923e9bb 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,7 +18,6 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs new file mode 100644 index 0000000..53bea9e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Services.Models.Identity.Role +{ + public class CreateRoleServiceModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs index 63d57f7..a784f5c 100644 --- a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.User { public class FriendServiceModel { + public Guid Id { get; set; } public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs new file mode 100644 index 0000000..83fcc34 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateFriendServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class UpdateUserCollectionServiceModel - { - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 835bf54..9277e3e 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { @@ -9,13 +12,13 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index dca00fa..a57fd23 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,8 +14,9 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -135,69 +136,57 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - //Method: ValidateUserOnUpdate - if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) - throw new ArgumentException("User does not exist!"); - - if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) - throw new ArgumentException("Username already exists!"); + await this.ValidateUserOnUpdate(updateUserServiceModel); await this.ValidateUserCollections(updateUserServiceModel); - //Method: Insert collections to user - HashSet languages = new(); - foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? - throw new ArgumentException("Invalid language name!")); + //Preserve roles + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); - HashSet technologies = new(); - foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? - throw new ArgumentException("Invalid technology name!")); + UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - User user = this._userMapper.Map(updateUserServiceModel); + updateUserServiceModel.Roles.Add(updateRoleServiceModel); + } - user.Languages = languages; - user.Technologies = technologies; + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); - bool successful = await this._userRepository.EditAsync(user); + UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List patchList) - { - User user = await this._userRepository.GetByIdAsync(id) ?? - throw new ArgumentException("User does not exist!"); + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - foreach (Patch patch in patchList) - { - bool successful = patch.Action switch - { - "replace" => ReplacePatch(updateUserServiceModel, patch), - "add" => AddPatch(updateUserServiceModel, patch), - "remove" => RemovePatch(updateUserServiceModel, patch), - _ => throw new ArgumentException("Invalid patch operation!"), - }; - - if (!successful) - throw new ArgumentException("A problem occurred while applying patch"); + updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); } - bool success = await this._userRepository.EditAsync(user); - if (success) - { - user = await this._userRepository.GetByIdAsync(id); - return this._userMapper.Map(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(user); } #endregion @@ -282,10 +271,20 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) + throw new ArgumentException("User does not exist!"); + + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) + throw new ArgumentException("Username already exists!"); + } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + foreach (var friend in updateUserServiceModel.Friends) { User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); @@ -294,29 +293,24 @@ namespace DevHive.Services.Services } // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + foreach (var language in updateUserServiceModel.Languages) { Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - if (default(Language) == returnedLanguage) + if (returnedLanguage == null) throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + foreach (var technology in updateUserServiceModel.Technologies) { Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - if (default(Technology) == returnedTechnology) + if (returnedTechnology == null) throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } - private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) - { - throw new NotImplementedException(); - } - private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -344,25 +338,6 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - // Type type = typeof(UpdateUserServiceModel); - // PropertyInfo property = type.GetProperty(patch.Name); - - // property.SetValue(updateUserServiceModel, patch.Value); - throw new NotImplementedException(); - } - - private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } - - private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From f8f3727319a03eb9dd9a2ed8546810beb732cdab Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:21:46 +0200 Subject: Cleaning of UserRepo&UserService of unused methods --- .../Interfaces/Repositories/IUserRepository.cs | 13 +--- src/DevHive.Data/Repositories/UserRepository.cs | 74 ---------------------- src/DevHive.Services/Interfaces/IUserService.cs | 5 -- src/DevHive.Services/Services/UserService.cs | 51 --------------- 4 files changed, 2 insertions(+), 141 deletions(-) (limited to 'src/DevHive.Services/Interfaces/IUserService.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index 456eb94..c29669d 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -8,10 +8,7 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IUserRepository : IRepository { - Task AddFriendToUserAsync(User user, User friend); - Task AddLanguageToUserAsync(User user, Language language); - Task AddTechnologyToUserAsync(User user, Technology technology); - + //Read Task GetByUsernameAsync(string username); Language GetUserLanguage(User user, Language language); HashSet GetUserLanguages(User user); @@ -19,13 +16,7 @@ namespace DevHive.Data.Interfaces.Repositories Technology GetUserTechnology(User user, Technology technology); IEnumerable QueryAll(); - Task EditUserLanguageAsync(User user, Language oldLang, Language newLang); - Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech); - - Task RemoveFriendAsync(User user, User friend); - Task RemoveLanguageFromUserAsync(User user, Language language); - Task RemoveTechnologyFromUserAsync(User user, Technology technology); - + //Validations Task DoesEmailExistAsync(string email); Task DoesUserExistAsync(Guid id); Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index c769f7e..f0c28f1 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -2,11 +2,9 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -28,32 +26,6 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(this._context); } - - public async Task AddFriendToUserAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Add(friend); - - return await this.SaveChangesAsync(this._context); - } - - public async Task AddLanguageToUserAsync(User user, Language language) - { - this._context.Update(user); - - user.Languages.Add(language); - - return await this.SaveChangesAsync(this._context); - } - - public async Task AddTechnologyToUserAsync(User user, Technology technology) - { - this._context.Update(user); - - user.Technologies.Add(technology); - - return await this.SaveChangesAsync(this._context); - } #endregion #region Read @@ -120,26 +92,6 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(this._context); } - - public async Task EditUserLanguageAsync(User user, Language oldLang, Language newLang) - { - this._context.Update(user); - - user.Languages.Remove(oldLang); - user.Languages.Add(newLang); - - return await this.SaveChangesAsync(this._context); - } - - public async Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech) - { - this._context.Update(user); - - user.Technologies.Remove(oldTech); - user.Technologies.Add(newTech); - - return await this.SaveChangesAsync(this._context); - } #endregion #region Delete @@ -151,32 +103,6 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(this._context); } - - public async Task RemoveFriendAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Remove(friend); - - return await this.SaveChangesAsync(this._context); - } - - public async Task RemoveLanguageFromUserAsync(User user, Language language) - { - this._context.Update(user); - - user.Languages.Remove(language); - - return await this.SaveChangesAsync(this._context); - } - - public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) - { - this._context.Update(user); - - user.Technologies.Remove(technology); - - return await this.SaveChangesAsync(this._context); - } #endregion #region Validations diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 923e9bb..51e3cf9 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Misc; using DevHive.Services.Models.Identity.User; namespace DevHive.Services.Interfaces @@ -12,15 +10,12 @@ namespace DevHive.Services.Interfaces Task LoginUser(LoginServiceModel loginModel); Task RegisterUser(RegisterServiceModel registerModel); - Task AddFriend(Guid userId, Guid friendId); - Task GetUserByUsername(string username); Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); Task DeleteUser(Guid id); - Task RemoveFriend(Guid userId, Guid friendId); Task ValidJWT(Guid id, string rawTokenData); } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index a57fd23..217154e 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -84,35 +84,7 @@ namespace DevHive.Services.Services } #endregion - #region Create - - public async Task AddFriend(Guid userId, Guid friendId) - { - Task userExists = this._userRepository.DoesUserExistAsync(userId); - Task friendExists = this._userRepository.DoesUserExistAsync(friendId); - - await Task.WhenAll(userExists, friendExists); - - if (!userExists.Result) - throw new ArgumentException("User doesn't exist!"); - - if (!friendExists.Result) - throw new ArgumentException("Friend doesn't exist!"); - - if (await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) - throw new ArgumentException("Friend already exists in your friends list."); - - User user = await this._userRepository.GetByIdAsync(userId); - User friend = await this._userRepository.GetByIdAsync(friendId); - - return user != null && friend != null ? - await this._userRepository.AddFriendToUserAsync(user, friend) : - throw new ArgumentException("Invalid user!"); - } - #endregion - #region Read - public async Task GetUserById(Guid id) { User user = await this._userRepository.GetByIdAsync(id) @@ -133,7 +105,6 @@ namespace DevHive.Services.Services #endregion #region Update - public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { await this.ValidateUserOnUpdate(updateUserServiceModel); @@ -191,7 +162,6 @@ namespace DevHive.Services.Services #endregion #region Delete - public async Task DeleteUser(Guid id) { if (!await this._userRepository.DoesUserExistAsync(id)) @@ -203,30 +173,9 @@ namespace DevHive.Services.Services if (!result) throw new InvalidOperationException("Unable to delete user!"); } - - public async Task RemoveFriend(Guid userId, Guid friendId) - { - bool userExists = await this._userRepository.DoesUserExistAsync(userId); - bool friendExists = await this._userRepository.DoesUserExistAsync(friendId); - - if (!userExists) - throw new ArgumentException("User doesn't exist!"); - - if (!friendExists) - throw new ArgumentException("Friend doesn't exist!"); - - if (!await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) - throw new ArgumentException("This ain't your friend, amigo."); - - User user = await this._userRepository.GetByIdAsync(userId); - User homie = await this._userRepository.GetByIdAsync(friendId); - - return await this._userRepository.RemoveFriendAsync(user, homie); - } #endregion #region Validations - public async Task ValidJWT(Guid id, string rawTokenData) { // There is authorization name in the beginning, i.e. "Bearer eyJh..." -- cgit v1.2.3