diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-26 09:25:24 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-26 09:25:24 +0200 |
| commit | 8e0038628e866ac5ce716e7971e150d2a8f23f4c (patch) | |
| tree | 4f254e27cec831100e479959e58f01ff4ac7045e /src | |
| parent | 42f4506395d6b9e7593253856a21b2b536e18df5 (diff) | |
| download | DevHive-8e0038628e866ac5ce716e7971e150d2a8f23f4c.tar DevHive-8e0038628e866ac5ce716e7971e150d2a8f23f4c.tar.gz DevHive-8e0038628e866ac5ce716e7971e150d2a8f23f4c.zip | |
Added GetById for rating
Diffstat (limited to 'src')
| -rw-r--r-- | src/Data/DevHive.Data/Repositories/RatingRepository.cs | 7 | ||||
| -rw-r--r-- | src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs | 4 | ||||
| -rw-r--r-- | src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs | 4 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs | 2 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Interfaces/IRatingService.cs | 8 | ||||
| -rw-r--r-- | src/Services/DevHive.Services/Services/RatingService.cs | 45 | ||||
| -rw-r--r-- | src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs (renamed from src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs) | 6 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs | 2 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/RatingController.cs | 9 |
9 files changed, 65 insertions, 22 deletions
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<Rating> GetByIdAsync(Guid id) + { + return await this._context.Rating + .Include(x => x.User) + .Include(x => x.Post) + .FirstOrDefaultAsync(x => x.Id == id); + } public async Task<List<Rating>> 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<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(); + } } } diff --git a/src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs index 8afd57e..40f4c6f 100644 --- a/src/Web/DevHive.Web.Models/Rating/ReadPostRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/ReadRatingWebModel.cs @@ -2,14 +2,14 @@ using System; namespace DevHive.Web.Models.Rating { - public class ReadPostRatingWebModel + public class ReadRatingWebModel { public Guid Id { get; set; } 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/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<CreateRatingWebModel, CreateRatingServiceModel>(); - CreateMap<ReadRatingServiceModel, ReadPostRatingWebModel>(); + CreateMap<ReadRatingServiceModel, ReadRatingWebModel>(); } } } 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<IActionResult> GetRatingById(Guid id) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingById(id); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } } } |
