aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-20 18:07:58 +0200
committertranstrike <transtrike@gmail.com>2021-01-20 18:07:58 +0200
commit8179af787a7bf375753a178b89111a91d84a8bb8 (patch)
treeeb2dbeb52e0875ddc06d5e6ca1bd97ef8d941d4e /src/DevHive.Services/Services
parentaa4f7bdd9a2df09fc47e82c2b85fb7647203ba8d (diff)
downloadDevHive-8179af787a7bf375753a178b89111a91d84a8bb8.tar
DevHive-8179af787a7bf375753a178b89111a91d84a8bb8.tar.gz
DevHive-8179af787a7bf375753a178b89111a91d84a8bb8.zip
Changed List to HashSet for "no duplicates" scenario
Diffstat (limited to 'src/DevHive.Services/Services')
-rw-r--r--src/DevHive.Services/Services/PostService.cs5
-rw-r--r--src/DevHive.Services/Services/UserService.cs12
2 files changed, 9 insertions, 8 deletions
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}"),
};