From 702e947c20cbdc2c5aaacfa0e8172bfc97912dd2 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 28 Jan 2021 20:20:41 +0200 Subject: Updated comments and posts to use Creator.Id, rather than CreatorId; updated service to give Posts and Comments the post and creator objects from db --- src/DevHive.Services/Services/PostService.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index c27f40b..c3dc82f 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -37,11 +37,13 @@ namespace DevHive.Services.Services Post post = this._postMapper.Map(createPostServiceModel); post.TimeCreated = DateTime.Now; + post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId); + bool success = await this._postRepository.AddAsync(post); if (success) { Post newPost = await this._postRepository - .GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated); + .GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated); return newPost.Id; } @@ -57,11 +59,14 @@ namespace DevHive.Services.Services Comment comment = this._postMapper.Map(createCommentServiceModel); comment.TimeCreated = DateTime.Now; + comment.Creator = await this._userRepository.GetByIdAsync(createCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); + bool success = await this._commentRepository.AddAsync(comment); if (success) { Comment newComment = await this._commentRepository - .GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated); + .GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); return newComment.Id; } @@ -76,7 +81,7 @@ namespace DevHive.Services.Services Post post = await this._postRepository.GetByIdAsync(id) ?? throw new ArgumentException("The post does not exist!"); - User user = await this._userRepository.GetByIdAsync(post.CreatorId) ?? + User user = await this._userRepository.GetByIdAsync(post.Creator.Id) ?? throw new ArgumentException("The user does not exist!"); ReadPostServiceModel readPostServiceModel = this._postMapper.Map(post); @@ -92,7 +97,7 @@ namespace DevHive.Services.Services Comment comment = await this._commentRepository.GetByIdAsync(id) ?? throw new ArgumentException("The comment does not exist"); - User user = await this._userRepository.GetByIdAsync(comment.CreatorId) ?? + User user = await this._userRepository.GetByIdAsync(comment.Creator.Id) ?? throw new ArgumentException("The user does not exist"); ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); @@ -113,6 +118,8 @@ namespace DevHive.Services.Services Post post = this._postMapper.Map(updatePostServiceModel); post.TimeCreated = DateTime.Now; + post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId); + bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); if (result) @@ -129,6 +136,9 @@ namespace DevHive.Services.Services Comment comment = this._postMapper.Map(updateCommentServiceModel); comment.TimeCreated = DateTime.Now; + comment.Creator = await this._userRepository.GetByIdAsync(updateCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(updateCommentServiceModel.PostId); + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); if (result) @@ -166,7 +176,7 @@ namespace DevHive.Services.Services User user = await this.GetUserForValidation(rawTokenData); //If user made the post - if (post.CreatorId == user.Id) + if (post.Creator.Id == user.Id) return true; //If user is admin else if (user.Roles.Any(x => x.Name == Role.AdminRole)) @@ -182,7 +192,7 @@ namespace DevHive.Services.Services User user = await this.GetUserForValidation(rawTokenData); //If user made the comment - if (comment.CreatorId == user.Id) + if (comment.Creator.Id == user.Id) return true; //If user is admin else if (user.Roles.Any(x => x.Name == Role.AdminRole)) -- cgit v1.2.3