From 379eda6a42fdba0a6ed7e7ae53e0fbf2acd774b6 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 26 Feb 2021 23:00:50 +0200 Subject: Adding update layer for rating_system --- .../Configurations/Mapping/RatingMappings.cs | 2 + .../DevHive.Services/Interfaces/IRatingService.cs | 5 ++- .../DevHive.Services/Services/PostService.cs | 1 - .../DevHive.Services/Services/RatingService.cs | 49 +++++++++++++++------- 4 files changed, 39 insertions(+), 18 deletions(-) (limited to 'src/Services/DevHive.Services') diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 7056afa..3ef7d33 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -11,6 +11,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index a554ce3..601b07e 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -10,9 +10,10 @@ namespace DevHive.Services.Interfaces Task RatePost(CreateRatingServiceModel createRatingServiceModel); Task GetRatingById(Guid ratingId); - Task GetUserRateFromPost(Guid userId, Guid postId); Task HasUserRatedThisPost(Guid userId, Guid postId); - Task RemoveUserRateFromPost(Guid userId, Guid postId); + Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); + + Task DeleteRating(Guid ratingId); } } diff --git a/src/Services/DevHive.Services/Services/PostService.cs b/src/Services/DevHive.Services/Services/PostService.cs index 4bece90..a3d5117 100644 --- a/src/Services/DevHive.Services/Services/PostService.cs +++ b/src/Services/DevHive.Services/Services/PostService.cs @@ -46,7 +46,6 @@ 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/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index e8c3c4c..20080ea 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -68,30 +68,49 @@ namespace DevHive.Services.Services return readRatingServiceModel; } - public async Task GetUserRateFromPost(Guid userId, Guid postId) + public async Task HasUserRatedThisPost(Guid userId, Guid postId) { - Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId) ?? - throw new ArgumentException("The rating does not exist"); + return await this._ratingRepository + .UserRatedPost(userId, postId); + } + #endregion - User user = await this._userRepository.GetByIdAsync(rating.User.Id) ?? - throw new ArgumentException("The user does not exist"); + #region Update + public async Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel) + { + Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId) ?? + throw new ArgumentException("Rating does not exist!"); - ReadRatingServiceModel readRatingServiceModel = this._mapper.Map(rating); - readRatingServiceModel.UserId = user.Id; + User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ?? + throw new ArgumentException("User does not exist!"); - return readRatingServiceModel; - } + if (!await this._ratingRepository.UserRatedPost(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId)) + throw new ArgumentException("User has not rated the post!"); - public async Task HasUserRatedThisPost(Guid userId, Guid postId) - { - return await this._ratingRepository - .UserRatedPost(userId, postId); + rating.User = user; + rating.IsLike = updateRatingServiceModel.IsLike; + + bool result = await this._ratingRepository.EditAsync(updateRatingServiceModel.Id, rating); + + if (result) + { + ReadRatingServiceModel readRatingServiceModel = this._mapper.Map(rating); + return readRatingServiceModel; + } + else + return null; } #endregion - public async Task RemoveUserRateFromPost(Guid userId, Guid postId) + #region Delete + public async Task DeleteRating(Guid ratingId) { - throw new NotImplementedException(); + if (!await this._ratingRepository.DoesRatingExist(ratingId)) + throw new ArgumentException("Rating does not exist!"); + + Rating rating = await this._ratingRepository.GetByIdAsync(ratingId); + return await this._ratingRepository.DeleteAsync(rating); } + #endregion } } -- cgit v1.2.3