aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-24 00:07:44 +0200
committertranstrike <transtrike@gmail.com>2021-01-24 00:07:44 +0200
commitf910a2a63cb83b35c6589591400a69c8f7f7917c (patch)
tree2e601172f734d53ab7a903426ea2d674b5048a76 /src/DevHive.Services
parente01a81954e0fba2c4521e03a76f48a970a87994f (diff)
downloadDevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.tar
DevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.tar.gz
DevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.zip
Migrations added; CRUD over Posts&Comments successfully completed
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/CommentMappings.cs5
-rw-r--r--src/DevHive.Services/Configurations/Mapping/PostMappings.cs5
-rw-r--r--src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs4
-rw-r--r--src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs6
-rw-r--r--src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs (renamed from src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs)4
-rw-r--r--src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs4
-rw-r--r--src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs8
-rw-r--r--src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs2
-rw-r--r--src/DevHive.Services/Services/PostService.cs64
9 files changed, 78 insertions, 24 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs
index 3cea801..ac3c8f6 100644
--- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs
@@ -14,7 +14,10 @@ namespace DevHive.Services.Configurations.Mapping
.ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage));
CreateMap<Comment, ReadCommentServiceModel>()
- .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id));
+ .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id))
+ .ForMember(dest => dest.IssuerFirstName, src => src.Ignore())
+ .ForMember(dest => dest.IssuerLastName, src => src.Ignore())
+ .ForMember(dest => dest.IssuerUsername, src => src.Ignore());
}
}
}
diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
index e4924a5..cea7b1c 100644
--- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
@@ -16,7 +16,10 @@ namespace DevHive.Services.Configurations.Mapping
.ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage));
CreateMap<Post, ReadPostServiceModel>()
- .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id));
+ .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id))
+ .ForMember(dest => dest.CreatorFirstName, src => src.Ignore())
+ .ForMember(dest => dest.CreatorLastName, src => src.Ignore())
+ .ForMember(dest => dest.CreatorUsername, src => src.Ignore());
}
}
}
diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs
index 4dfd848..8d49659 100644
--- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs
@@ -6,10 +6,8 @@ namespace DevHive.Services.Models.Post.Comment
{
public Guid PostId { get; set; }
- public Guid IssuerId { get; set; }
+ public Guid CreatorId { get; set; }
public string Message { get; set; }
-
- public DateTime TimeCreated { get; set; }
}
}
diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs
index c6ff612..12e29a0 100644
--- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs
@@ -6,7 +6,11 @@ namespace DevHive.Services.Models.Post.Comment
{
public Guid CommentId { get; set; }
- public Guid IssuerId { get; set; }
+ public string IssuerFirstName { get; set; }
+
+ public string IssuerLastName { get; set; }
+
+ public string IssuerUsername { get; set; }
public Guid PostId { get; set; }
diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs
index 51cd739..3827d4d 100644
--- a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs
@@ -4,8 +4,12 @@ namespace DevHive.Services.Models.Post.Comment
{
public class UpdateCommentServiceModel
{
+ public Guid CreatorId { get; set; }
+
public Guid CommentId { get; set; }
+ public Guid PostId { get; set; }
+
public string NewMessage { get; set; }
}
}
diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
index 6b83f3e..36f6351 100644
--- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
@@ -4,12 +4,10 @@ namespace DevHive.Services.Models.Post.Post
{
public class CreatePostServiceModel
{
- public Guid IssuerId { get; set; }
+ public Guid CreatorId { get; set; }
public string Message { get; set; }
- public DateTime TimeCreated { get; set; }
-
// public List<IFormFile> Files { get; set; }
}
}
diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
index 52b9232..3e673c1 100644
--- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
@@ -8,13 +8,17 @@ namespace DevHive.Services.Models.Post.Post
{
public Guid PostId { get; set; }
- public Guid CreatorId { get; set; }
+ public string CreatorFirstName { get; set; }
+
+ public string CreatorLastName { get; set; }
+
+ public string CreatorUsername { get; set; }
public string Message { get; set; }
public DateTime TimeCreated { get; set; }
- public List<ReadCommentServiceModel> Comments { get; set; }
+ public List<ReadCommentServiceModel> Comments { get; set; } = new();
//public List<string> Files { get; set; }
}
diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
index 67ee711..8924b07 100644
--- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
@@ -6,6 +6,8 @@ namespace DevHive.Services.Models.Post.Post
{
public Guid PostId { get; set; }
+ public Guid CreatorId { get; set; }
+
public string NewMessage { get; set; }
// public List<IFormFile> Files { get; set; }
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs
index 377fe05..7fc975c 100644
--- a/src/DevHive.Services/Services/PostService.cs
+++ b/src/DevHive.Services/Services/PostService.cs
@@ -31,6 +31,9 @@ namespace DevHive.Services.Services
#region Create
public async Task<Guid> CreatePost(CreatePostServiceModel createPostServiceModel)
{
+ if(!await this._userRepository.DoesUserExistAsync(createPostServiceModel.CreatorId))
+ throw new ArgumentException("User does not exist!");
+
Post post = this._postMapper.Map<Post>(createPostServiceModel);
post.TimeCreated = DateTime.Now;
@@ -38,7 +41,7 @@ namespace DevHive.Services.Services
if (success)
{
Post newPost = await this._postRepository
- .GetPostByCreatorAndTimeCreatedAsync(createPostServiceModel.IssuerId, createPostServiceModel.TimeCreated);
+ .GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated);
return newPost.Id;
}
@@ -52,13 +55,13 @@ namespace DevHive.Services.Services
throw new ArgumentException("Post does not exist!");
Comment comment = this._postMapper.Map<Comment>(createCommentServiceModel);
- createCommentServiceModel.TimeCreated = DateTime.Now;
+ comment.TimeCreated = DateTime.Now;
bool success = await this._commentRepository.AddAsync(comment);
if (success)
{
Comment newComment = await this._commentRepository
- .GetCommentByIssuerAndTimeCreatedAsync(createCommentServiceModel.IssuerId, createCommentServiceModel.TimeCreated);
+ .GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated);
return newComment.Id;
}
@@ -73,7 +76,15 @@ namespace DevHive.Services.Services
Post post = await this._postRepository.GetByIdAsync(id) ??
throw new ArgumentException("The post does not exist!");
- return this._postMapper.Map<ReadPostServiceModel>(post);
+ User user = await this._userRepository.GetByIdAsync(post.CreatorId) ??
+ throw new ArgumentException("User does not exist He could've been deleted!");
+
+ ReadPostServiceModel readPostServiceModel = this._postMapper.Map<ReadPostServiceModel>(post);
+ readPostServiceModel.CreatorFirstName = user.FirstName;
+ readPostServiceModel.CreatorLastName = user.LastName;
+ readPostServiceModel.CreatorUsername = user.UserName;
+
+ return readPostServiceModel;
}
public async Task<ReadCommentServiceModel> GetCommentById(Guid id)
@@ -81,7 +92,14 @@ namespace DevHive.Services.Services
Comment comment = await this._commentRepository.GetByIdAsync(id) ??
throw new ArgumentException("The comment does not exist");
- return this._postMapper.Map<ReadCommentServiceModel>(comment);
+ User user = await this._userRepository.GetByIdAsync(comment.CreatorId);
+
+ ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map<ReadCommentServiceModel>(comment);
+ readCommentServiceModel.IssuerFirstName = user.FirstName;
+ readCommentServiceModel.IssuerLastName = user.LastName;
+ readCommentServiceModel.IssuerUsername = user.UserName;
+
+ return readCommentServiceModel;
}
#endregion
@@ -92,6 +110,8 @@ namespace DevHive.Services.Services
throw new ArgumentException("Post does not exist!");
Post post = this._postMapper.Map<Post>(updatePostServiceModel);
+ post.TimeCreated = DateTime.Now;
+
bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post);
if (result)
@@ -106,6 +126,8 @@ namespace DevHive.Services.Services
throw new ArgumentException("Comment does not exist!");
Comment comment = this._postMapper.Map<Comment>(updateCommentServiceModel);
+ comment.TimeCreated = DateTime.Now;
+
bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment);
if (result)
@@ -138,29 +160,45 @@ namespace DevHive.Services.Services
#region Validations
public async Task<bool> ValidateJwtForPost(Guid postId, string rawTokenData)
{
- Post post = await this._postRepository.GetByIdAsync(postId);
+ Post post = await this._postRepository.GetByIdAsync(postId) ??
+ throw new ArgumentException("Post does not exist!");
User user = await this.GetUserForValidation(rawTokenData);
- return post.CreatorId == user.Id;
+ //If user made the post
+ if (post.CreatorId == user.Id)
+ return true;
+ //If user is admin
+ else if(user.Roles.Any(x => x.Name == Role.AdminRole))
+ return true;
+ else
+ return false;
}
public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData)
{
- Comment comment = await this._commentRepository.GetByIdAsync(commentId);
+ Comment comment = await this._commentRepository.GetByIdAsync(commentId) ??
+ throw new ArgumentException("Comment does not exist!");
User user = await this.GetUserForValidation(rawTokenData);
- return comment.IssuerId == user.Id;
+ //If user made the comment
+ if (comment.CreatorId == user.Id)
+ return true;
+ //If user is admin
+ else if(user.Roles.Any(x => x.Name == Role.AdminRole))
+ return true;
+ else
+ return false;
}
private async Task<User> GetUserForValidation(string rawTokenData)
{
- var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
+ JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
- string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First();
+ Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First());
//HashSet<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
- User user = await this._userRepository.GetByUsernameAsync(jwtUserName)
- ?? throw new ArgumentException("User does not exist!");
+ User user = await this._userRepository.GetByIdAsync(jwtUserId) ??
+ throw new ArgumentException("User does not exist!");
return user;
}