diff options
| author | transtrike <transtrike@gmail.com> | 2021-02-15 19:06:14 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-02-15 19:06:14 +0200 |
| commit | 4c03712af14c37718b7be5b23fcadeb86f2a2191 (patch) | |
| tree | 0c6b5a592244e5b2574afa9e4919d4a3111eea34 /src/Services | |
| parent | 09fc4603e82f69c926f9457085aa3fa48bb3939c (diff) | |
| download | DevHive-4c03712af14c37718b7be5b23fcadeb86f2a2191.tar DevHive-4c03712af14c37718b7be5b23fcadeb86f2a2191.tar.gz DevHive-4c03712af14c37718b7be5b23fcadeb86f2a2191.zip | |
Code Analyzer added to all csproj; Removed unnessessary code; Fixed formatting
Diffstat (limited to 'src/Services')
14 files changed, 46 insertions, 55 deletions
diff --git a/src/Services/DevHive.Services.Models/DevHive.Services.Models.csproj b/src/Services/DevHive.Services.Models/DevHive.Services.Models.csproj index 3f0eadf..914efe0 100644 --- a/src/Services/DevHive.Services.Models/DevHive.Services.Models.csproj +++ b/src/Services/DevHive.Services.Models/DevHive.Services.Models.csproj @@ -2,12 +2,11 @@ <PropertyGroup> <TargetFramework>net5.0</TargetFramework> </PropertyGroup> - <ItemGroup> - <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" /> + <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2"/> + <PackageReference Include="SonarAnalyzer.CSharp" Version="8.18.0.27296"/> </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\..\Common\DevHive.Common.Models\DevHive.Common.csproj" /> + <ProjectReference Include="..\..\Common\DevHive.Common.Models\DevHive.Common.csproj"/> </ItemGroup> -</Project> +</Project>
\ No newline at end of file diff --git a/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj b/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj index eb33d07..bdfb2bb 100644 --- a/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj +++ b/src/Services/DevHive.Services.Tests/DevHive.Services.Tests.csproj @@ -4,14 +4,15 @@ <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> - <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.3" /> - <PackageReference Include="Moq" Version="4.16.0" /> - <PackageReference Include="NUnit" Version="3.13.1" /> - <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" /> - <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.3"/> + <PackageReference Include="Moq" Version="4.16.0"/> + <PackageReference Include="NUnit" Version="3.13.1"/> + <PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3"/> + <PackageReference Include="SonarAnalyzer.CSharp" Version="8.18.0.27296"/> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\DevHive.Services\DevHive.Services.csproj" /> + <ProjectReference Include="..\DevHive.Services\DevHive.Services.csproj"/> </ItemGroup> <PropertyGroup> <EnableNETAnalyzers>true</EnableNETAnalyzers> diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs index 8fddce7..ce997c1 100644 --- a/src/Services/DevHive.Services.Tests/UserService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs @@ -29,7 +29,7 @@ namespace DevHive.Services.Tests private Mock<ILanguageRepository> LanguageRepositoryMock { get; set; } private Mock<ITechnologyRepository> TechnologyRepositoryMock { get; set; } private Mock<IMapper> MapperMock { get; set; } - private JWTOptions JWTOptions { get; set; } + private JwtOptions JwtOptions { get; set; } private UserService UserService { get; set; } #region SetUps @@ -41,10 +41,10 @@ namespace DevHive.Services.Tests this.CloudServiceMock = new Mock<ICloudService>(); this.LanguageRepositoryMock = new Mock<ILanguageRepository>(); this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>(); - this.JWTOptions = new JWTOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); + this.JwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); this.MapperMock = new Mock<IMapper>(); // TODO: give actual UserManager and RoleManager to UserService - this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, null, null, this.MapperMock.Object, this.JWTOptions, this.CloudServiceMock.Object); + this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, null, null, this.MapperMock.Object, this.JwtOptions, this.CloudServiceMock.Object); } #endregion @@ -364,7 +364,7 @@ namespace DevHive.Services.Tests #region HelperMethods private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles) { - byte[] signingKey = Encoding.ASCII.GetBytes(this.JWTOptions.Secret); + byte[] signingKey = Encoding.ASCII.GetBytes(this.JwtOptions.Secret); HashSet<Claim> claims = new() { new Claim("ID", $"{userId}"), diff --git a/src/Services/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/PostMappings.cs index 9362f90..1d7d88b 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -12,7 +12,6 @@ namespace DevHive.Services.Configurations.Mapping public PostMappings() { CreateMap<CreatePostServiceModel, Post>(); - // .ForMember(dest => dest.Files, src => src.Ignore()); CreateMap<UpdatePostServiceModel, Post>() .ForMember(dest => dest.Id, src => src.MapFrom(p => p.PostId)) // .ForMember(dest => dest.Files, src => src.Ignore()) diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 1dbb7b4..fefa6d8 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -8,8 +8,6 @@ namespace DevHive.Services.Configurations.Mapping { public RatingMappings() { - // CreateMap<Rating, ReadPostRatingServiceModel>() - // .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Post.Id)); } } } diff --git a/src/Services/DevHive.Services/DevHive.Services.csproj b/src/Services/DevHive.Services/DevHive.Services.csproj index b09c46d..650a304 100644 --- a/src/Services/DevHive.Services/DevHive.Services.csproj +++ b/src/Services/DevHive.Services/DevHive.Services.csproj @@ -3,22 +3,23 @@ <TargetFramework>net5.0</TargetFramework> </PropertyGroup> <ItemGroup> - <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" /> + <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0"/> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> - <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" /> - <PackageReference Include="AutoMapper" Version="10.1.1" /> - <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" /> - <PackageReference Include="CloudinaryDotNet" Version="1.14.0" /> + <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0"/> + <PackageReference Include="AutoMapper" Version="10.1.1"/> + <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1"/> + <PackageReference Include="CloudinaryDotNet" Version="1.14.0"/> + <PackageReference Include="SonarAnalyzer.CSharp" Version="8.18.0.27296"/> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\..\Data\DevHive.Data\DevHive.Data.csproj" /> - <ProjectReference Include="..\DevHive.Services.Models\DevHive.Services.Models.csproj" /> + <ProjectReference Include="..\..\Data\DevHive.Data\DevHive.Data.csproj"/> + <ProjectReference Include="..\DevHive.Services.Models\DevHive.Services.Models.csproj"/> </ItemGroup> <PropertyGroup> <EnableNETAnalyzers>true</EnableNETAnalyzers> <AnalysisLevel>latest</AnalysisLevel> </PropertyGroup> -</Project> +</Project>
\ No newline at end of file diff --git a/src/Services/DevHive.Services/Interfaces/ICloudService.cs b/src/Services/DevHive.Services/Interfaces/ICloudService.cs index 3ae7a24..040729f 100644 --- a/src/Services/DevHive.Services/Interfaces/ICloudService.cs +++ b/src/Services/DevHive.Services/Interfaces/ICloudService.cs @@ -9,8 +9,6 @@ namespace DevHive.Services.Interfaces { Task<List<string>> UploadFilesToCloud(List<IFormFile> formFiles); - // Task<List<FileContentResult>> GetFilesFromCloud(List<string> fileUrls); - Task<bool> RemoveFilesFromCloud(List<string> fileUrls); } } diff --git a/src/Services/DevHive.Services/Interfaces/IRoleService.cs b/src/Services/DevHive.Services/Interfaces/IRoleService.cs index 2c31b06..05df917 100644 --- a/src/Services/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRoleService.cs @@ -6,7 +6,7 @@ namespace DevHive.Services.Interfaces { public interface IRoleService { - Task<Guid> CreateRole(CreateRoleServiceModel roleServiceModel); + Task<Guid> CreateRole(CreateRoleServiceModel createRoleServiceModel); Task<RoleServiceModel> GetRoleById(Guid id); diff --git a/src/Services/DevHive.Services/Options/JWTOptions.cs b/src/Services/DevHive.Services/Options/JwtOptions.cs index 95458f5..d973f45 100644 --- a/src/Services/DevHive.Services/Options/JWTOptions.cs +++ b/src/Services/DevHive.Services/Options/JwtOptions.cs @@ -2,9 +2,9 @@ using Microsoft.Extensions.Options; namespace DevHive.Services.Options { - public class JWTOptions + public class JwtOptions { - public JWTOptions(string secret) + public JwtOptions(string secret) { this.Secret = secret; } diff --git a/src/Services/DevHive.Services/Services/FeedService.cs b/src/Services/DevHive.Services/Services/FeedService.cs index 5feef6e..17cc43f 100644 --- a/src/Services/DevHive.Services/Services/FeedService.cs +++ b/src/Services/DevHive.Services/Services/FeedService.cs @@ -57,7 +57,7 @@ namespace DevHive.Services.Services /// <summary> /// This method is used in the profile pages. - /// See the FeedRepository "GetUsersPosts" menthod for more information on how it works. + /// See the FeedRepository "GetUsersPosts" method for more information on how it works. /// </summary> public async Task<ReadPageServiceModel> GetUserPage(GetPageServiceModel model) { diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs index 0becd9f..a3d5117 100644 --- a/src/Services/DevHive.Services/Services/PostService.cs +++ b/src/Services/DevHive.Services/Services/PostService.cs @@ -41,7 +41,7 @@ namespace DevHive.Services.Services if (createPostServiceModel.Files.Count != 0) { List<string> fileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files); - post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls); + post.Attachments = GetPostAttachmentsFromUrls(post, fileUrls); } post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); @@ -105,8 +105,8 @@ namespace DevHive.Services.Services } List<string> fileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ?? - throw new ArgumentNullException("Unable to upload images to cloud"); - post.Attachments = this.GetPostAttachmentsFromUrls(post, fileUrls); + throw new ArgumentException("Unable to upload images to cloud!"); + post.Attachments = GetPostAttachmentsFromUrls(post, fileUrls); } post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId); @@ -202,8 +202,7 @@ namespace DevHive.Services.Services { JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); - //HashSet<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + Guid jwtUserId = Guid.Parse(GetClaimTypeValues("ID", jwt.Claims).First()); User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? throw new ArgumentException("User does not exist!"); @@ -214,7 +213,7 @@ namespace DevHive.Services.Services /// <summary> /// Returns all values from a given claim type /// </summary> - private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) + private static List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) { List<string> toReturn = new(); @@ -227,9 +226,9 @@ namespace DevHive.Services.Services #endregion #region Misc - private List<PostAttachments> GetPostAttachmentsFromUrls(Post post, List<string> fileUrls) + private static List<PostAttachments> GetPostAttachmentsFromUrls(Post post, List<string> fileUrls) { - List<PostAttachments> postAttachments = new List<PostAttachments>(); + List<PostAttachments> postAttachments = new(); foreach (string url in fileUrls) postAttachments.Add(new PostAttachments { Post = post, FileUrl = url }); return postAttachments; diff --git a/src/Services/DevHive.Services/Services/RateService.cs b/src/Services/DevHive.Services/Services/RateService.cs index d423d8c..5e924ab 100644 --- a/src/Services/DevHive.Services/Services/RateService.cs +++ b/src/Services/DevHive.Services/Services/RateService.cs @@ -28,7 +28,7 @@ namespace DevHive.Services.Services { throw new NotImplementedException(); // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) - // throw new ArgumentException("Post does not exist!"); + // throw new ArgumentException("Post does not exist!"); // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) // throw new ArgumentException("User does not exist!"); diff --git a/src/Services/DevHive.Services/Services/RoleService.cs b/src/Services/DevHive.Services/Services/RoleService.cs index 5472e44..1285421 100644 --- a/src/Services/DevHive.Services/Services/RoleService.cs +++ b/src/Services/DevHive.Services/Services/RoleService.cs @@ -5,7 +5,6 @@ using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Interfaces; using DevHive.Services.Models.Role; -using DevHive.Services.Models.Language; namespace DevHive.Services.Services { @@ -20,17 +19,17 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task<Guid> CreateRole(CreateRoleServiceModel roleServiceModel) + public async Task<Guid> CreateRole(CreateRoleServiceModel createRoleServiceModel) { - if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + if (await this._roleRepository.DoesNameExist(createRoleServiceModel.Name)) throw new ArgumentException("Role already exists!"); - Role role = this._roleMapper.Map<Role>(roleServiceModel); + Role role = this._roleMapper.Map<Role>(createRoleServiceModel); bool success = await this._roleRepository.AddAsync(role); if (success) { - Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); + Role newRole = await this._roleRepository.GetByNameAsync(createRoleServiceModel.Name); return newRole.Id; } else diff --git a/src/Services/DevHive.Services/Services/UserService.cs b/src/Services/DevHive.Services/Services/UserService.cs index 9a63853..e31eb8d 100644 --- a/src/Services/DevHive.Services/Services/UserService.cs +++ b/src/Services/DevHive.Services/Services/UserService.cs @@ -28,7 +28,7 @@ namespace DevHive.Services.Services private readonly UserManager<User> _userManager; private readonly RoleManager<Role> _roleManager; private readonly IMapper _userMapper; - private readonly JWTOptions _jwtOptions; + private readonly JwtOptions _jwtOptions; private readonly ICloudService _cloudService; public UserService(IUserRepository userRepository, @@ -38,7 +38,7 @@ namespace DevHive.Services.Services UserManager<User> userManager, RoleManager<Role> roleManager, IMapper mapper, - JWTOptions jwtOptions, + JwtOptions jwtOptions, ICloudService cloudService) { this._userRepository = userRepository; @@ -128,7 +128,7 @@ namespace DevHive.Services.Services User currentUser = await this._userRepository.GetByIdAsync(updateUserServiceModel.Id); await this.PopulateUserModel(currentUser, updateUserServiceModel); - if (updateUserServiceModel.Friends.Count() > 0) + if (updateUserServiceModel.Friends.Count > 0) await this.CreateRelationToFriends(currentUser, updateUserServiceModel.Friends.ToList()); else currentUser.Friends.Clear(); @@ -157,7 +157,7 @@ namespace DevHive.Services.Services } string fileUrl = (await this._cloudService.UploadFilesToCloud(new List<IFormFile> { updateProfilePictureServiceModel.Picture }))[0] ?? - throw new ArgumentNullException("Unable to upload profile picture to cloud"); + throw new ArgumentException("Unable to upload profile picture to cloud"); bool successful = await this._userRepository.UpdateProfilePicture(updateProfilePictureServiceModel.UserId, fileUrl); @@ -201,16 +201,13 @@ namespace DevHive.Services.Services /* Check if user is trying to do something to himself, unless he's an admin */ /* Check roles */ - if (!jwtRoleNames.Contains(Role.AdminRole)) - if (user.Id != id) - return false; + if (!jwtRoleNames.Contains(Role.AdminRole) && user.Id != id) + return false; // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) foreach (var role in user.Roles) - { if (!jwtRoleNames.Contains(role.Name)) return false; - } // Check if jwt contains only roles of user if (jwtRoleNames.Count != user.Roles.Count) |
