diff options
Diffstat (limited to 'src/Services/DevHive.Services')
3 files changed, 43 insertions, 12 deletions
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 6d21de0..7056afa 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -9,6 +9,8 @@ namespace DevHive.Services.Configurations.Mapping public RatingMappings() { CreateMap<CreateRatingServiceModel, Rating>(); + + CreateMap<Rating, ReadRatingServiceModel>(); } } } diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index 5dea04f..a554ce3 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -7,8 +7,12 @@ namespace DevHive.Services.Interfaces { public interface IRatingService { - Task<Guid> RatePost(CreateRatingServiceModel ratePostServiceModel); + Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel); - bool HasUserRatedThisPost(User user, Post post); + Task<ReadRatingServiceModel> GetRatingById(Guid ratingId); + Task<ReadRatingServiceModel> GetUserRateFromPost(Guid userId, Guid postId); + Task<bool> HasUserRatedThisPost(Guid userId, Guid postId); + + Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId); } } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index a4d04b1..e8c3c4c 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -24,6 +24,7 @@ namespace DevHive.Services.Services this._mapper = mapper; } + #region Create public async Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel) { if (!await this._postRepository.DoesPostExist(createRatingServiceModel.PostId)) @@ -50,23 +51,47 @@ namespace DevHive.Services.Services else return Guid.Empty; } + #endregion - public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId) + #region Read + public async Task<ReadRatingServiceModel> GetRatingById(Guid ratingId) { - throw new NotImplementedException(); - // Post post = await this._postRepository.GetByIdAsync(postId); - // User user = await this._userRepository.GetByIdAsync(userId); + 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; - // if (!this.HasUserRatedThisPost(user, post)) - // throw new ArgumentException("You haven't rated this post, lmao!"); + return readRatingServiceModel; } - public bool HasUserRatedThisPost(User user, Post post) + public async Task<ReadRatingServiceModel> GetUserRateFromPost(Guid userId, Guid postId) { - throw new NotImplementedException(); - // return post.Rating.UsersThatRated - // .Any(x => x.Id == user.Id); + Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId) ?? + 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; + + return readRatingServiceModel; } + public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId) + { + return await this._ratingRepository + .UserRatedPost(userId, postId); + } + #endregion + + public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + } } } |
