diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-25 22:49:00 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-25 22:49:00 +0200 |
| commit | 42f4506395d6b9e7593253856a21b2b536e18df5 (patch) | |
| tree | a6aaba71407593348878e170dbff5c52a311320c /src/Services | |
| parent | a851adfac47a26cae83e9161d37902a219e5ebf3 (diff) | |
| download | DevHive-42f4506395d6b9e7593253856a21b2b536e18df5.tar DevHive-42f4506395d6b9e7593253856a21b2b536e18df5.tar.gz DevHive-42f4506395d6b9e7593253856a21b2b536e18df5.zip | |
Adding RatePost functionality
Diffstat (limited to 'src/Services')
4 files changed, 37 insertions, 27 deletions
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs new file mode 100644 index 0000000..8710751 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DevHive.Services.Models.Post.Rating +{ + public class UpdateRatingServiceModel + { + public Guid Id { get; set; } + + public Guid UserId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index fefa6d8..6d21de0 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -8,6 +8,7 @@ namespace DevHive.Services.Configurations.Mapping { public RatingMappings() { + CreateMap<CreateRatingServiceModel, Rating>(); } } } diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index adb4313..5dea04f 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -7,7 +7,7 @@ namespace DevHive.Services.Interfaces { public interface IRatingService { - Task<ReadRatingServiceModel> RatePost(CreateRatingServiceModel ratePostServiceModel); + Task<Guid> RatePost(CreateRatingServiceModel ratePostServiceModel); bool HasUserRatedThisPost(User user, Post post); } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 45ff7c0..a4d04b1 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -24,29 +24,31 @@ namespace DevHive.Services.Services this._mapper = mapper; } - public async Task<ReadRatingServiceModel> RatePost(CreateRatingServiceModel ratePostServiceModel) + public async Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel) { - throw new NotImplementedException(); - // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) - // throw new ArgumentException("Post does not exist!"); + if (!await this._postRepository.DoesPostExist(createRatingServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); - // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) - // throw new ArgumentException("User does not exist!"); + if (await this._ratingRepository.UserRatedPost(createRatingServiceModel.UserId, createRatingServiceModel.PostId)) + throw new ArgumentException("User already rated the post!"); - // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); - // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId); + Rating rating = this._mapper.Map<Rating>(createRatingServiceModel); - // if (this.HasUserRatedThisPost(user, post)) - // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); + User user = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId); + Post post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId); + rating.User = user; + rating.Post = post; - // this.Rate(user, post, ratePostServiceModel.Liked); + bool success = await this._ratingRepository.AddAsync(rating); - // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating); - // if (!success) - // throw new InvalidOperationException("Unable to rate the post!"); + if (success) + { + Rating newRating = await this._ratingRepository.GetRatingByUserAndPostId(rating.User.Id, rating.Post.Id); - // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id); - // return this._mapper.Map<ReadPostRatingServiceModel>(newRating); + return newRating.Id; + } + else + return Guid.Empty; } public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId) @@ -66,15 +68,5 @@ namespace DevHive.Services.Services // .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); - } } } |
