From c20758b0194ecd6fbf50afbd808801542a8f292d Mon Sep 17 00:00:00 2001 From: Jelena Petrovic Date: Mon, 28 Nov 2022 17:38:42 +0100 Subject: dodavanje samo novog komentara u vec ucitane komentare #58 --- Backend/Api/Api/Controllers/PostController.cs | 2 +- Backend/Api/Api/Interfaces/IPostService.cs | 2 +- Backend/Api/Api/Services/PostService.cs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 4c6fbac..ec5829e 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -98,7 +98,7 @@ namespace Api.Controllers [HttpPost("posts/{id}/addcomment")] [Authorize(Roles = "User")] - public async Task> addComment([FromBody] CommentReceive cmnt,string id) + public async Task> addComment([FromBody] CommentReceive cmnt,string id) { var userid = await _userService.UserIdFromJwt(); var c = await _postService.AddComment(cmnt, userid, id); diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 5b04dc3..9bf71d5 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -10,7 +10,7 @@ namespace Api.Interfaces Task postToPostSend(Post post); Task AddOrReplaceRating(RatingReceive rating, string userid); Task RemoveRating(string postid, string userid); - Task AddComment(CommentReceive cmnt, string userid, string postid); + Task AddComment(CommentReceive cmnt, string userid, string postid); Task> ListComments(string postid); Task> CascadeComments(string parentid, Post p); Task DeleteComments(string postid, string cmntid,string userid); diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 0676d74..e59c735 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -178,20 +178,30 @@ namespace Api.Services } return false; } - public async Task AddComment(CommentReceive cmnt,string userid,string postid) + public async Task AddComment(CommentReceive cmnt,string userid,string postid) { Post p = await _posts.Find(post => post._id == postid).FirstOrDefaultAsync(); if (p != null) { - Comment c= new Comment(); + Comment c = new Comment(); + CommentSend c1= new CommentSend(); c.parentId = cmnt.parentId; + c1.parentId = cmnt.parentId; c.userId = userid; + c1.userId = userid; c.comment = cmnt.comment; + c1.comment = cmnt.comment; c.timestamp = DateTime.Now.ToUniversalTime(); + c1.timestamp = c.timestamp; c._id = ObjectId.GenerateNewId().ToString(); + c1._id = c._id; + var user = await _users.Find(x => x._id == c.userId).FirstOrDefaultAsync(); + if (user != null) + c1.username = user.username; + else c1.username = "Deleted user"; p.comments.Add(c); await _posts.ReplaceOneAsync(x => x._id == postid, p); - return c; + return c1; } return null; } -- cgit v1.2.3