diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-26 23:00:50 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-02-26 23:00:50 +0200 |
| commit | 379eda6a42fdba0a6ed7e7ae53e0fbf2acd774b6 (patch) | |
| tree | cee90836aaa0d4f540a40b32a589a783eb71fdb8 /src | |
| parent | 8e0038628e866ac5ce716e7971e150d2a8f23f4c (diff) | |
| download | DevHive-379eda6a42fdba0a6ed7e7ae53e0fbf2acd774b6.tar DevHive-379eda6a42fdba0a6ed7e7ae53e0fbf2acd774b6.tar.gz DevHive-379eda6a42fdba0a6ed7e7ae53e0fbf2acd774b6.zip | |
Adding update layer for rating_system
Diffstat (limited to 'src')
11 files changed, 88 insertions, 25 deletions
diff --git a/src/Data/DevHive.Data.Models/Post.cs b/src/Data/DevHive.Data.Models/Post.cs index 716248f..c95a8f1 100644 --- a/src/Data/DevHive.Data.Models/Post.cs +++ b/src/Data/DevHive.Data.Models/Post.cs @@ -21,8 +21,6 @@ namespace DevHive.Data.Models public List<Rating> Ratings { get; set; } - public int CurrentRating { get; set; } - public List<PostAttachments> Attachments { get; set; } = new(); } } diff --git a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs index 4cc34c8..db37d00 100644 --- a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs @@ -11,5 +11,7 @@ namespace DevHive.Data.Interfaces Task<List<Rating>> GetRatingsByPostId(Guid postId); Task<bool> UserRatedPost(Guid userId, Guid postId); Task<Rating> GetRatingByUserAndPostId(Guid userId, Guid postId); + + Task<bool> DoesRatingExist(Guid id); } } diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index 02f92c0..9bb2368 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -32,19 +32,24 @@ namespace DevHive.Data.Repositories return await this._context.Rating .Where(x => x.Post.Id == postId).ToListAsync(); } - public async Task<bool> UserRatedPost(Guid userId, Guid postId) { - return await this._context.UserRate + return await this._context.Rating .Where(x => x.Post.Id == postId) .AnyAsync(x => x.User.Id == userId); } - public async Task<Rating> GetRatingByUserAndPostId(Guid userId, Guid postId) { return await this._context.Rating .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId); } + + public async Task<bool> DoesRatingExist(Guid id) + { + return await this._context.Rating + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } } } diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs index f6233f9..1ea8d8f 100644 --- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs +++ b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs @@ -8,6 +8,8 @@ namespace DevHive.Services.Models.Post.Rating public Guid UserId { get; set; } + public Guid PostId { 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 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<CreateRatingServiceModel, Rating>(); CreateMap<Rating, ReadRatingServiceModel>(); + + CreateMap<UpdateRatingServiceModel, Rating>(); } } } 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<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel); 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); + Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); + + Task<bool> 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<ReadRatingServiceModel> GetUserRateFromPost(Guid userId, Guid postId) + public async Task<bool> 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<ReadRatingServiceModel> 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<ReadRatingServiceModel>(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<bool> 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<ReadRatingServiceModel>(rating); + return readRatingServiceModel; + } + else + return null; } #endregion - public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId) + #region Delete + public async Task<bool> 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 } } diff --git a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs new file mode 100644 index 0000000..07ba0c3 --- /dev/null +++ b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DevHive.Web.Models.Rating +{ + public class UpdateRatingWebModel + { + public Guid PostId { 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 aabee01..23c3eeb 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -11,6 +11,8 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<CreateRatingWebModel, CreateRatingServiceModel>(); CreateMap<ReadRatingServiceModel, ReadRatingWebModel>(); + + CreateMap<UpdateRatingWebModel, UpdateRatingServiceModel>(); } } } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index d2a7801..216dc27 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] + //[Authorize(Roles = "Admin,User")] [Route("api/[controller]")] public class RatingController { @@ -25,7 +26,6 @@ namespace DevHive.Web.Controllers } [HttpPost] - [Authorize(Roles = "Admin,User")] public async Task<IActionResult> RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) { CreateRatingServiceModel ratePostServiceModel = this._mapper.Map<CreateRatingServiceModel>(createRatingWebModel); @@ -36,7 +36,7 @@ namespace DevHive.Web.Controllers if (Guid.Empty == id) return new BadRequestResult(); - return new OkObjectResult(id); + return new OkObjectResult(new { Id = id }); } [HttpGet] @@ -47,5 +47,23 @@ namespace DevHive.Web.Controllers return new OkObjectResult(readPostRatingWebModel); } + + [HttpPut] + public async Task<IActionResult> UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + { + UpdateRatingServiceModel updateRatingServiceModel = + this._mapper.Map<UpdateRatingServiceModel>(updateRatingWebModel); + updateRatingServiceModel.UserId = userId; + + ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); + + if (readRatingServiceModel == null) + return new BadRequestResult(); + else + { + ReadRatingWebModel readRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + return new OkObjectResult(readRatingWebModel); + } + } } } |
