diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-20 18:07:58 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-20 18:07:58 +0200 |
| commit | 8179af787a7bf375753a178b89111a91d84a8bb8 (patch) | |
| tree | eb2dbeb52e0875ddc06d5e6ca1bd97ef8d941d4e /src/DevHive.Services | |
| parent | aa4f7bdd9a2df09fc47e82c2b85fb7647203ba8d (diff) | |
| download | DevHive-8179af787a7bf375753a178b89111a91d84a8bb8.tar DevHive-8179af787a7bf375753a178b89111a91d84a8bb8.tar.gz DevHive-8179af787a7bf375753a178b89111a91d84a8bb8.zip | |
Changed List to HashSet for "no duplicates" scenario
Diffstat (limited to 'src/DevHive.Services')
5 files changed, 19 insertions, 18 deletions
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<LanguageServiceModel> Languages { get; set; } - public IList<TechnologyServiceModel> Technologies { get; set; } + public HashSet<LanguageServiceModel> Languages { get; set; } + public HashSet<TechnologyServiceModel> 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<UpdateUserCollectionServiceModel> Roles { get; set; } = new List<UpdateUserCollectionServiceModel>(); + public HashSet<UpdateUserCollectionServiceModel> Roles { get; set; } = new HashSet<UpdateUserCollectionServiceModel>(); - public IList<UpdateUserCollectionServiceModel> Friends { get; set; } = new List<UpdateUserCollectionServiceModel>(); + public HashSet<UpdateUserCollectionServiceModel> Friends { get; set; } = new HashSet<UpdateUserCollectionServiceModel>(); - public IList<UpdateUserCollectionServiceModel> Languages { get; set; } = new List<UpdateUserCollectionServiceModel>(); + public HashSet<UpdateUserCollectionServiceModel> Languages { get; set; } = new HashSet<UpdateUserCollectionServiceModel>(); - public IList<UpdateUserCollectionServiceModel> Technologies { get; set; } = new List<UpdateUserCollectionServiceModel>(); + public HashSet<UpdateUserCollectionServiceModel> Technologies { get; set; } = new HashSet<UpdateUserCollectionServiceModel>(); } } 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<RoleServiceModel> Roles { get; set; } = new List<RoleServiceModel>(); + public HashSet<RoleServiceModel> Roles { get; set; } = new HashSet<RoleServiceModel>(); - public IList<FriendServiceModel> Friends { get; set; } = new List<FriendServiceModel>(); + public HashSet<FriendServiceModel> Friends { get; set; } = new HashSet<FriendServiceModel>(); - public IList<LanguageServiceModel> Languages { get; set; } = new List<LanguageServiceModel>(); + public HashSet<LanguageServiceModel> Languages { get; set; } = new HashSet<LanguageServiceModel>(); - public IList<TechnologyServiceModel> Technologies { get; set; } = new List<TechnologyServiceModel>(); + public HashSet<TechnologyServiceModel> Technologies { get; set; } = new HashSet<TechnologyServiceModel>(); } } 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<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + //HashSet<string> 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<Role>() { defaultRole }; + user.Roles = new HashSet<Role>() { defaultRole }; await this._userRepository.AddAsync(user); @@ -144,12 +144,12 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - List<Language> languages = new(); + HashSet<Language> languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? throw new ArgumentException("Invalid language name!")); - List<Technology> technologies = new(); + HashSet<Technology> 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<string> 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<Role> roles) + private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List<Claim> claims = new() + HashSet<Claim> claims = new() { new Claim("ID", $"{userId}"), }; |
