From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../DevHive.Web.Models/Post/ReadPostWebModel.cs | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs (limited to 'src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs') diff --git a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs new file mode 100644 index 0000000..8238f47 --- /dev/null +++ b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Common.Models.Misc; + +namespace DevHive.Web.Models.Post +{ + public class ReadPostWebModel + { + public Guid PostId { get; set; } + + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } + + public List FileUrls { get; set; } + // public List Files { get; set; } + } +} -- cgit v1.2.3 From 4c03712af14c37718b7be5b23fcadeb86f2a2191 Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 15 Feb 2021 19:06:14 +0200 Subject: Code Analyzer added to all csproj; Removed unnessessary code; Fixed formatting --- .../DevHive.Common.Models/DevHive.Common.csproj | 1 + .../DevHive.Data.Models/DevHive.Data.Models.csproj | 1 + .../DevHive.Data.Tests/DevHive.Data.Tests.csproj | 13 +++--- src/Data/DevHive.Data/DevHive.Data.csproj | 1 + .../DevHive.Services.Models.csproj | 9 ++-- .../DevHive.Services.Tests.csproj | 13 +++--- .../DevHive.Services.Tests/UserService.Tests.cs | 8 ++-- .../Configurations/Mapping/PostMappings.cs | 1 - .../Configurations/Mapping/RatingMappings.cs | 2 - .../DevHive.Services/DevHive.Services.csproj | 17 +++---- .../DevHive.Services/Interfaces/ICloudService.cs | 2 - .../DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../DevHive.Services/Options/JWTOptions.cs | 14 ------ .../DevHive.Services/Options/JwtOptions.cs | 14 ++++++ .../DevHive.Services/Services/FeedService.cs | 2 +- .../DevHive.Services/Services/PostService.cs | 15 +++--- .../DevHive.Services/Services/RateService.cs | 2 +- .../DevHive.Services/Services/RoleService.cs | 9 ++-- .../DevHive.Services/Services/UserService.cs | 15 +++--- .../Attributes/GoodPasswordAttribute.cs | 23 +++++++++ .../Attributes/GoodPasswordModelValidation.cs | 24 ---------- .../Attributes/OnlyLettersModelValidation.cs | 4 +- .../DevHive.Web.Models/DevHive.Web.Models.csproj | 8 ++-- .../DevHive.Web.Models/Post/ReadPostWebModel.cs | 1 - .../DevHive.Web.Tests/CommentController.Tests.cs | 35 +++++++------- src/Web/DevHive.Web.Tests/DevHive.Web.Tests.csproj | 11 +++-- .../DevHive.Web.Tests/LanguageController.Tests.cs | 4 +- src/Web/DevHive.Web.Tests/PostController.Tests.cs | 29 ++++++------ src/Web/DevHive.Web.Tests/RoleController.Tests.cs | 4 +- .../TechnologyController.Tests.cs | 4 +- src/Web/DevHive.Web.Tests/UserController.Tests.cs | 46 ++++++++---------- .../Extensions/ConfigureAutoMapper.cs | 5 +- .../ConfigureExceptionHandlerMiddleware.cs | 2 - .../Configurations/Extensions/ConfigureJWT.cs | 54 ---------------------- .../Configurations/Extensions/ConfigureJwt.cs | 54 ++++++++++++++++++++++ src/Web/DevHive.Web/DevHive.Web.csproj | 21 +++++---- .../DevHive.Web/Middleware/ExceptionMiddleware.cs | 14 ++---- 37 files changed, 229 insertions(+), 255 deletions(-) delete mode 100644 src/Services/DevHive.Services/Options/JWTOptions.cs create mode 100644 src/Services/DevHive.Services/Options/JwtOptions.cs create mode 100644 src/Web/DevHive.Web.Models/Attributes/GoodPasswordAttribute.cs delete mode 100644 src/Web/DevHive.Web.Models/Attributes/GoodPasswordModelValidation.cs delete mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs (limited to 'src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs') diff --git a/src/Common/DevHive.Common.Models/DevHive.Common.csproj b/src/Common/DevHive.Common.Models/DevHive.Common.csproj index 4829c80..f6d662c 100644 --- a/src/Common/DevHive.Common.Models/DevHive.Common.csproj +++ b/src/Common/DevHive.Common.Models/DevHive.Common.csproj @@ -4,6 +4,7 @@ + true diff --git a/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj b/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj index e58a6d8..e9dc644 100644 --- a/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj +++ b/src/Data/DevHive.Data.Models/DevHive.Data.Models.csproj @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj index 568edda..2af369f 100644 --- a/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj +++ b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj @@ -4,14 +4,15 @@ false - - - - - + + + + + + - + true diff --git a/src/Data/DevHive.Data/DevHive.Data.csproj b/src/Data/DevHive.Data/DevHive.Data.csproj index fac1581..46928c6 100644 --- a/src/Data/DevHive.Data/DevHive.Data.csproj +++ b/src/Data/DevHive.Data/DevHive.Data.csproj @@ -12,6 +12,7 @@ + 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 @@ net5.0 - - + + - - + - + \ 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 @@ false - - - - - + + + + + + - + true 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 LanguageRepositoryMock { get; set; } private Mock TechnologyRepositoryMock { get; set; } private Mock 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(); this.LanguageRepositoryMock = new Mock(); this.TechnologyRepositoryMock = new Mock(); - this.JWTOptions = new JWTOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); + this.JwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); this.MapperMock = new Mock(); // 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 roles) { - byte[] signingKey = Encoding.ASCII.GetBytes(this.JWTOptions.Secret); + byte[] signingKey = Encoding.ASCII.GetBytes(this.JwtOptions.Secret); HashSet 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(); - // .ForMember(dest => dest.Files, src => src.Ignore()); CreateMap() .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() - // .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 @@ net5.0 - + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - + + + + + - - + + true latest - + \ 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> UploadFilesToCloud(List formFiles); - // Task> GetFilesFromCloud(List fileUrls); - Task RemoveFilesFromCloud(List 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 CreateRole(CreateRoleServiceModel roleServiceModel); + Task CreateRole(CreateRoleServiceModel createRoleServiceModel); Task GetRoleById(Guid id); diff --git a/src/Services/DevHive.Services/Options/JWTOptions.cs b/src/Services/DevHive.Services/Options/JWTOptions.cs deleted file mode 100644 index 95458f5..0000000 --- a/src/Services/DevHive.Services/Options/JWTOptions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.Extensions.Options; - -namespace DevHive.Services.Options -{ - public class JWTOptions - { - public JWTOptions(string secret) - { - this.Secret = secret; - } - - public string Secret { get; init; } - } -} diff --git a/src/Services/DevHive.Services/Options/JwtOptions.cs b/src/Services/DevHive.Services/Options/JwtOptions.cs new file mode 100644 index 0000000..d973f45 --- /dev/null +++ b/src/Services/DevHive.Services/Options/JwtOptions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Options; + +namespace DevHive.Services.Options +{ + public class JwtOptions + { + public JwtOptions(string secret) + { + this.Secret = secret; + } + + public string Secret { get; init; } + } +} 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 /// /// 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. /// public async Task 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 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 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 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 /// /// Returns all values from a given claim type /// - private List GetClaimTypeValues(string type, IEnumerable claims) + private static List GetClaimTypeValues(string type, IEnumerable claims) { List toReturn = new(); @@ -227,9 +226,9 @@ namespace DevHive.Services.Services #endregion #region Misc - private List GetPostAttachmentsFromUrls(Post post, List fileUrls) + private static List GetPostAttachmentsFromUrls(Post post, List fileUrls) { - List postAttachments = new List(); + List 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 CreateRole(CreateRoleServiceModel roleServiceModel) + public async Task 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(roleServiceModel); + Role role = this._roleMapper.Map(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 _userManager; private readonly RoleManager _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 userManager, RoleManager 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 { 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) diff --git a/src/Web/DevHive.Web.Models/Attributes/GoodPasswordAttribute.cs b/src/Web/DevHive.Web.Models/Attributes/GoodPasswordAttribute.cs new file mode 100644 index 0000000..c452499 --- /dev/null +++ b/src/Web/DevHive.Web.Models/Attributes/GoodPasswordAttribute.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Models.Attributes +{ + public class GoodPasswordAttribute : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + for (int i = 0; i < stringValue.Length; i++) + { + if (char.IsDigit(stringValue[i])) + { + base.ErrorMessage = "Password must be atleast 5 characters long!"; + return stringValue.Length >= 5; + } + } + base.ErrorMessage = "Password must contain atleast 1 digit!"; + return false; + } + } +} diff --git a/src/Web/DevHive.Web.Models/Attributes/GoodPasswordModelValidation.cs b/src/Web/DevHive.Web.Models/Attributes/GoodPasswordModelValidation.cs deleted file mode 100644 index 5ecb41a..0000000 --- a/src/Web/DevHive.Web.Models/Attributes/GoodPasswordModelValidation.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Attributes -{ - public class GoodPassword : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - for (int i = 0; i < stringValue.Length; i++) - { - if (Char.IsDigit(stringValue[i])) - { - base.ErrorMessage = "Password must be atleast 5 characters long!"; - return stringValue.Length >= 5; - } - } - base.ErrorMessage = "Password must contain atleast 1 digit!"; - return false; - } - } -} diff --git a/src/Web/DevHive.Web.Models/Attributes/OnlyLettersModelValidation.cs b/src/Web/DevHive.Web.Models/Attributes/OnlyLettersModelValidation.cs index 0f6adb1..faaeee4 100644 --- a/src/Web/DevHive.Web.Models/Attributes/OnlyLettersModelValidation.cs +++ b/src/Web/DevHive.Web.Models/Attributes/OnlyLettersModelValidation.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace DevHive.Web.Models.Attributes { - public class OnlyLetters : ValidationAttribute + public class OnlyLettersAttribute : ValidationAttribute { public override bool IsValid(object value) { @@ -11,7 +11,7 @@ namespace DevHive.Web.Models.Attributes foreach (char ch in stringValue) { - if (!Char.IsLetter(ch)) + if (!char.IsLetter(ch)) return false; } return true; diff --git a/src/Web/DevHive.Web.Models/DevHive.Web.Models.csproj b/src/Web/DevHive.Web.Models/DevHive.Web.Models.csproj index ca49b8c..64d0bd0 100644 --- a/src/Web/DevHive.Web.Models/DevHive.Web.Models.csproj +++ b/src/Web/DevHive.Web.Models/DevHive.Web.Models.csproj @@ -3,10 +3,10 @@ net5.0 - + - - + + - + \ No newline at end of file diff --git a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs index 8238f47..3ae93aa 100644 --- a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs +++ b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs @@ -21,6 +21,5 @@ namespace DevHive.Web.Models.Post public List Comments { get; set; } public List FileUrls { get; set; } - // public List Files { get; set; } } } diff --git a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs index 3a03f1a..98397e7 100644 --- a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs @@ -35,11 +35,11 @@ namespace DevHive.Web.Tests public void AddComment_ReturnsOkObjectResult_WhenCommentIsSuccessfullyCreated() { Guid id = Guid.NewGuid(); - CreateCommentWebModel createCommentWebModel = new CreateCommentWebModel + CreateCommentWebModel createCommentWebModel = new() { Message = MESSAGE }; - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; @@ -67,11 +67,11 @@ namespace DevHive.Web.Tests public void AddComment_ReturnsBadRequestObjectResult_WhenCommentIsNotCreatedSuccessfully() { Guid id = Guid.NewGuid(); - CreateCommentWebModel createCommentWebModel = new CreateCommentWebModel + CreateCommentWebModel createCommentWebModel = new() { Message = MESSAGE }; - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; @@ -86,8 +86,8 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); - BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; - string resultMessage = badRequsetObjectResult.Value.ToString(); + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequestObjectResult.Value.ToString(); Assert.AreEqual(errorMessage, resultMessage); } @@ -95,8 +95,7 @@ namespace DevHive.Web.Tests [Test] public void AddComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - Guid id = Guid.NewGuid(); - CreateCommentWebModel createCommentWebModel = new CreateCommentWebModel + CreateCommentWebModel createCommentWebModel = new() { Message = MESSAGE }; @@ -115,11 +114,11 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - ReadCommentServiceModel readCommentServiceModel = new ReadCommentServiceModel + ReadCommentServiceModel readCommentServiceModel = new() { Message = MESSAGE }; - ReadCommentWebModel readCommentWebModel = new ReadCommentWebModel + ReadCommentWebModel readCommentWebModel = new() { Message = MESSAGE }; @@ -132,7 +131,7 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); OkObjectResult okObjectResult = result as OkObjectResult; - ReadCommentWebModel resultModel = okObjectResult.Value as Models.Comment.ReadCommentWebModel; + ReadCommentWebModel resultModel = okObjectResult.Value as ReadCommentWebModel; Assert.AreEqual(MESSAGE, resultModel.Message); } @@ -143,11 +142,11 @@ namespace DevHive.Web.Tests public void Update_ShouldReturnOkResult_WhenCommentIsUpdatedSuccessfully() { Guid id = Guid.NewGuid(); - UpdateCommentWebModel updateCommentWebModel = new UpdateCommentWebModel + UpdateCommentWebModel updateCommentWebModel = new() { NewMessage = MESSAGE }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + UpdateCommentServiceModel updateCommentServiceModel = new() { NewMessage = MESSAGE }; @@ -171,11 +170,11 @@ namespace DevHive.Web.Tests public void Update_ShouldReturnBadObjectResult_WhenCommentIsNotUpdatedSuccessfully() { string message = "Unable to update comment!"; - UpdateCommentWebModel updateCommentWebModel = new UpdateCommentWebModel + UpdateCommentWebModel updateCommentWebModel = new() { NewMessage = MESSAGE }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + UpdateCommentServiceModel updateCommentServiceModel = new() { NewMessage = MESSAGE }; @@ -196,7 +195,7 @@ namespace DevHive.Web.Tests [Test] public void Update_ShouldReturnUnauthorizedResult_WhenUserIsNotAuthorized() { - UpdateCommentWebModel updateCommentWebModel = new UpdateCommentWebModel + UpdateCommentWebModel updateCommentWebModel = new() { NewMessage = MESSAGE }; @@ -224,7 +223,7 @@ namespace DevHive.Web.Tests } [Test] - public void DeletComment_ReturnsBadRequestObjectResult_WhenCommentIsNotDeletedSuccessfully() + public void DeleteComment_ReturnsBadRequestObjectResult_WhenCommentIsNotDeletedSuccessfully() { string message = "Could not delete Comment"; Guid id = Guid.NewGuid(); @@ -243,7 +242,7 @@ namespace DevHive.Web.Tests } [Test] - public void DeletComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() + public void DeleteComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); diff --git a/src/Web/DevHive.Web.Tests/DevHive.Web.Tests.csproj b/src/Web/DevHive.Web.Tests/DevHive.Web.Tests.csproj index f8390de..465698c 100644 --- a/src/Web/DevHive.Web.Tests/DevHive.Web.Tests.csproj +++ b/src/Web/DevHive.Web.Tests/DevHive.Web.Tests.csproj @@ -4,13 +4,14 @@ false - - - - + + + + + - + true diff --git a/src/Web/DevHive.Web.Tests/LanguageController.Tests.cs b/src/Web/DevHive.Web.Tests/LanguageController.Tests.cs index 7c8d64e..af4672a 100644 --- a/src/Web/DevHive.Web.Tests/LanguageController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/LanguageController.Tests.cs @@ -81,8 +81,8 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); - BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; - string resultMessage = badRequsetObjectResult.Value.ToString(); + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequestObjectResult.Value.ToString(); Assert.AreEqual(errorMessage, resultMessage); } diff --git a/src/Web/DevHive.Web.Tests/PostController.Tests.cs b/src/Web/DevHive.Web.Tests/PostController.Tests.cs index 96b0356..f10f8dd 100644 --- a/src/Web/DevHive.Web.Tests/PostController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/PostController.Tests.cs @@ -32,11 +32,11 @@ namespace DevHive.Web.Tests [Test] public void CreatePost_ReturnsOkObjectResult_WhenPostIsSuccessfullyCreated() { - CreatePostWebModel createPostWebModel = new CreatePostWebModel + CreatePostWebModel createPostWebModel = new() { Message = MESSAGE }; - CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel + CreatePostServiceModel createPostServiceModel = new() { Message = MESSAGE }; @@ -64,11 +64,11 @@ namespace DevHive.Web.Tests [Test] public void CreatePost_ReturnsBadRequestObjectResult_WhenPostIsNotCreatedSuccessfully() { - CreatePostWebModel createTechnologyWebModel = new CreatePostWebModel + CreatePostWebModel createTechnologyWebModel = new() { Message = MESSAGE }; - CreatePostServiceModel createTechnologyServiceModel = new CreatePostServiceModel + CreatePostServiceModel createTechnologyServiceModel = new() { Message = MESSAGE }; @@ -83,8 +83,8 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); - BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; - string resultMessage = badRequsetObjectResult.Value.ToString(); + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequestObjectResult.Value.ToString(); Assert.AreEqual(errorMessage, resultMessage); } @@ -92,8 +92,7 @@ namespace DevHive.Web.Tests [Test] public void CreatePost_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - Guid id = Guid.NewGuid(); - CreatePostWebModel createPostWebModel = new CreatePostWebModel + CreatePostWebModel createPostWebModel = new() { Message = MESSAGE }; @@ -112,11 +111,11 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel + ReadPostServiceModel readPostServiceModel = new() { Message = MESSAGE }; - ReadPostWebModel readPostWebModel = new ReadPostWebModel + ReadPostWebModel readPostWebModel = new() { Message = MESSAGE }; @@ -140,11 +139,11 @@ namespace DevHive.Web.Tests public void Update_ShouldReturnOkResult_WhenPostIsUpdatedSuccessfully() { Guid id = Guid.NewGuid(); - UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + UpdatePostWebModel updatePostWebModel = new() { NewMessage = MESSAGE }; - UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel + UpdatePostServiceModel updatePostServiceModel = new() { NewMessage = MESSAGE }; @@ -163,11 +162,11 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); string message = "Could not update post!"; - UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + UpdatePostWebModel updatePostWebModel = new() { NewMessage = MESSAGE }; - UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel + UpdatePostServiceModel updatePostServiceModel = new() { NewMessage = MESSAGE }; @@ -188,7 +187,7 @@ namespace DevHive.Web.Tests [Test] public void Update_ShouldReturnUnauthorizedResult_WhenUserIsNotAuthorized() { - UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + UpdatePostWebModel updatePostWebModel = new() { NewMessage = MESSAGE }; diff --git a/src/Web/DevHive.Web.Tests/RoleController.Tests.cs b/src/Web/DevHive.Web.Tests/RoleController.Tests.cs index 64e3f11..ff80dc0 100644 --- a/src/Web/DevHive.Web.Tests/RoleController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/RoleController.Tests.cs @@ -81,8 +81,8 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); - BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; - string resultMessage = badRequsetObjectResult.Value.ToString(); + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequestObjectResult.Value.ToString(); Assert.AreEqual(errorMessage, resultMessage); } diff --git a/src/Web/DevHive.Web.Tests/TechnologyController.Tests.cs b/src/Web/DevHive.Web.Tests/TechnologyController.Tests.cs index 164bcbf..a00f1db 100644 --- a/src/Web/DevHive.Web.Tests/TechnologyController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/TechnologyController.Tests.cs @@ -84,8 +84,8 @@ namespace DevHive.Web.Tests Assert.IsInstanceOf(result); - BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; - string resultMessage = badRequsetObjectResult.Value.ToString(); + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequestObjectResult.Value.ToString(); Assert.AreEqual(errorMessage, resultMessage); } diff --git a/src/Web/DevHive.Web.Tests/UserController.Tests.cs b/src/Web/DevHive.Web.Tests/UserController.Tests.cs index 7457ad7..13f618e 100644 --- a/src/Web/DevHive.Web.Tests/UserController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/UserController.Tests.cs @@ -33,17 +33,17 @@ namespace DevHive.Web.Tests public void LoginUser_ReturnsOkObjectResult_WhenUserIsSuccessfullyLoggedIn() { Guid id = Guid.NewGuid(); - LoginWebModel loginWebModel = new LoginWebModel + LoginWebModel loginWebModel = new() { UserName = USERNAME }; - LoginServiceModel loginServiceModel = new LoginServiceModel + LoginServiceModel loginServiceModel = new() { UserName = USERNAME }; string token = "goshotrapov"; - TokenModel tokenModel = new TokenModel(token); - TokenWebModel tokenWebModel = new TokenWebModel(token); + TokenModel tokenModel = new(token); + TokenWebModel tokenWebModel = new(token); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(loginServiceModel); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(tokenWebModel); @@ -61,18 +61,17 @@ namespace DevHive.Web.Tests [Test] public void RegisterUser_ReturnsOkObjectResult_WhenUserIsSuccessfullyRegistered() { - Guid id = Guid.NewGuid(); - RegisterWebModel registerWebModel = new RegisterWebModel + RegisterWebModel registerWebModel = new() { UserName = USERNAME }; - RegisterServiceModel registerServiceModel = new RegisterServiceModel + RegisterServiceModel registerServiceModel = new() { UserName = USERNAME }; string token = "goshotrapov"; - TokenModel tokenModel = new TokenModel(token); - TokenWebModel tokenWebModel = new TokenWebModel(token); + TokenModel tokenModel = new(token); + TokenWebModel tokenWebModel = new(token); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(registerServiceModel); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(tokenWebModel); @@ -95,11 +94,11 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - UserServiceModel userServiceModel = new UserServiceModel + UserServiceModel userServiceModel = new() { UserName = USERNAME }; - UserWebModel userWebModel = new UserWebModel + UserWebModel userWebModel = new() { UserName = USERNAME }; @@ -121,12 +120,6 @@ namespace DevHive.Web.Tests [Test] public void GetById_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - Guid id = Guid.NewGuid(); - UserWebModel userWebModel = new UserWebModel - { - UserName = USERNAME - }; - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); IActionResult result = this.UserController.GetById(Guid.NewGuid(), null).Result; @@ -137,12 +130,11 @@ namespace DevHive.Web.Tests [Test] public void GetUser_ReturnsTheUser_WhenItExists() { - Guid id = Guid.NewGuid(); - UserWebModel userWebModel = new UserWebModel + UserWebModel userWebModel = new() { UserName = USERNAME }; - UserServiceModel userServiceModel = new UserServiceModel + UserServiceModel userServiceModel = new() { UserName = USERNAME }; @@ -166,15 +158,15 @@ namespace DevHive.Web.Tests public void Update_ShouldReturnOkResult_WhenUserIsUpdatedSuccessfully() { Guid id = Guid.NewGuid(); - UpdateUserWebModel updateUserWebModel = new UpdateUserWebModel + UpdateUserWebModel updateUserWebModel = new() { UserName = USERNAME }; - UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel + UpdateUserServiceModel updateUserServiceModel = new() { UserName = USERNAME }; - UserServiceModel userServiceModel = new UserServiceModel + UserServiceModel userServiceModel = new() { UserName = USERNAME }; @@ -192,13 +184,13 @@ namespace DevHive.Web.Tests public void UpdateProfilePicture_ShouldReturnOkObjectResult_WhenProfilePictureIsUpdatedSuccessfully() { string profilePictureURL = "goshotrapov"; - UpdateProfilePictureWebModel updateProfilePictureWebModel = new UpdateProfilePictureWebModel(); - UpdateProfilePictureServiceModel updateProfilePictureServiceModel = new UpdateProfilePictureServiceModel(); - ProfilePictureServiceModel profilePictureServiceModel = new ProfilePictureServiceModel + UpdateProfilePictureWebModel updateProfilePictureWebModel = new(); + UpdateProfilePictureServiceModel updateProfilePictureServiceModel = new(); + ProfilePictureServiceModel profilePictureServiceModel = new() { ProfilePictureURL = profilePictureURL }; - ProfilePictureWebModel profilePictureWebModel = new ProfilePictureWebModel + ProfilePictureWebModel profilePictureWebModel = new() { ProfilePictureURL = profilePictureURL }; diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs index 8b7d657..0b8194e 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs @@ -1,6 +1,5 @@ using System; using AutoMapper; -//using AutoMapper.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; @@ -15,10 +14,10 @@ namespace DevHive.Web.Configurations.Extensions public static void UseAutoMapperConfiguration(this IApplicationBuilder app) { - var config = new MapperConfiguration(cfg => + _ = new MapperConfiguration(cfg => { cfg.AllowNullCollections = true; }); } } -} \ No newline at end of file +} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs index 286727f..c017a8c 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -6,8 +6,6 @@ namespace DevHive.Web.Configurations.Extensions { public static class ConfigureExceptionHandlerMiddleware { - public static void ExceptionHandlerMiddlewareConfiguration(this IServiceCollection services) { } - public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) { app.UseMiddleware(); diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs deleted file mode 100644 index d422bc8..0000000 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.Text; -using System.Threading.Tasks; -using DevHive.Services.Options; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.IdentityModel.Tokens; - -namespace DevHive.Web.Configurations.Extensions -{ - public static class JWTExtensions - { - public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) - { - services.AddSingleton(new JWTOptions(configuration - .GetSection("AppSettings") - .GetSection("Secret") - .Value)); - - // Get key from appsettings.json - var key = Encoding.ASCII.GetBytes(configuration - .GetSection("AppSettings") - .GetSection("Secret") - .Value); - - // Setup Jwt Authentication - services.AddAuthentication(x => - { - x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }) - .AddJwtBearer(x => - { - x.Events = new JwtBearerEvents - { - OnTokenValidated = context => - { - // TODO: add more authentication - return Task.CompletedTask; - } - }; - x.RequireHttpsMetadata = false; - x.SaveToken = true; - x.TokenValidationParameters = new TokenValidationParameters - { - //ValidateIssuerSigningKey = false, - IssuerSigningKey = new SymmetricSecurityKey(key), - ValidateIssuer = false, - ValidateAudience = false - }; - }); - } - } -} \ No newline at end of file diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs new file mode 100644 index 0000000..03d4b11 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs @@ -0,0 +1,54 @@ +using System.Text; +using System.Threading.Tasks; +using DevHive.Services.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureJwt + { + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(new JwtOptions(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value)); + + // Get key from appsettings.json + var key = Encoding.ASCII.GetBytes(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value); + + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + // TODO: add more authentication + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + //ValidateIssuerSigningKey = false, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; + }); + } + } +} diff --git a/src/Web/DevHive.Web/DevHive.Web.csproj b/src/Web/DevHive.Web/DevHive.Web.csproj index 6f78b69..6511c37 100644 --- a/src/Web/DevHive.Web/DevHive.Web.csproj +++ b/src/Web/DevHive.Web/DevHive.Web.csproj @@ -7,21 +7,22 @@ latest - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - + + + + + + + - - + + \ No newline at end of file diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs index cb6d4ca..f159b69 100644 --- a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs +++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -9,18 +9,11 @@ namespace DevHive.Web.Middleware public class ExceptionMiddleware { private readonly RequestDelegate _next; - // private readonly ILogger _logger; public ExceptionMiddleware(RequestDelegate next) { this._next = next; - // this._logger = logger; } - // public ExceptionMiddleware(RequestDelegate next, ILogger logger) - // { - // this._logger = logger; - // this._next = next; - // } public async Task InvokeAsync(HttpContext httpContext) { @@ -30,20 +23,19 @@ namespace DevHive.Web.Middleware } catch (Exception ex) { - // this._logger.LogError($"Something went wrong: {ex}"); await HandleExceptionAsync(httpContext, ex); } } - private Task HandleExceptionAsync(HttpContext context, Exception exception) + private static Task HandleExceptionAsync(HttpContext context, Exception exception) { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return context.Response.WriteAsync(new { - StatusCode = context.Response.StatusCode, - Message = exception.Message + context.Response.StatusCode, + exception.Message }.ToString()); } } -- cgit v1.2.3 From a851adfac47a26cae83e9161d37902a219e5ebf3 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sun, 21 Feb 2021 22:21:08 +0200 Subject: model update for rating --- src/Data/DevHive.Data.Models/Interfaces/IRating.cs | 7 +- src/Data/DevHive.Data.Models/Post.cs | 4 +- src/Data/DevHive.Data.Models/Rating.cs | 5 +- src/Data/DevHive.Data/DevHiveContext.cs | 5 +- .../DevHive.Data/Interfaces/IRatingRepository.cs | 3 +- .../DevHive.Data/Repositories/PostRepository.cs | 2 +- .../DevHive.Data/Repositories/RatingRepository.cs | 5 +- .../Post/Rating/CreateRatingServiceModel.cs | 13 ++++ .../Post/Rating/RatePostServiceModel.cs | 13 ---- .../Post/Rating/ReadPostRatingServiceModel.cs | 15 ---- .../Post/Rating/ReadRatingServiceModel.cs | 15 ++++ .../Post/ReadPostServiceModel.cs | 3 +- .../DevHive.Services/Interfaces/IRateService.cs | 14 ---- .../DevHive.Services/Interfaces/IRatingService.cs | 14 ++++ .../DevHive.Services/Services/PostService.cs | 1 + .../DevHive.Services/Services/RateService.cs | 80 ---------------------- .../DevHive.Services/Services/RatingService.cs | 80 ++++++++++++++++++++++ .../DevHive.Web.Models/Post/ReadPostWebModel.cs | 2 + .../Extensions/ConfigureDependencyInjection.cs | 2 +- .../Configurations/Mapping/RatingMappings.cs | 4 +- src/Web/DevHive.Web/Controllers/RateController.cs | 8 +-- 21 files changed, 152 insertions(+), 143 deletions(-) create mode 100644 src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services/Interfaces/IRateService.cs create mode 100644 src/Services/DevHive.Services/Interfaces/IRatingService.cs delete mode 100644 src/Services/DevHive.Services/Services/RateService.cs create mode 100644 src/Services/DevHive.Services/Services/RatingService.cs (limited to 'src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs') diff --git a/src/Data/DevHive.Data.Models/Interfaces/IRating.cs b/src/Data/DevHive.Data.Models/Interfaces/IRating.cs index 1b081b0..f04f823 100644 --- a/src/Data/DevHive.Data.Models/Interfaces/IRating.cs +++ b/src/Data/DevHive.Data.Models/Interfaces/IRating.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using DevHive.Data.Models; @@ -5,10 +6,10 @@ namespace DevHive.Data.Models.Interfaces { public interface IRating : IModel { - // Post Post { get; set; } + bool IsLike { get; set; } - int Rate { get; set; } + Post Post { get; set; } - // HashSet UsersThatRated { get; set; } + User User { get; set; } } } diff --git a/src/Data/DevHive.Data.Models/Post.cs b/src/Data/DevHive.Data.Models/Post.cs index 15b6b77..716248f 100644 --- a/src/Data/DevHive.Data.Models/Post.cs +++ b/src/Data/DevHive.Data.Models/Post.cs @@ -19,7 +19,9 @@ namespace DevHive.Data.Models public List Comments { get; set; } = new(); - public Rating Rating { get; set; } = new(); + public List Ratings { get; set; } + + public int CurrentRating { get; set; } public List Attachments { get; set; } = new(); } diff --git a/src/Data/DevHive.Data.Models/Rating.cs b/src/Data/DevHive.Data.Models/Rating.cs index 13fdbce..8743a3e 100644 --- a/src/Data/DevHive.Data.Models/Rating.cs +++ b/src/Data/DevHive.Data.Models/Rating.cs @@ -6,12 +6,13 @@ namespace DevHive.Data.Models { public class Rating : IRating { + //if adding rating to comments change Post for intreface IRatable! public Guid Id { get; set; } - public Guid PostId { get; set; } + public User User { get; set; } public Post Post { get; set; } - public int Rate { get; set; } + public bool IsLike { get; set; } } } diff --git a/src/Data/DevHive.Data/DevHiveContext.cs b/src/Data/DevHive.Data/DevHiveContext.cs index ece3439..b0bbdc6 100644 --- a/src/Data/DevHive.Data/DevHiveContext.cs +++ b/src/Data/DevHive.Data/DevHiveContext.cs @@ -88,11 +88,10 @@ namespace DevHive.Data builder.Entity() .HasOne(x => x.Post) - .WithOne(x => x.Rating) - .HasForeignKey(x => x.PostId); + .WithMany(x => x.Ratings); builder.Entity() - .HasOne(x => x.Rating) + .HasMany(x => x.Ratings) .WithOne(x => x.Post); /* User Rated Posts */ diff --git a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs index b5aff88..c8636b6 100644 --- a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories.Interfaces; @@ -7,7 +8,7 @@ namespace DevHive.Data.Interfaces { public interface IRatingRepository : IRepository { - Task GetRatingByPostId(Guid postId); + Task> GetRatingsByPostId(Guid postId); Task UserRatedPost(Guid userId, Guid postId); } } diff --git a/src/Data/DevHive.Data/Repositories/PostRepository.cs b/src/Data/DevHive.Data/Repositories/PostRepository.cs index b6c5e37..0ab1afb 100644 --- a/src/Data/DevHive.Data/Repositories/PostRepository.cs +++ b/src/Data/DevHive.Data/Repositories/PostRepository.cs @@ -36,7 +36,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Comments) .Include(x => x.Creator) .Include(x => x.Attachments) - // .Include(x => x.Rating) + .Include(x => x.CurrentRating) .FirstOrDefaultAsync(x => x.Id == id); } diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index b361693..2f56aee 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces; @@ -19,10 +20,10 @@ namespace DevHive.Data.Repositories this._postRepository = postRepository; } - public async Task GetRatingByPostId(Guid postId) + public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating - .FirstOrDefaultAsync(x => x.Post.Id == postId); + .Where(x => x.Post.Id == postId).ToListAsync(); } public async Task UserRatedPost(Guid userId, Guid postId) diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs new file mode 100644 index 0000000..aaeab73 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class CreateRatingServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs deleted file mode 100644 index d4eb7bd..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class RatePostServiceModel - { - public Guid UserId { get; set; } - - public Guid PostId { get; set; } - - public bool Liked { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs deleted file mode 100644 index 8c73aaf..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class ReadPostRatingServiceModel - { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - - public int Likes { get; set; } - - public int Dislikes { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..dbc7ecc --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class ReadRatingServiceModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs index a7aa882..33d6520 100644 --- a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs +++ b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs @@ -21,6 +21,7 @@ namespace DevHive.Services.Models.Post public List Comments { get; set; } = new(); public List FileUrls { get; set; } - // public List Files { get; set; } = new(); + + public int CurrentRating { get; set; } } } diff --git a/src/Services/DevHive.Services/Interfaces/IRateService.cs b/src/Services/DevHive.Services/Interfaces/IRateService.cs deleted file mode 100644 index 359ef55..0000000 --- a/src/Services/DevHive.Services/Interfaces/IRateService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; - -namespace DevHive.Services.Interfaces -{ - public interface IRateService - { - Task RatePost(RatePostServiceModel ratePostServiceModel); - - bool HasUserRatedThisPost(User user, Post post); - } -} diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs new file mode 100644 index 0000000..adb4313 --- /dev/null +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Rating; + +namespace DevHive.Services.Interfaces +{ + public interface IRatingService + { + Task RatePost(CreateRatingServiceModel ratePostServiceModel); + + bool HasUserRatedThisPost(User user, Post post); + } +} diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs index a3d5117..4bece90 100644 --- a/src/Services/DevHive.Services/Services/PostService.cs +++ b/src/Services/DevHive.Services/Services/PostService.cs @@ -46,6 +46,7 @@ namespace DevHive.Services.Services post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); post.TimeCreated = DateTime.Now; + post.CurrentRating = 0; bool success = await this._postRepository.AddAsync(post); if (success) diff --git a/src/Services/DevHive.Services/Services/RateService.cs b/src/Services/DevHive.Services/Services/RateService.cs deleted file mode 100644 index caf4b80..0000000 --- a/src/Services/DevHive.Services/Services/RateService.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Interfaces; -using DevHive.Data.Models; -using DevHive.Services.Interfaces; -using DevHive.Services.Models.Post.Rating; - -namespace DevHive.Services.Services -{ - public class RateService : IRateService - { - private readonly IPostRepository _postRepository; - private readonly IUserRepository _userRepository; - private readonly IRatingRepository _ratingRepository; - private readonly IMapper _mapper; - - public RateService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper) - { - this._postRepository = postRepository; - this._ratingRepository = ratingRepository; - this._userRepository = userRepository; - this._mapper = mapper; - } - - public async Task RatePost(RatePostServiceModel ratePostServiceModel) - { - throw new NotImplementedException(); - // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) - // throw new ArgumentException("Post does not exist!"); - - // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) - // throw new ArgumentException("User does not exist!"); - - // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); - // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId); - - // if (this.HasUserRatedThisPost(user, post)) - // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); - - // this.Rate(user, post, ratePostServiceModel.Liked); - - // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating); - // if (!success) - // throw new InvalidOperationException("Unable to rate the post!"); - - // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id); - // return this._mapper.Map(newRating); - } - - public async Task RemoveUserRateFromPost(Guid userId, Guid postId) - { - throw new NotImplementedException(); - // Post post = await this._postRepository.GetByIdAsync(postId); - // User user = await this._userRepository.GetByIdAsync(userId); - - // if (!this.HasUserRatedThisPost(user, post)) - // throw new ArgumentException("You haven't rated this post, lmao!"); - } - - public bool HasUserRatedThisPost(User user, Post post) - { - throw new NotImplementedException(); - // return post.Rating.UsersThatRated - // .Any(x => x.Id == user.Id); - } - - private void Rate(User user, Post post, bool liked) - { - throw new NotImplementedException(); - // if (liked) - // post.Rating.Rate++; - // else - // post.Rating.Rate--; - - // post.Rating.UsersThatRated.Add(user); - } - } -} diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs new file mode 100644 index 0000000..45ff7c0 --- /dev/null +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -0,0 +1,80 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Rating; + +namespace DevHive.Services.Services +{ + public class RatingService : IRatingService + { + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; + private readonly IRatingRepository _ratingRepository; + private readonly IMapper _mapper; + + public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper) + { + this._postRepository = postRepository; + this._ratingRepository = ratingRepository; + this._userRepository = userRepository; + this._mapper = mapper; + } + + public async Task RatePost(CreateRatingServiceModel ratePostServiceModel) + { + throw new NotImplementedException(); + // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) + // throw new ArgumentException("Post does not exist!"); + + // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) + // throw new ArgumentException("User does not exist!"); + + // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); + // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId); + + // if (this.HasUserRatedThisPost(user, post)) + // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); + + // this.Rate(user, post, ratePostServiceModel.Liked); + + // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating); + // if (!success) + // throw new InvalidOperationException("Unable to rate the post!"); + + // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id); + // return this._mapper.Map(newRating); + } + + public async Task RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + // Post post = await this._postRepository.GetByIdAsync(postId); + // User user = await this._userRepository.GetByIdAsync(userId); + + // if (!this.HasUserRatedThisPost(user, post)) + // throw new ArgumentException("You haven't rated this post, lmao!"); + } + + public bool HasUserRatedThisPost(User user, Post post) + { + throw new NotImplementedException(); + // return post.Rating.UsersThatRated + // .Any(x => x.Id == user.Id); + } + + private void Rate(User user, Post post, bool liked) + { + throw new NotImplementedException(); + // if (liked) + // post.Rating.Rate++; + // else + // post.Rating.Rate--; + + // post.Rating.UsersThatRated.Add(user); + } + } +} diff --git a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs index 3ae93aa..d6ea1f4 100644 --- a/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs +++ b/src/Web/DevHive.Web.Models/Post/ReadPostWebModel.cs @@ -21,5 +21,7 @@ namespace DevHive.Web.Models.Post public List Comments { get; set; } public List FileUrls { get; set; } + + public int CurrentRating { get; set; } } } diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index c547951..153b17f 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -32,7 +32,7 @@ namespace DevHive.Web.Configurations.Extensions cloudName: configuration.GetSection("Cloud").GetSection("cloudName").Value, apiKey: configuration.GetSection("Cloud").GetSection("apiKey").Value, apiSecret: configuration.GetSection("Cloud").GetSection("apiSecret").Value)); - services.AddTransient(); + services.AddTransient(); } } } diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs index a29e06c..1b43b2b 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -8,9 +8,9 @@ namespace DevHive.Web.Configurations.Mapping { public RatingMappings() { - CreateMap(); + CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/Web/DevHive.Web/Controllers/RateController.cs b/src/Web/DevHive.Web/Controllers/RateController.cs index 72eb932..7f8a95f 100644 --- a/src/Web/DevHive.Web/Controllers/RateController.cs +++ b/src/Web/DevHive.Web/Controllers/RateController.cs @@ -13,11 +13,11 @@ namespace DevHive.Web.Controllers [Route("api/[controller]")] public class RateController { - private readonly IRateService _rateService; + private readonly IRatingService _rateService; private readonly IUserService _userService; private readonly IMapper _mapper; - public RateController(IRateService rateService, IUserService userService, IMapper mapper) + public RateController(IRatingService rateService, IUserService userService, IMapper mapper) { this._rateService = rateService; this._userService = userService; @@ -28,10 +28,10 @@ namespace DevHive.Web.Controllers [Authorize(Roles = "Admin,User")] public async Task RatePost(Guid userId, [FromBody] RatePostWebModel ratePostWebModel, [FromHeader] string authorization) { - RatePostServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); + CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); ratePostServiceModel.UserId = userId; - ReadPostRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); + ReadRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map(readPostRatingServiceModel); return new OkObjectResult(readPostRatingWebModel); -- cgit v1.2.3