blob: f2f60d18fd6235753305fb825fa66e1d46cca0bc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Models;
using DevHive.Services.Models.Post.Comment;
using DevHive.Services.Models.Post.Post;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using DevHive.Services.Interfaces;
using DevHive.Data.Interfaces.Repositories;
namespace DevHive.Services.Services
{
public class PostService : IPostService
{
private readonly IPostRepository _postRepository;
private readonly IUserRepository _userRepository;
private readonly IMapper _postMapper;
public PostService(IPostRepository postRepository, IUserRepository userRepository, IMapper postMapper)
{
this._postRepository = postRepository;
this._userRepository = userRepository;
this._postMapper = postMapper;
}
//Create
public async Task<Guid> CreatePost(CreatePostServiceModel postServiceModel)
{
Post post = this._postMapper.Map<Post>(postServiceModel);
bool success = await this._postRepository.AddAsync(post);
if(success)
{
Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated);
return newPost.Id;
}
else
return Guid.Empty;
}
public async Task<Guid> AddComment(CreateCommentServiceModel commentServiceModel)
{
commentServiceModel.TimeCreated = DateTime.Now;
Comment comment = this._postMapper.Map<Comment>(commentServiceModel);
bool success = await this._postRepository.AddCommentAsync(comment);
if(success)
{
Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated);
return newComment.Id;
}
else
return Guid.Empty;
}
//Read
public async Task<PostServiceModel> GetPostById(Guid id)
{
Post post = await this._postRepository.GetByIdAsync(id)
?? throw new ArgumentException("Post does not exist!");
return this._postMapper.Map<PostServiceModel>(post);
}
public async Task<CommentServiceModel> GetCommentById(Guid id)
{
Comment comment = await this._postRepository.GetCommentByIdAsync(id);
if (comment == null)
throw new ArgumentException("The comment does not exist");
return this._postMapper.Map<CommentServiceModel>(comment);
}
//Update
public async Task<bool> UpdatePost(UpdatePostServiceModel postServiceModel)
{
if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId))
throw new ArgumentException("Comment does not exist!");
Post post = this._postMapper.Map<Post>(postServiceModel);
return await this._postRepository.EditAsync(post);
}
public async Task<bool> UpdateComment(UpdateCommentServiceModel commentServiceModel)
{
if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id))
throw new ArgumentException("Comment does not exist!");
Comment comment = this._postMapper.Map<Comment>(commentServiceModel);
bool result = await this._postRepository.EditCommentAsync(comment);
return result;
}
//Delete
public async Task<bool> DeletePost(Guid id)
{
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))
throw new ArgumentException("Comment does not exist!");
Comment comment = await this._postRepository.GetCommentByIdAsync(id);
bool result = await this._postRepository.DeleteCommentAsync(comment);
return result;
}
//Validate
public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData)
{
Comment comment = await this._postRepository.GetCommentByIdAsync(commentId);
User user = await this.GetUserForValidation(rawTokenData);
if (comment.IssuerId != user.Id)
return false;
return true;
}
private async Task<User> GetUserForValidation(string rawTokenData)
{
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0];
//List<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
User user = await this._userRepository.GetByUsernameAsync(jwtUserName)
?? throw new ArgumentException("User does not exist!");
return user;
}
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;
}
}
}
|