From 42f4506395d6b9e7593253856a21b2b536e18df5 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 25 Feb 2021 22:49:00 +0200 Subject: Adding RatePost functionality --- .../DevHive.Web/Controllers/RatingController.cs | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/Web/DevHive.Web/Controllers/RatingController.cs (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs new file mode 100644 index 0000000..673aee1 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -0,0 +1,42 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Rating; +using DevHive.Web.Models.Rating; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class RatingController + { + private readonly IRatingService _rateService; + private readonly IUserService _userService; + private readonly IMapper _mapper; + + public RatingController(IRatingService rateService, IUserService userService, IMapper mapper) + { + this._rateService = rateService; + this._userService = userService; + this._mapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin,User")] + public async Task RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) + { + CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(createRatingWebModel); + ratePostServiceModel.UserId = userId; + + Guid id = await this._rateService.RatePost(ratePostServiceModel); + + if (Guid.Empty == id) + return new BadRequestResult(); + + return new OkObjectResult(id); + } + } +} -- cgit v1.2.3 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 (limited to 'src/Web/DevHive.Web/Controllers/RatingController.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 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 --- src/Data/DevHive.Data.Models/Post.cs | 2 - .../DevHive.Data/Interfaces/IRatingRepository.cs | 2 + .../DevHive.Data/Repositories/RatingRepository.cs | 11 +++-- .../Post/Rating/UpdateRatingServiceModel.cs | 2 + .../Configurations/Mapping/RatingMappings.cs | 2 + .../DevHive.Services/Interfaces/IRatingService.cs | 5 ++- .../DevHive.Services/Services/PostService.cs | 1 - .../DevHive.Services/Services/RatingService.cs | 49 +++++++++++++++------- .../Rating/UpdateRatingWebModel.cs | 15 +++++++ .../Configurations/Mapping/RatingMappings.cs | 2 + .../DevHive.Web/Controllers/RatingController.cs | 22 +++++++++- 11 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') 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 Ratings { get; set; } - public int CurrentRating { get; set; } - public List 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> GetRatingsByPostId(Guid postId); Task UserRatedPost(Guid userId, Guid postId); Task GetRatingByUserAndPostId(Guid userId, Guid postId); + + Task 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 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 GetRatingByUserAndPostId(Guid userId, Guid postId) { return await this._context.Rating .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId); } + + public async Task 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(); 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 } } 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(); CreateMap(); + + CreateMap(); } } } 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 RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) { CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(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 UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + { + UpdateRatingServiceModel updateRatingServiceModel = + this._mapper.Map(updateRatingWebModel); + updateRatingServiceModel.UserId = userId; + + ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); + + if (readRatingServiceModel == null) + return new BadRequestResult(); + else + { + ReadRatingWebModel readRatingWebModel = this._mapper.Map(readRatingServiceModel); + return new OkObjectResult(readRatingWebModel); + } + } } } -- cgit v1.2.3 From 6089bc2e3b14155c334c9fed057383b9ca6e4df6 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sat, 27 Feb 2021 21:53:14 +0200 Subject: Adding user validation for rating layer --- .../DevHive.Services/Interfaces/IRatingService.cs | 4 ++ .../DevHive.Services/Services/RatingService.cs | 67 +++++++++++++++++++++- .../Rating/UpdateRatingWebModel.cs | 2 + .../DevHive.Web/Controllers/RatingController.cs | 6 ++ 4 files changed, 78 insertions(+), 1 deletion(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index 601b07e..b9ddc2c 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -15,5 +15,9 @@ namespace DevHive.Services.Interfaces Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); Task DeleteRating(Guid ratingId); + + Task ValidateJwtForCreating(Guid userId, string rawTokenData); + + Task ValidateJwtForRating(Guid commentId, string rawTokenData); } } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 20080ea..22eeb99 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Interfaces; @@ -78,7 +81,7 @@ namespace DevHive.Services.Services #region Update public async Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel) { - Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId) ?? + Rating rating = await this._ratingRepository.GetByIdAsync(updateRatingServiceModel.Id) ?? throw new ArgumentException("Rating does not exist!"); User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ?? @@ -112,5 +115,67 @@ namespace DevHive.Services.Services return await this._ratingRepository.DeleteAsync(rating); } #endregion + + #region Validations + /// + /// Checks whether the user Id in the token and the given user Id match + /// + public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) + { + User user = await this.GetUserForValidation(rawTokenData); + + return user.Id == userId; + } + + /// + /// Checks whether the comment, gotten with the commentId, + /// is made by the user in the token + /// or if the user in the token is an admin + /// + public async Task ValidateJwtForRating(Guid commentId, string rawTokenData) + { + Rating rating = await this._ratingRepository.GetByIdAsync(commentId) ?? + throw new ArgumentException("Rating does not exist!"); + User user = await this.GetUserForValidation(rawTokenData); + + //If user made the comment + if (rating.User.Id == user.Id) + return true; + //If user is admin + else if (user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; + } + + /// + /// Returns the user, via their Id in the token + /// + private async Task GetUserForValidation(string rawTokenData) + { + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); + + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); + + return user; + } + + /// + /// Returns all values from a given claim type + /// + private List GetClaimTypeValues(string type, IEnumerable claims) + { + List toReturn = new(); + + foreach (var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + #endregion } } diff --git a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs index 07ba0c3..425c3e1 100644 --- a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs @@ -8,6 +8,8 @@ namespace DevHive.Web.Models.Rating { public class UpdateRatingWebModel { + public Guid Id { get; set; } + public Guid PostId { get; set; } public bool IsLike { get; set; } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 216dc27..33e6992 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -28,6 +28,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) { + if (!await this._rateService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(createRatingWebModel); ratePostServiceModel.UserId = userId; @@ -51,6 +54,9 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { + if (!await this._rateService.ValidateJwtForRating(updateRatingWebModel.Id, authorization)) + return new UnauthorizedResult(); + UpdateRatingServiceModel updateRatingServiceModel = this._mapper.Map(updateRatingWebModel); updateRatingServiceModel.UserId = userId; -- cgit v1.2.3 From d53d67776f0746cc6eb8973f7c55767fcf82df65 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sat, 27 Feb 2021 22:13:47 +0200 Subject: adding delete rating functionality --- src/Web/DevHive.Web/Controllers/CommentController.cs | 1 - src/Web/DevHive.Web/Controllers/RatingController.cs | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs index c38e300..7273dda 100644 --- a/src/Web/DevHive.Web/Controllers/CommentController.cs +++ b/src/Web/DevHive.Web/Controllers/CommentController.cs @@ -77,7 +77,6 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestObjectResult("Could not delete Comment"); } - } } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 33e6992..344acb2 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -71,5 +71,16 @@ namespace DevHive.Web.Controllers return new OkObjectResult(readRatingWebModel); } } + + [HttpDelete] + public async Task DeleteTating(Guid id, [FromHeader] string authorization) + { + if (!await this._rateService.ValidateJwtForRating(id, authorization)) + return new UnauthorizedResult(); + + return await this._rateService.DeleteRating(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Rating"); + } } } -- cgit v1.2.3 From d0b2f33af25f6da7ceb85e836bc1e1f7bea8bb4d Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sun, 28 Feb 2021 21:48:36 +0200 Subject: tryibg to fix rating layers --- .../Configurations/Mapping/RatingMappings.cs | 5 +- .../DevHive.Services/Interfaces/IRatingService.cs | 7 +- .../DevHive.Services/Services/RatingService.cs | 84 ++++------------------ .../Rating/CreateRatingWebModel.cs | 2 +- .../Extensions/ConfigureDependencyInjection.cs | 2 +- .../DevHive.Web/Controllers/RatingController.cs | 25 +++++-- 6 files changed, 42 insertions(+), 83 deletions(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 3ef7d33..4534511 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -8,7 +8,10 @@ namespace DevHive.Services.Configurations.Mapping { public RatingMappings() { - CreateMap(); + CreateMap() + .ForMember(dest => dest.User, src => src.Ignore()) + .ForMember(dest => dest.Post, src => src.Ignore()) + .ForMember(dest => dest.Id, src => src.Ignore()); CreateMap(); diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index b9ddc2c..beea821 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -10,14 +10,13 @@ namespace DevHive.Services.Interfaces Task RatePost(CreateRatingServiceModel createRatingServiceModel); Task GetRatingById(Guid ratingId); - Task HasUserRatedThisPost(Guid userId, Guid postId); + Task GetRatingByPostAndUser(Guid userId, Guid postId); + Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); Task DeleteRating(Guid ratingId); - Task ValidateJwtForCreating(Guid userId, string rawTokenData); - - Task ValidateJwtForRating(Guid commentId, string rawTokenData); + Task HasUserRatedThisPost(Guid userId, Guid postId); } } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 22eeb99..6ddba1c 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -38,10 +38,8 @@ namespace DevHive.Services.Services Rating rating = this._mapper.Map(createRatingServiceModel); - User user = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId); - Post post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId); - rating.User = user; - rating.Post = post; + rating.User = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId); + rating.Post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId); bool success = await this._ratingRepository.AddAsync(rating); @@ -62,19 +60,21 @@ namespace DevHive.Services.Services 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; + readRatingServiceModel.UserId = rating.User.Id; return readRatingServiceModel; } - public async Task HasUserRatedThisPost(Guid userId, Guid postId) + public async Task GetRatingByPostAndUser(Guid userId, Guid postId) { - return await this._ratingRepository - .UserRatedPost(userId, postId); + Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId) ?? + throw new ArgumentException("The rating does not exist"); + + ReadRatingServiceModel readRatingServiceModel = this._mapper.Map(rating); + readRatingServiceModel.UserId = rating.User.Id; + + return readRatingServiceModel; } #endregion @@ -116,66 +116,10 @@ namespace DevHive.Services.Services } #endregion - #region Validations - /// - /// Checks whether the user Id in the token and the given user Id match - /// - public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) - { - User user = await this.GetUserForValidation(rawTokenData); - - return user.Id == userId; - } - - /// - /// Checks whether the comment, gotten with the commentId, - /// is made by the user in the token - /// or if the user in the token is an admin - /// - public async Task ValidateJwtForRating(Guid commentId, string rawTokenData) - { - Rating rating = await this._ratingRepository.GetByIdAsync(commentId) ?? - throw new ArgumentException("Rating does not exist!"); - User user = await this.GetUserForValidation(rawTokenData); - - //If user made the comment - if (rating.User.Id == user.Id) - return true; - //If user is admin - else if (user.Roles.Any(x => x.Name == Role.AdminRole)) - return true; - else - return false; - } - - /// - /// Returns the user, via their Id in the token - /// - private async Task GetUserForValidation(string rawTokenData) - { - JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - - Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); - - User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? - throw new ArgumentException("User does not exist!"); - - return user; - } - - /// - /// Returns all values from a given claim type - /// - private List GetClaimTypeValues(string type, IEnumerable claims) + public async Task HasUserRatedThisPost(Guid userId, Guid postId) { - List toReturn = new(); - - foreach (var claim in claims) - if (claim.Type == type) - toReturn.Add(claim.Value); - - return toReturn; + return await this._ratingRepository + .UserRatedPost(userId, postId); } - #endregion } } diff --git a/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs index 010e04e..abbb702 100644 --- a/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/CreateRatingWebModel.cs @@ -6,6 +6,6 @@ namespace DevHive.Web.Models.Rating { public Guid PostId { get; set; } - public bool IsLiked { get; set; } + public bool IsLike { get; set; } } } diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 6a5799f..a0d0979 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -30,7 +30,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(options => new CloudinaryService( diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 344acb2..5716b85 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; using DevHive.Services.Models.Post.Rating; using DevHive.Web.Models.Rating; @@ -17,18 +18,20 @@ namespace DevHive.Web.Controllers private readonly IRatingService _rateService; private readonly IUserService _userService; private readonly IMapper _mapper; + private readonly IJwtService _jwtService; - public RatingController(IRatingService rateService, IUserService userService, IMapper mapper) + public RatingController(IRatingService rateService, IUserService userService, IMapper mapper, IJwtService jwtService) { this._rateService = rateService; this._userService = userService; this._mapper = mapper; + this._jwtService = jwtService; } [HttpPost] public async Task RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForCreating(userId, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); CreateRatingServiceModel ratePostServiceModel = this._mapper.Map(createRatingWebModel); @@ -51,10 +54,20 @@ namespace DevHive.Web.Controllers return new OkObjectResult(readPostRatingWebModel); } + [HttpGet] + [Route("GetByUserAndPost")] + public async Task GetRatingByUserAndPost(Guid userId, Guid postId) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingByPostAndUser(userId, postId); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + [HttpPut] public async Task UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForRating(updateRatingWebModel.Id, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); UpdateRatingServiceModel updateRatingServiceModel = @@ -73,12 +86,12 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task DeleteTating(Guid id, [FromHeader] string authorization) + public async Task DeleteTating(Guid userId, Guid ratingId, [FromHeader] string authorization) { - if (!await this._rateService.ValidateJwtForRating(id, authorization)) + if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); - return await this._rateService.DeleteRating(id) ? + return await this._rateService.DeleteRating(ratingId) ? new OkResult() : new BadRequestObjectResult("Could not delete Rating"); } -- cgit v1.2.3 From f986ca67edd425c32eaec5a20fecdc5786f9d8e3 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:27:26 +0200 Subject: Fixing bugs in rating layer --- src/Data/DevHive.Data/Repositories/RatingRepository.cs | 4 ++++ src/Services/DevHive.Services/Interfaces/IRatingService.cs | 2 -- src/Services/DevHive.Services/Services/RatingService.cs | 8 +------- src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs | 4 ---- src/Web/DevHive.Web/Controllers/RatingController.cs | 3 ++- 5 files changed, 7 insertions(+), 14 deletions(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index 9bb2368..1784144 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -30,6 +30,8 @@ namespace DevHive.Data.Repositories public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating + .Include(x => x.User) + .Include(x => x.Post) .Where(x => x.Post.Id == postId).ToListAsync(); } public async Task UserRatedPost(Guid userId, Guid postId) @@ -41,6 +43,8 @@ namespace DevHive.Data.Repositories public async Task GetRatingByUserAndPostId(Guid userId, Guid postId) { return await this._context.Rating + .Include(x => x.User) + .Include(x => x.Post) .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId); } diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index beea821..b262f45 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -16,7 +16,5 @@ namespace DevHive.Services.Interfaces Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel); Task DeleteRating(Guid ratingId); - - Task HasUserRatedThisPost(Guid userId, Guid postId); } } diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 6ddba1c..6d0d0b9 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -81,7 +81,7 @@ namespace DevHive.Services.Services #region Update public async Task UpdateRating(UpdateRatingServiceModel updateRatingServiceModel) { - Rating rating = await this._ratingRepository.GetByIdAsync(updateRatingServiceModel.Id) ?? + Rating rating = await this._ratingRepository.GetRatingByUserAndPostId(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId) ?? throw new ArgumentException("Rating does not exist!"); User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ?? @@ -115,11 +115,5 @@ namespace DevHive.Services.Services return await this._ratingRepository.DeleteAsync(rating); } #endregion - - public async Task HasUserRatedThisPost(Guid userId, Guid postId) - { - return await this._ratingRepository - .UserRatedPost(userId, postId); - } } } diff --git a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs index 425c3e1..176c099 100644 --- a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs @@ -8,10 +8,6 @@ namespace DevHive.Web.Models.Rating { public class UpdateRatingWebModel { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - public bool IsLike { get; set; } } } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 5716b85..8d3ac92 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -65,7 +65,7 @@ namespace DevHive.Web.Controllers } [HttpPut] - public async Task UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + public async Task UpdateRating(Guid userId, Guid postId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); @@ -73,6 +73,7 @@ namespace DevHive.Web.Controllers UpdateRatingServiceModel updateRatingServiceModel = this._mapper.Map(updateRatingWebModel); updateRatingServiceModel.UserId = userId; + updateRatingServiceModel.PostId = postId; ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); -- cgit v1.2.3 From 4612913c72c3d2b4d3e28629df9f2ed97f0c29a9 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:55:28 +0200 Subject: Moved Rating service models outside of Post folder --- .../Post/Rating/CreateRatingServiceModel.cs | 13 ------------- .../Post/Rating/ReadRatingServiceModel.cs | 15 --------------- .../Post/Rating/UpdateRatingServiceModel.cs | 15 --------------- .../Rating/CreateRatingServiceModel.cs | 13 +++++++++++++ .../Rating/ReadRatingServiceModel.cs | 15 +++++++++++++++ .../Rating/UpdateRatingServiceModel.cs | 15 +++++++++++++++ .../Configurations/Mapping/RatingMappings.cs | 2 +- .../DevHive.Services/Interfaces/IRatingService.cs | 2 +- src/Services/DevHive.Services/Services/RatingService.cs | 2 +- .../DevHive.Web/Configurations/Mapping/RatingMappings.cs | 2 +- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 11 files changed, 48 insertions(+), 48 deletions(-) delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs deleted file mode 100644 index aaeab73..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class CreateRatingServiceModel - { - public Guid UserId { get; set; } - - public Guid PostId { get; set; } - - public bool IsLike { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs deleted file mode 100644 index 86b4957..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class ReadRatingServiceModel - { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - - public Guid UserId { 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 deleted file mode 100644 index 1ea8d8f..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class UpdateRatingServiceModel - { - public Guid Id { get; set; } - - public Guid UserId { get; set; } - - public Guid PostId { get; set; } - - public bool IsLike { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs new file mode 100644 index 0000000..486e3d2 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class CreateRatingServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..56a5ab0 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class ReadRatingServiceModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public Guid UserId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs new file mode 100644 index 0000000..923e789 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class UpdateRatingServiceModel + { + public Guid Id { get; set; } + + 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 4534511..9ad5f25 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index b262f45..be33300 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Interfaces { diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 6d0d0b9..1f77a6e 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -8,7 +8,7 @@ using AutoMapper; using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Interfaces; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Services { diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs index 23c3eeb..1d731d8 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -1,5 +1,5 @@ using AutoMapper; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; using DevHive.Web.Models.Rating; namespace DevHive.Web.Configurations.Mapping diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 8d3ac92..c93a56d 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; using DevHive.Web.Models.Rating; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -- cgit v1.2.3 From 95693f70a2314eab65ac289580d264fe12c42a06 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:56:48 +0200 Subject: Adding authorization to Rating Controller --- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index c93a56d..8c44243 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -11,7 +11,7 @@ using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] - //[Authorize(Roles = "Admin,User")] + [Authorize(Roles = "Admin,User")] [Route("api/[controller]")] public class RatingController { -- cgit v1.2.3 From 6cc1bceebfa1ed01472303db226c000c2345b016 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:57:49 +0200 Subject: Removed _userService from RatingController --- src/Web/DevHive.Web/Controllers/RatingController.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 8c44243..f6ab0c5 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -16,14 +16,12 @@ namespace DevHive.Web.Controllers public class RatingController { private readonly IRatingService _rateService; - private readonly IUserService _userService; private readonly IMapper _mapper; private readonly IJwtService _jwtService; - public RatingController(IRatingService rateService, IUserService userService, IMapper mapper, IJwtService jwtService) + public RatingController(IRatingService rateService, IMapper mapper, IJwtService jwtService) { this._rateService = rateService; - this._userService = userService; this._mapper = mapper; this._jwtService = jwtService; } -- cgit v1.2.3 From b3fd74a4207334b7b31e9450a078bd10437648c3 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 13 Mar 2021 08:46:31 +0200 Subject: Fixed type in delete rating method name in rating controller --- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Web/DevHive.Web/Controllers/RatingController.cs') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index f6ab0c5..7d21795 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -85,7 +85,7 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task DeleteTating(Guid userId, Guid ratingId, [FromHeader] string authorization) + public async Task DeleteRating(Guid userId, Guid ratingId, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); -- cgit v1.2.3