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 ++++- .../Activities/ActivitySinglePost.kt | 71 +++++++++++----------- .../brzodolokacije/Interfaces/IBackendApi.kt | 2 +- 5 files changed, 52 insertions(+), 41 deletions(-) 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; } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt index b788b93..d1ec76b 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt @@ -1,16 +1,11 @@ package com.example.brzodolokacije.Activities -import android.app.Dialog -import android.content.Context import android.content.Intent import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.preference.PreferenceManager -import android.provider.ContactsContract.CommonDataKinds.Im import android.util.Log -import android.view.Gravity -import android.widget.ImageButton import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @@ -26,7 +21,6 @@ import com.example.brzodolokacije.Services.SharedPreferencesHelper import com.example.brzodolokacije.databinding.ActivitySinglePostBinding import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.gson.Gson -import kotlinx.android.synthetic.main.activity_single_post.* import okhttp3.ResponseBody import org.osmdroid.config.Configuration import org.osmdroid.tileprovider.tilesource.TileSourceFactory @@ -197,10 +191,11 @@ class ActivitySinglePost : AppCompatActivity() { val postApi= RetrofitHelper.getInstance() val token= SharedPreferencesHelper.getValue("jwt", this@ActivitySinglePost) val request=postApi.addComment("Bearer "+token,post._id,comment) - request.enqueue(object : retrofit2.Callback { - override fun onResponse(call: Call, response: Response) { + request.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { if(response.isSuccessful){ - requestGetComments() + var newComment=response.body()!! + requestGetComments(newComment) binding.NewComment.text.clear() }else{ if(response.errorBody()!=null) @@ -210,38 +205,46 @@ class ActivitySinglePost : AppCompatActivity() { } - override fun onFailure(call: Call, t: Throwable) { + override fun onFailure(call: Call, t: Throwable) { Log.d("main2",t.message.toString()) } }) } - fun requestGetComments(){ - val postApi= RetrofitHelper.getInstance() - val token= SharedPreferencesHelper.getValue("jwt", this@ActivitySinglePost) - val request=postApi.getComments("Bearer "+token,post._id) - request.enqueue(object : retrofit2.Callback?> { - override fun onResponse(call: Call?>, response: Response?>) { - if(response.isSuccessful){ - comments= response.body()!! - if(comments!=null && comments!!.isNotEmpty()){ - buildRecyclerViewComments() - if(comments!=null) - binding.tvCommentCount.text=comments?.size.toString() - else - binding.tvCommentCount.text="0" + fun requestGetComments(newComment:CommentSend?=null){ + if(newComment==null){ + val postApi= RetrofitHelper.getInstance() + val token= SharedPreferencesHelper.getValue("jwt", this@ActivitySinglePost) + val request=postApi.getComments("Bearer "+token,post._id) + request.enqueue(object : retrofit2.Callback?> { + override fun onResponse(call: Call?>, response: Response?>) { + if(response.isSuccessful){ + comments= response.body()!! + if(comments!=null && comments!!.isNotEmpty()){ + buildRecyclerViewComments() + if(comments!=null) + binding.tvCommentCount.text=comments?.size.toString() + else + binding.tvCommentCount.text="0" + } + }else{ + if(response.errorBody()!=null) + Log.d("main1",response.message().toString()) } - }else{ - if(response.errorBody()!=null) - Log.d("main1",response.message().toString()) - } - } + } - override fun onFailure(call: Call?>, t: Throwable) { - Log.d("main2",t.message.toString()) - } - }) + override fun onFailure(call: Call?>, t: Throwable) { + Log.d("main2",t.message.toString()) + } + }) + } + else{ + (adapterComments as CommentsAdapter).items.add(0,newComment) + recyclerViewComments?.adapter=adapterComments + Log.d("main",newComment.username) + binding.tvCommentCount.text=comments?.size.toString() + } } fun requestAddRating(rating:RatingReceive){ @@ -325,6 +328,4 @@ class ActivitySinglePost : AppCompatActivity() { } }) } - - } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt index fc2d24f..ce8d7e3 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt @@ -40,7 +40,7 @@ interface IBackendApi { @POST("api/Post/posts/{id}/addrating") fun addRating(@Header("Authorization") authHeader:String,@Path("id") id:String,@Body rating: RatingReceive):Call @POST("api/Post/posts/{id}/addcomment") - fun addComment(@Header("Authorization") authHeader:String,@Path("id") id:String,@Body rating: CommentReceive):Call + fun addComment(@Header("Authorization") authHeader:String,@Path("id") id:String,@Body rating: CommentReceive):Call @GET("api/Post/posts/{id}/listcomments") fun getComments(@Header("Authorization") authHeader:String,@Path("id") id:String):Call> -- cgit v1.2.3