aboutsummaryrefslogtreecommitdiff
path: root/src/Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services')
-rw-r--r--src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs)4
-rw-r--r--src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs (renamed from src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs)6
-rw-r--r--src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs15
-rw-r--r--src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs3
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs5
-rw-r--r--src/Services/DevHive.Services/Interfaces/IRateService.cs14
-rw-r--r--src/Services/DevHive.Services/Interfaces/IRatingService.cs23
-rw-r--r--src/Services/DevHive.Services/Services/RateService.cs80
-rw-r--r--src/Services/DevHive.Services/Services/RatingService.cs181
9 files changed, 231 insertions, 100 deletions
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs
index d4eb7bd..aaeab73 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/RatePostServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs
@@ -2,12 +2,12 @@ using System;
namespace DevHive.Services.Models.Post.Rating
{
- public class RatePostServiceModel
+ public class CreateRatingServiceModel
{
public Guid UserId { get; set; }
public Guid PostId { get; set; }
- public bool Liked { get; set; }
+ public bool IsLike { get; set; }
}
}
diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs
index 8c73aaf..86b4957 100644
--- a/src/Services/DevHive.Services.Models/Post/Rating/ReadPostRatingServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs
@@ -2,14 +2,14 @@ using System;
namespace DevHive.Services.Models.Post.Rating
{
- public class ReadPostRatingServiceModel
+ public class ReadRatingServiceModel
{
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/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs
new file mode 100644
index 0000000..1ea8d8f
--- /dev/null
+++ b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs
@@ -0,0 +1,15 @@
+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/Post/ReadPostServiceModel.cs b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs
index a7aa882..33d6520 100644
--- a/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs
+++ b/src/Services/DevHive.Services.Models/Post/ReadPostServiceModel.cs
@@ -21,6 +21,7 @@ namespace DevHive.Services.Models.Post
public List<IdModel> Comments { get; set; } = new();
public List<string> FileUrls { get; set; }
- // public List<FileContentResult> Files { get; set; } = new();
+
+ public int CurrentRating { get; set; }
}
}
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
index fefa6d8..3ef7d33 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs
@@ -8,6 +8,11 @@ namespace DevHive.Services.Configurations.Mapping
{
public RatingMappings()
{
+ CreateMap<CreateRatingServiceModel, Rating>();
+
+ CreateMap<Rating, ReadRatingServiceModel>();
+
+ CreateMap<UpdateRatingServiceModel, Rating>();
}
}
}
diff --git a/src/Services/DevHive.Services/Interfaces/IRateService.cs b/src/Services/DevHive.Services/Interfaces/IRateService.cs
deleted file mode 100644
index 359ef55..0000000
--- a/src/Services/DevHive.Services/Interfaces/IRateService.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using DevHive.Data.Models;
-using DevHive.Services.Models.Post.Rating;
-
-namespace DevHive.Services.Interfaces
-{
- public interface IRateService
- {
- Task<ReadPostRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel);
-
- bool HasUserRatedThisPost(User user, Post post);
- }
-}
diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
new file mode 100644
index 0000000..b9ddc2c
--- /dev/null
+++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Services.Models.Post.Rating;
+
+namespace DevHive.Services.Interfaces
+{
+ public interface IRatingService
+ {
+ Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel);
+
+ Task<ReadRatingServiceModel> GetRatingById(Guid ratingId);
+ Task<bool> HasUserRatedThisPost(Guid userId, Guid postId);
+
+ Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel);
+
+ Task<bool> DeleteRating(Guid ratingId);
+
+ Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData);
+
+ Task<bool> ValidateJwtForRating(Guid commentId, string rawTokenData);
+ }
+}
diff --git a/src/Services/DevHive.Services/Services/RateService.cs b/src/Services/DevHive.Services/Services/RateService.cs
deleted file mode 100644
index caf4b80..0000000
--- a/src/Services/DevHive.Services/Services/RateService.cs
+++ /dev/null
@@ -1,80 +0,0 @@
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-using AutoMapper;
-using DevHive.Data.Interfaces;
-using DevHive.Data.Models;
-using DevHive.Services.Interfaces;
-using DevHive.Services.Models.Post.Rating;
-
-namespace DevHive.Services.Services
-{
- public class RateService : IRateService
- {
- private readonly IPostRepository _postRepository;
- private readonly IUserRepository _userRepository;
- private readonly IRatingRepository _ratingRepository;
- private readonly IMapper _mapper;
-
- public RateService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper)
- {
- this._postRepository = postRepository;
- this._ratingRepository = ratingRepository;
- this._userRepository = userRepository;
- this._mapper = mapper;
- }
-
- public async Task<ReadPostRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel)
- {
- throw new NotImplementedException();
- // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId))
- // throw new ArgumentException("Post does not exist!");
-
- // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId))
- // throw new ArgumentException("User does not exist!");
-
- // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId);
- // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId);
-
- // if (this.HasUserRatedThisPost(user, post))
- // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)");
-
- // this.Rate(user, post, ratePostServiceModel.Liked);
-
- // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating);
- // if (!success)
- // throw new InvalidOperationException("Unable to rate the post!");
-
- // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id);
- // return this._mapper.Map<ReadPostRatingServiceModel>(newRating);
- }
-
- public async Task<ReadPostRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId)
- {
- throw new NotImplementedException();
- // Post post = await this._postRepository.GetByIdAsync(postId);
- // User user = await this._userRepository.GetByIdAsync(userId);
-
- // if (!this.HasUserRatedThisPost(user, post))
- // throw new ArgumentException("You haven't rated this post, lmao!");
- }
-
- public bool HasUserRatedThisPost(User user, Post post)
- {
- throw new NotImplementedException();
- // return post.Rating.UsersThatRated
- // .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);
- }
- }
-}
diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs
new file mode 100644
index 0000000..22eeb99
--- /dev/null
+++ b/src/Services/DevHive.Services/Services/RatingService.cs
@@ -0,0 +1,181 @@
+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;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post.Rating;
+
+namespace DevHive.Services.Services
+{
+ public class RatingService : IRatingService
+ {
+ private readonly IPostRepository _postRepository;
+ private readonly IUserRepository _userRepository;
+ private readonly IRatingRepository _ratingRepository;
+ private readonly IMapper _mapper;
+
+ public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper)
+ {
+ this._postRepository = postRepository;
+ this._ratingRepository = ratingRepository;
+ this._userRepository = userRepository;
+ this._mapper = mapper;
+ }
+
+ #region Create
+ public async Task<Guid> RatePost(CreateRatingServiceModel createRatingServiceModel)
+ {
+ if (!await this._postRepository.DoesPostExist(createRatingServiceModel.PostId))
+ throw new ArgumentException("Post does not exist!");
+
+ if (await this._ratingRepository.UserRatedPost(createRatingServiceModel.UserId, createRatingServiceModel.PostId))
+ throw new ArgumentException("User already rated the post!");
+
+ Rating rating = this._mapper.Map<Rating>(createRatingServiceModel);
+
+ User user = await this._userRepository.GetByIdAsync(createRatingServiceModel.UserId);
+ Post post = await this._postRepository.GetByIdAsync(createRatingServiceModel.PostId);
+ rating.User = user;
+ rating.Post = post;
+
+ bool success = await this._ratingRepository.AddAsync(rating);
+
+ if (success)
+ {
+ Rating newRating = await this._ratingRepository.GetRatingByUserAndPostId(rating.User.Id, rating.Post.Id);
+
+ return newRating.Id;
+ }
+ else
+ return Guid.Empty;
+ }
+ #endregion
+
+ #region Read
+ public async Task<ReadRatingServiceModel> GetRatingById(Guid ratingId)
+ {
+ 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;
+
+ return readRatingServiceModel;
+ }
+
+ public async Task<bool> HasUserRatedThisPost(Guid userId, Guid postId)
+ {
+ return await this._ratingRepository
+ .UserRatedPost(userId, postId);
+ }
+ #endregion
+
+ #region Update
+ public async Task<ReadRatingServiceModel> UpdateRating(UpdateRatingServiceModel updateRatingServiceModel)
+ {
+ Rating rating = await this._ratingRepository.GetByIdAsync(updateRatingServiceModel.Id) ??
+ throw new ArgumentException("Rating does not exist!");
+
+ User user = await this._userRepository.GetByIdAsync(updateRatingServiceModel.UserId) ??
+ throw new ArgumentException("User does not exist!");
+
+ if (!await this._ratingRepository.UserRatedPost(updateRatingServiceModel.UserId, updateRatingServiceModel.PostId))
+ throw new ArgumentException("User has not rated the post!");
+
+ 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
+
+ #region Delete
+ public async Task<bool> DeleteRating(Guid ratingId)
+ {
+ 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
+
+ #region Validations
+ /// <summary>
+ /// Checks whether the user Id in the token and the given user Id match
+ /// </summary>
+ public async Task<bool> ValidateJwtForCreating(Guid userId, string rawTokenData)
+ {
+ User user = await this.GetUserForValidation(rawTokenData);
+
+ return user.Id == userId;
+ }
+
+ /// <summary>
+ /// 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
+ /// </summary>
+ public async Task<bool> 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;
+ }
+
+ /// <summary>
+ /// Returns the user, via their Id in the token
+ /// </summary>
+ private async Task<User> 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;
+ }
+
+ /// <summary>
+ /// Returns all values from a given claim type
+ /// </summary>
+ private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
+ {
+ List<string> toReturn = new();
+
+ foreach (var claim in claims)
+ if (claim.Type == type)
+ toReturn.Add(claim.Value);
+
+ return toReturn;
+ }
+ #endregion
+ }
+}