From 8f6a50566a069c782482a167f601e6eca9588e73 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 15:30:09 +0200 Subject: User update now updates languages, roles and friends --- .../Models/Identity/User/UpdateUserServiceModel.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs') diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 96d1ff0..5b5a178 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,9 +1,19 @@ using System; +using System.Collections.Generic; namespace DevHive.Services.Models.Identity.User { - public class UpdateUserServiceModel : UserServiceModel + public class UpdateUserServiceModel : BaseUserServiceModel { public Guid Id { get; set; } + + public IList Roles { get; set; } = new List(); + + public IList Friends { get; set; } = new List(); + + public IList Languages { get; set; } = new List(); + + public IList Technologies { get; set; } = new List(); + } } -- cgit v1.2.3 From 8179af787a7bf375753a178b89111a91d84a8bb8 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 20 Jan 2021 18:07:58 +0200 Subject: Changed List to HashSet for "no duplicates" scenario --- src/DevHive.Data/Interfaces/Models/ILanguage.cs | 2 +- src/DevHive.Data/Interfaces/Models/IRole.cs | 2 +- src/DevHive.Data/Interfaces/Models/ITechnology.cs | 2 +- src/DevHive.Data/Interfaces/Models/IUser.cs | 8 ++++---- .../Interfaces/Repositories/IUserRepository.cs | 4 ++-- src/DevHive.Data/Models/Language.cs | 2 +- src/DevHive.Data/Models/Role.cs | 2 +- src/DevHive.Data/Models/Technology.cs | 2 +- src/DevHive.Data/Models/User.cs | 8 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 4 ++-- .../Models/Identity/User/RegisterServiceModel.cs | 4 ++-- .../Models/Identity/User/UpdateUserServiceModel.cs | 8 ++++---- .../Models/Identity/User/UserServiceModel.cs | 8 ++++---- src/DevHive.Services/Services/PostService.cs | 5 +++-- src/DevHive.Services/Services/UserService.cs | 12 ++++++------ .../DevHive.Data.Tests/UserRepositoryTests.cs | 18 +++++++++--------- .../Models/Identity/User/UpdateUserWebModel.cs | 6 +++--- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 8 ++++---- 18 files changed, 53 insertions(+), 52 deletions(-) (limited to 'src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs') diff --git a/src/DevHive.Data/Interfaces/Models/ILanguage.cs b/src/DevHive.Data/Interfaces/Models/ILanguage.cs index 9549777..b77d5ae 100644 --- a/src/DevHive.Data/Interfaces/Models/ILanguage.cs +++ b/src/DevHive.Data/Interfaces/Models/ILanguage.cs @@ -6,7 +6,7 @@ namespace DevHive.Data.Interfaces.Models public interface ILanguage : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IRole.cs b/src/DevHive.Data/Interfaces/Models/IRole.cs index 0623f07..c8b7068 100644 --- a/src/DevHive.Data/Interfaces/Models/IRole.cs +++ b/src/DevHive.Data/Interfaces/Models/IRole.cs @@ -5,6 +5,6 @@ namespace DevHive.Data.Interfaces.Models { public interface IRole { - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/ITechnology.cs b/src/DevHive.Data/Interfaces/Models/ITechnology.cs index 159a21a..153f75f 100644 --- a/src/DevHive.Data/Interfaces/Models/ITechnology.cs +++ b/src/DevHive.Data/Interfaces/Models/ITechnology.cs @@ -6,6 +6,6 @@ namespace DevHive.Data.Interfaces.Models public interface ITechnology : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index ef8c927..08ce385 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,9 +8,9 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Languages { get; set; } - IList Technologies { get; set; } - IList Roles { get; set; } - IList Friends { get; set; } + HashSet Languages { get; set; } + HashSet Technologies { get; set; } + HashSet Roles { get; set; } + HashSet Friends { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index eca6adb..456eb94 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -14,8 +14,8 @@ namespace DevHive.Data.Interfaces.Repositories Task GetByUsernameAsync(string username); Language GetUserLanguage(User user, Language language); - IList GetUserLanguages(User user); - IList GetUserTechnologies(User user); + HashSet GetUserLanguages(User user); + HashSet GetUserTechnologies(User user); Technology GetUserTechnology(User user, Technology technology); IEnumerable QueryAll(); diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index 0db8cb6..f2b2786 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e63f007..e0855aa 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -12,6 +12,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 9096cbe..a0728d5 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index cf779f5..2ac7adf 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -19,15 +19,15 @@ namespace DevHive.Data.Models /// Languages that the user uses or is familiar with /// // [Unique] - public IList Languages { get; set; } + public HashSet Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 2ca8099..492d46b 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -82,7 +82,7 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.UserName == username); } - public IList GetUserLanguages(User user) + public HashSet GetUserLanguages(User user) { return user.Languages; } @@ -93,7 +93,7 @@ namespace DevHive.Data.Repositories .FirstOrDefault(x => x.Id == language.Id); } - public IList GetUserTechnologies(User user) + public HashSet GetUserTechnologies(User user) { return user.Technologies; } diff --git a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 74f66b4..3171ea6 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -6,8 +6,8 @@ namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel { - public IList Languages { get; set; } - public IList Technologies { get; set; } + public HashSet Languages { get; set; } + public HashSet Technologies { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 5b5a178..87af43a 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -7,13 +7,13 @@ namespace DevHive.Services.Models.Identity.User { public Guid Id { get; set; } - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 913b5c0..5fcd494 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index f2f60d1..9503b8a 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -9,6 +9,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; +using System.Linq; namespace DevHive.Services.Services { @@ -131,8 +132,8 @@ namespace DevHive.Services.Services { var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; - //List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByUsernameAsync(jwtUserName) ?? throw new ArgumentException("User does not exist!"); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index ee4b24d..51c4432 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -75,7 +75,7 @@ namespace DevHive.Services.Services // Set the default role to the user Role defaultRole = await this._roleRepository.GetByNameAsync(Role.DefaultRole); - user.Roles = new List() { defaultRole }; + user.Roles = new HashSet() { defaultRole }; await this._userRepository.AddAsync(user); @@ -144,12 +144,12 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - List languages = new(); + HashSet languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? throw new ArgumentException("Invalid language name!")); - List technologies = new(); + HashSet technologies = new(); foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? throw new ArgumentException("Invalid technology name!")); @@ -257,7 +257,7 @@ namespace DevHive.Services.Services // There is authorization name in the beginning, i.e. "Bearer eyJh..." var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims)[0]); + Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims).First()); List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByIdAsync(jwtUserID) @@ -326,11 +326,11 @@ namespace DevHive.Services.Services } } - private string WriteJWTSecurityToken(Guid userId, IList roles) + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List claims = new() + HashSet claims = new() { new Claim("ID", $"{userId}"), }; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index b0a5b93..be116b0 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -165,10 +165,10 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Languages; + HashSet dummyUserLanguages = dummyUser.Languages; //Act - IList languages = this._userRepository.GetUserLanguages(dummyUser); + HashSet languages = this._userRepository.GetUserLanguages(dummyUser); //Assert Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -185,7 +185,7 @@ namespace DevHive.Data.Tests // Language dummyLang = await this._languageRepository.GetByNameAsync("csharp"); // //Act - // IList languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); + // HashSet languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); // //Assert // Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -195,7 +195,7 @@ namespace DevHive.Data.Tests #region HelperMethods private User CreateDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -204,7 +204,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -213,7 +213,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { @@ -237,7 +237,7 @@ namespace DevHive.Data.Tests private User CreateAnotherDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -246,7 +246,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -255,7 +255,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 724930c..3c38ab6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -16,14 +16,14 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public IList Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] - public IList Languages { get; set; } + public HashSet Languages { get; set; } [NotNull] [Required] - public IList Technologies { get; set; } + public HashSet Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 1d2d17b..5b80ba3 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -11,18 +11,18 @@ namespace DevHive.Web.Models.Identity.User { [NotNull] [Required] - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); [NotNull] [Required] - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); [NotNull] [Required] - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } -- cgit v1.2.3 From 0d4ec399e6c9246dd0d3050dda2c81043cf7f51d Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 09:29:50 +0200 Subject: Fixed Password property positioning --- src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs | 1 - src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs | 4 +++- src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs | 2 ++ src/DevHive.Services/Services/UserService.cs | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs') diff --git a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs index 7a160f8..514f82a 100644 --- a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs @@ -6,6 +6,5 @@ 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/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 3171ea6..adc4119 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -6,8 +6,10 @@ namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel { + public string Password { get; set; } + public HashSet Languages { get; set; } + public HashSet Technologies { get; set; } - public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 87af43a..835bf54 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -7,6 +7,8 @@ namespace DevHive.Services.Models.Identity.User { public Guid Id { get; set; } + public string Password { get; set; } + public HashSet Roles { get; set; } = new HashSet(); public HashSet Friends { get; set; } = new HashSet(); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 629b489..dca00fa 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,6 +15,7 @@ using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; using System.Reflection; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace DevHive.Services.Services { @@ -313,7 +314,7 @@ namespace DevHive.Services.Services private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) { - + throw new NotImplementedException(); } private string WriteJWTSecurityToken(Guid userId, HashSet roles) -- 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/Models/Identity/User/UpdateUserServiceModel.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