diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-28 21:48:36 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-28 21:48:36 +0200 |
| commit | d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d (patch) | |
| tree | 1ce4dc748a9d66bdcc30558ae7119064f3440e72 | |
| parent | 8d604f9e353cf0b8b8302fc6fb71dd4408c937fe (diff) | |
| download | DevHive-d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d.tar DevHive-d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d.tar.gz DevHive-d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d.zip | |
tryibg to fix rating layers
6 files changed, 42 insertions, 83 deletions
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 3ef7d33..4534511 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -8,7 +8,10 @@ namespace DevHive.Services.Configurations.Mapping { public RatingMappings() { - CreateMap<CreateRatingServiceModel, Rating>(); + CreateMap<CreateRatingServiceModel, Rating>() + .ForMember(dest => dest.User, src => src.Ignore()) + .ForMember(dest => dest.Post, src => src.Ignore()) + .ForMember(dest => dest.Id, src => src.Ignore()); CreateMap<Rating, ReadRatingServiceModel>(); diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index b9ddc2c..beea821 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -10,14 +10,13 @@ namespace DevHive.Services.Interfaces Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel); Task<ReadRatingServiceModel> GetRatingById(Guid ratingId); - Task<bool> HasUserRatedThisPost(Guid userId, Guid postId); + Task<ReadRatingServiceModel> GetRatingByPostAndUser(Guid userId, Guid postId); + Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); Task<bool> DeleteRating(Guid ratingId); - Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData); - - Task<bool> ValidateJwtForRating(Guid commentId, string rawTokenData); + Task<bool> HasUserRatedThisPost(Guid userId, Guid postId); } } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 22eeb99..6ddba1c 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -38,10 +38,8 @@ namespace DevHive.Services.Services Rating rating = this._mapper.Map<Rating>(createRatingServiceModel); - User user = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId); - Post post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId); - rating.User = user; - rating.Post = post; + rating.User = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId); + rating.Post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId); bool success = await this._ratingRepository.AddAsync(rating); @@ -62,19 +60,21 @@ namespace DevHive.Services.Services Rating rating = await this._ratingRepository.GetByIdAsync(ratingId) ?? throw new ArgumentException("The rating does not exist"); - User user = await this._userRepository.GetByIdAsync(rating.User.Id) ?? - throw new ArgumentException("The user does not exist"); - ReadRatingServiceModel readRatingServiceModel = this._mapper.Map<ReadRatingServiceModel>(rating); - readRatingServiceModel.UserId = user.Id; + readRatingServiceModel.UserId = rating.User.Id; return readRatingServiceModel; } - public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId) + public async Task<ReadRatingServiceModel> GetRatingByPostAndUser(Guid userId, Guid postId) { - return await this._ratingRepository - .UserRatedPost(userId, postId); + Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId) ?? + throw new ArgumentException("The rating does not exist"); + + ReadRatingServiceModel readRatingServiceModel = this._mapper.Map<ReadRatingServiceModel>(rating); + readRatingServiceModel.UserId = rating.User.Id; + + return readRatingServiceModel; } #endregion @@ -116,66 +116,10 @@ namespace DevHive.Services.Services } #endregion - #region Validations - /// <summary> - /// Checks whether the user Id in the token and the given user Id match - /// </summary> - public async Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData) - { - User user = await this.GetUserForValidation(rawTokenData); - - return user.Id == userId; - } - - /// <summary> - /// Checks whether the comment, gotten with the commentId, - /// is made by the user in the token - /// or if the user in the token is an admin - /// </summary> - public async Task<bool> ValidateJwtForRating(Guid commentId, string rawTokenData) - { - Rating rating = await this._ratingRepository.GetByIdAsync(commentId) ?? - throw new ArgumentException("Rating does not exist!"); - User user = await this.GetUserForValidation(rawTokenData); - - //If user made the comment - if (rating.User.Id == user.Id) - return true; - //If user is admin - else if (user.Roles.Any(x => x.Name == Role.AdminRole)) - return true; - else - return false; - } - - /// <summary> - /// Returns the user, via their Id in the token - /// </summary> - private async Task<User> GetUserForValidation(string rawTokenData) - { - JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - - Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); - - User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? - throw new ArgumentException("User does not exist!"); - - return user; - } - - /// <summary> - /// Returns all values from a given claim type - /// </summary> - private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) + public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId) { - List<string> toReturn = new(); - - foreach (var claim in claims) - if (claim.Type == type) - toReturn.Add(claim.Value); - - return toReturn; + return await this._ratingRepository + .UserRatedPost(userId, postId); } - #endregion } } diff --git a/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs index 010e04e..abbb702 100644 --- a/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs @@ -6,6 +6,6 @@ namespace DevHive.Web.Models.Rating { public Guid PostId { get; set; } - public bool IsLiked { get; set; } + public bool IsLike { get; set; } } } diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 6a5799f..a0d0979 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -30,7 +30,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient<IPostService, PostService>(); services.AddTransient<ICommentService, CommentService>(); services.AddTransient<IFeedService, FeedService>(); - services.AddTransient<IRateService, RateService>(); + services.AddTransient<IRatingService, RatingService>(); services.AddTransient<ICloudService, CloudinaryService>(options => new CloudinaryService( diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 344acb2..5716b85 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; using DevHive.Services.Models.Post.Rating; using DevHive.Web.Models.Rating; @@ -17,18 +18,20 @@ namespace DevHive.Web.Controllers private readonly IRatingService _rateService; private readonly IUserService _userService; private readonly IMapper _mapper; + private readonly IJwtService _jwtService; - public RatingController(IRatingService rateService, IUserService userService, IMapper mapper) + public RatingController(IRatingService rateService, IUserService userService, IMapper mapper, IJwtService jwtService) { this._rateService = rateService; this._userService = userService; this._mapper = mapper; + this._jwtService = jwtService; } [HttpPost] public async Task<IActionResult> RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForCreating(userId, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); CreateRatingServiceModel ratePostServiceModel = this._mapper.Map<CreateRatingServiceModel>(createRatingWebModel); @@ -51,10 +54,20 @@ namespace DevHive.Web.Controllers return new OkObjectResult(readPostRatingWebModel); } + [HttpGet] + [Route("GetByUserAndPost")] + public async Task<IActionResult> GetRatingByUserAndPost(Guid userId, Guid postId) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingByPostAndUser(userId, postId); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + [HttpPut] public async Task<IActionResult> UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForRating(updateRatingWebModel.Id, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); UpdateRatingServiceModel updateRatingServiceModel = @@ -73,12 +86,12 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task<IActionResult> DeleteTating(Guid id, [FromHeader] string authorization) + public async Task<IActionResult> DeleteTating(Guid userId, Guid ratingId, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForRating(id, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); - return await this._rateService.DeleteRating(id) ? + return await this._rateService.DeleteRating(ratingId) ? new OkResult() : new BadRequestObjectResult("Could not delete Rating"); } |
