diff options
Diffstat (limited to 'src/DevHive.Services/Services/PostService.cs')
| -rw-r--r-- | src/DevHive.Services/Services/PostService.cs | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 321c694..da9e76b 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -7,17 +7,18 @@ using DevHive.Services.Models.Post.Comment; using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Data.Interfaces; namespace DevHive.Services.Services { - public class PostService + public class PostService : IPostService { - private readonly PostRepository _postRepository; - private readonly UserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; private readonly IMapper _postMapper; - public PostService(PostRepository postRepository, UserRepository userRepository , IMapper postMapper) + public PostService(IPostRepository postRepository, IUserRepository userRepository, IMapper postMapper) { this._postRepository = postRepository; this._userRepository = userRepository; @@ -36,16 +37,16 @@ namespace DevHive.Services.Services { commentServiceModel.TimeCreated = DateTime.Now; Comment comment = this._postMapper.Map<Comment>(commentServiceModel); - + bool result = await this._postRepository.AddCommentAsync(comment); return result; } - + //Read public async Task<PostServiceModel> GetPostById(Guid id) { - Post post = await this._postRepository.GetByIdAsync(id) + Post post = await this._postRepository.GetByIdAsync(id) ?? throw new ArgumentException("Post does not exist!"); return this._postMapper.Map<PostServiceModel>(post); @@ -55,7 +56,7 @@ namespace DevHive.Services.Services { Comment comment = await this._postRepository.GetCommentByIdAsync(id); - if(comment == null) + if (comment == null) throw new ArgumentException("The comment does not exist"); return this._postMapper.Map<CommentServiceModel>(comment); @@ -88,7 +89,7 @@ namespace DevHive.Services.Services Post post = await this._postRepository.GetByIdAsync(id); return await this._postRepository.DeleteAsync(post); } - + public async Task<bool> DeleteComment(Guid id) { if (!await this._postRepository.DoesCommentExist(id)) @@ -118,7 +119,7 @@ namespace DevHive.Services.Services string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; //List<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - + User user = await this._userRepository.GetByUsername(jwtUserName) ?? throw new ArgumentException("User does not exist!"); @@ -128,8 +129,8 @@ namespace DevHive.Services.Services private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims) { List<string> toReturn = new(); - - foreach(var claim in claims) + + foreach (var claim in claims) if (claim.Type == type) toReturn.Add(claim.Value); |
