diff options
Diffstat (limited to 'src/DevHive.Services')
6 files changed, 112 insertions, 61 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs new file mode 100644 index 0000000..5da1b0d --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -0,0 +1,12 @@ +using AutoMapper; + +namespace DevHive.Services.Configurations.Mapping +{ + public class RatingMappings : Profile + { + public RatingMappings() + { + + } + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs new file mode 100644 index 0000000..d4eb7bd --- /dev/null +++ b/src/DevHive.Services/Models/Post/Rating/RatePostServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class RatePostServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool Liked { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..b071e74 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class ReadRatingServiceModel + { + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs index ceb5ebf..37d653c 100644 --- a/src/DevHive.Services/Services/FeedService.cs +++ b/src/DevHive.Services/Services/FeedService.cs @@ -38,7 +38,7 @@ namespace DevHive.Services.Services if (user == null) throw new ArgumentException("User doesn't exist!"); - List<User> friendsList = user.Friends.ToList(); + List<User> friendsList = user.Friends.Select(x => x.Friend).ToList(); if (friendsList.Count == 0) throw new ArgumentException("User has no friends to get feed from!"); diff --git a/src/DevHive.Services/Services/RatingService.cs b/src/DevHive.Services/Services/RatingService.cs new file mode 100644 index 0000000..2c5a6b6 --- /dev/null +++ b/src/DevHive.Services/Services/RatingService.cs @@ -0,0 +1,54 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Rating; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace DevHive.Services.Services +{ + public class RatingService + { + private readonly IPostRepository _postRepository; + private readonly IRatingRepository _ratingRepository; + private readonly IMapper _mapper; + + public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IMapper mapper) + { + this._postRepository = postRepository; + this._ratingRepository = ratingRepository; + this._mapper = mapper; + } + + public async Task<ReadRatingServiceModel> RatePost(RatePostServiceModel ratePostServiceModel) + { + if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) + throw new ArgumentNullException("Post does not exist!"); + + if (!await this._ratingRepository.HasUserRatedThisPost(ratePostServiceModel.UserId, ratePostServiceModel.PostId)) + throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); + + Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); + + Rating rating = post.Rating; + if (ratePostServiceModel.Liked) + rating.Likes++; + else + rating.Dislikes++; + + bool success = await this._ratingRepository.EditAsync(rating.Id, rating); + if (!success) + throw new InvalidOperationException("Unable to rate the post!"); + + Rating newRating = await this._ratingRepository.GetByIdAsync(rating.Id); + return this._mapper.Map<ReadRatingServiceModel>(newRating); + } + + public async Task<ReadRatingServiceModel> RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index ec74b5f..c2c42e0 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,6 +14,7 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; +using DevHive.Data.RelationModels; namespace DevHive.Services.Services { @@ -52,7 +53,7 @@ namespace DevHive.Services.Services if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password)) throw new ArgumentException("Incorrect password!"); - return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles)); + return new TokenModel(WriteJWTSecurityToken(user.Id, user.UserName, user.Roles)); } public async Task<TokenModel> RegisterUser(RegisterServiceModel registerModel) @@ -77,7 +78,7 @@ namespace DevHive.Services.Services await this._userRepository.AddAsync(user); - return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles)); + return new TokenModel(WriteJWTSecurityToken(user.Id, user.UserName, user.Roles)); } #endregion @@ -106,8 +107,6 @@ namespace DevHive.Services.Services { await this.ValidateUserOnUpdate(updateUserServiceModel); - await this.ValidateUserCollections(updateUserServiceModel); - User user = await this.PopulateModel(updateUserServiceModel); bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); @@ -189,62 +188,13 @@ namespace DevHive.Services.Services throw new ArgumentException("Username already exists!"); } - private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) - { - //Do NOT allow a user to change his roles, unless he is an Admin - bool isAdmin = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)) - .Roles.Any(r => r.Name == Role.AdminRole); - - if (isAdmin) - { - // Roles - foreach (var role in updateUserServiceModel.Roles) - { - Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? - throw new ArgumentException($"Role {role.Name} does not exist!"); - } - } - //Preserve original user roles - else - { - HashSet<Role> roles = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)).Roles; - - foreach (var role in roles) - { - Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? - throw new ArgumentException($"Role {role.Name} does not exist!"); - } - } - - // Friends - foreach (var friend in updateUserServiceModel.Friends) - { - User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.UserName) ?? - throw new ArgumentException($"User {friend.UserName} does not exist!"); - } - - // Languages - foreach (var language in updateUserServiceModel.Languages) - { - Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name) ?? - throw new ArgumentException($"Language {language.Name} does not exist!"); - } - - // Technology - foreach (var technology in updateUserServiceModel.Technologies) - { - Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name) ?? - throw new ArgumentException($"Technology {technology.Name} does not exist!"); - } - } - - private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles) + private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - HashSet<Claim> claims = new() { new Claim("ID", $"{userId}"), + new Claim("Username", username), }; foreach (var role in roles) @@ -268,12 +218,12 @@ namespace DevHive.Services.Services #endregion #region Misc - public async Task<Guid> SuperSecretPromotionToAdmin(Guid userId) + public async Task<TokenModel> SuperSecretPromotionToAdmin(Guid userId) { User user = await this._userRepository.GetByIdAsync(userId) ?? throw new ArgumentException("User does not exist! Can't promote shit in this country..."); - if (!await this._roleRepository.DoesNameExist("Admin")) + if (!await this._roleRepository.DoesNameExist(Role.AdminRole)) { Role adminRole = new() { @@ -289,7 +239,9 @@ namespace DevHive.Services.Services user.Roles.Add(admin); await this._userRepository.EditAsync(user.Id, user); - return admin.Id; + User newUser = await this._userRepository.GetByIdAsync(userId); + + return new TokenModel(WriteJWTSecurityToken(newUser.Id, newUser.UserName, newUser.Roles); } private async Task<User> PopulateModel(UpdateUserServiceModel updateUserServiceModel) @@ -309,14 +261,21 @@ namespace DevHive.Services.Services user.Roles = roles; /* Fetch Friends and replace model's*/ - HashSet<User> friends = new(); + HashSet<UserFriends> friends = new(); int friendsCount = updateUserServiceModel.Friends.Count; for (int i = 0; i < friendsCount; i++) { User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? throw new ArgumentException("Invalid friend's username!"); - friends.Add(friend); + UserFriends relation = new() + { + UserId = user.Id, + User = user, + FriendId = friend.Id, + Friend = friend + }; + friends.Add(relation); } user.Friends = friends; |
