From 8e0038628e866ac5ce716e7971e150d2a8f23f4c Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 26 Feb 2021 09:25:24 +0200 Subject: Added GetById for rating --- .../DevHive.Data/Repositories/RatingRepository.cs | 7 ++++ .../Post/Rating/ReadRatingServiceModel.cs | 4 +- .../Post/Rating/UpdateRatingServiceModel.cs | 4 -- .../Configurations/Mapping/RatingMappings.cs | 2 + .../DevHive.Services/Interfaces/IRatingService.cs | 8 +++- .../DevHive.Services/Services/RatingService.cs | 45 +++++++++++++++++----- .../Rating/ReadPostRatingWebModel.cs | 15 -------- .../Rating/ReadRatingWebModel.cs | 15 ++++++++ .../Configurations/Mapping/RatingMappings.cs | 2 +- .../DevHive.Web/Controllers/RatingController.cs | 9 +++++ 10 files changed, 77 insertions(+), 34 deletions(-) delete mode 100644 src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs create mode 100644 src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index 4db208e..02f92c0 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -20,6 +20,13 @@ namespace DevHive.Data.Repositories this._postRepository = postRepository; } + public override async Task GetByIdAsync(Guid id) + { + return await this._context.Rating + .Include(x => x.User) + .Include(x => x.Post) + .FirstOrDefaultAsync(x => x.Id == id); + } public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs index dbc7ecc..86b4957 100644 --- a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs +++ b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs @@ -8,8 +8,8 @@ namespace DevHive.Services.Models.Post.Rating public Guid PostId { get; set; } - public int Likes { get; set; } + public Guid UserId { get; set; } - public int Dislikes { get; set; } + public bool IsLike { get; set; } } } diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs index 8710751..f6233f9 100644 --- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs +++ b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace DevHive.Services.Models.Post.Rating { 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(); + + CreateMap(); } } } 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 RatePost(CreateRatingServiceModel ratePostServiceModel); + Task RatePost(CreateRatingServiceModel createRatingServiceModel); - bool HasUserRatedThisPost(User user, Post post); + Task GetRatingById(Guid ratingId); + Task GetUserRateFromPost(Guid userId, Guid postId); + Task HasUserRatedThisPost(Guid userId, Guid postId); + + Task 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 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 RemoveUserRateFromPost(Guid userId, Guid postId) + #region Read + public async Task 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(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 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(rating); + readRatingServiceModel.UserId = user.Id; + + return readRatingServiceModel; } + public async Task HasUserRatedThisPost(Guid userId, Guid postId) + { + return await this._ratingRepository + .UserRatedPost(userId, postId); + } + #endregion + + public async Task RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + } } } diff --git a/src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs deleted file mode 100644 index 8afd57e..0000000 --- a/src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Rating -{ - public class ReadPostRatingWebModel - { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - - public int Likes { get; set; } - - public int Dislikes { get; set; } - } -} diff --git a/src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs new file mode 100644 index 0000000..40f4c6f --- /dev/null +++ b/src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Web.Models.Rating +{ + public class ReadRatingWebModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public Guid UserId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs index c8b7353..aabee01 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -10,7 +10,7 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 673aee1..d2a7801 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -38,5 +38,14 @@ namespace DevHive.Web.Controllers return new OkObjectResult(id); } + + [HttpGet] + public async Task GetRatingById(Guid id) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingById(id); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } } } -- cgit v1.2.3