From d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sun, 28 Feb 2021 21:48:36 +0200 Subject: tryibg to fix rating layers --- .../Configurations/Mapping/RatingMappings.cs | 5 +- .../DevHive.Services/Interfaces/IRatingService.cs | 7 +- .../DevHive.Services/Services/RatingService.cs | 84 ++++------------------ 3 files changed, 21 insertions(+), 75 deletions(-) (limited to 'src/Services') 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(); + CreateMap() + .ForMember(dest => dest.User, src => src.Ignore()) + .ForMember(dest => dest.Post, src => src.Ignore()) + .ForMember(dest => dest.Id, src => src.Ignore()); CreateMap(); 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 RatePost(CreateRatingServiceModel createRatingServiceModel); Task GetRatingById(Guid ratingId); - Task HasUserRatedThisPost(Guid userId, Guid postId); + Task GetRatingByPostAndUser(Guid userId, Guid postId); + Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); Task DeleteRating(Guid ratingId); - Task ValidateJwtForCreating(Guid userId, string rawTokenData); - - Task ValidateJwtForRating(Guid commentId, string rawTokenData); + Task 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(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(rating); - readRatingServiceModel.UserId = user.Id; + readRatingServiceModel.UserId = rating.User.Id; return readRatingServiceModel; } - public async Task HasUserRatedThisPost(Guid userId, Guid postId) + public async Task 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(rating); + readRatingServiceModel.UserId = rating.User.Id; + + return readRatingServiceModel; } #endregion @@ -116,66 +116,10 @@ namespace DevHive.Services.Services } #endregion - #region Validations - /// - /// Checks whether the user Id in the token and the given user Id match - /// - public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) - { - User user = await this.GetUserForValidation(rawTokenData); - - return user.Id == userId; - } - - /// - /// 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 - /// - public async Task 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; - } - - /// - /// Returns the user, via their Id in the token - /// - private async Task 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; - } - - /// - /// Returns all values from a given claim type - /// - private List GetClaimTypeValues(string type, IEnumerable claims) + public async Task HasUserRatedThisPost(Guid userId, Guid postId) { - List 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 } } -- cgit v1.2.3