diff options
author | Jelena Petrovic <jelenapetrovic.7119@gmail.com> | 2022-11-28 17:38:42 +0100 |
---|---|---|
committer | Jelena Petrovic <jelenapetrovic.7119@gmail.com> | 2022-11-28 17:38:42 +0100 |
commit | c20758b0194ecd6fbf50afbd808801542a8f292d (patch) | |
tree | e57be44d8defce758a5e07afb625be289d8f1ad3 | |
parent | e4f4d06678b01028d1434865e721cd49707f822f (diff) |
dodavanje samo novog komentara u vec ucitane komentare #58
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<ActionResult<Comment>> addComment([FromBody] CommentReceive cmnt,string id) + public async Task<ActionResult<CommentSend>> 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<PostSend> postToPostSend(Post post); Task<Boolean> AddOrReplaceRating(RatingReceive rating, string userid); Task<Boolean> RemoveRating(string postid, string userid); - Task<Comment> AddComment(CommentReceive cmnt, string userid, string postid); + Task<CommentSend> AddComment(CommentReceive cmnt, string userid, string postid); Task<List<CommentSend>> ListComments(string postid); Task<List<CommentSend>> CascadeComments(string parentid, Post p); Task<Boolean> 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<Comment> AddComment(CommentReceive cmnt,string userid,string postid) + public async Task<CommentSend> 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<ResponseBody?> { - override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) { + request.enqueue(object : retrofit2.Callback<CommentSend?> { + override fun onResponse(call: Call<CommentSend?>, response: Response<CommentSend?>) { 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<ResponseBody?>, t: Throwable) { + override fun onFailure(call: Call<CommentSend?>, 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<MutableList<CommentSend>?> { - override fun onResponse(call: Call<MutableList<CommentSend>?>, response: Response<MutableList<CommentSend>?>) { - 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<MutableList<CommentSend>?> { + override fun onResponse(call: Call<MutableList<CommentSend>?>, response: Response<MutableList<CommentSend>?>) { + 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<MutableList<CommentSend>?>, t: Throwable) { - Log.d("main2",t.message.toString()) - } - }) + override fun onFailure(call: Call<MutableList<CommentSend>?>, 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<ResponseBody> @POST("api/Post/posts/{id}/addcomment") - fun addComment(@Header("Authorization") authHeader:String,@Path("id") id:String,@Body rating: CommentReceive):Call<ResponseBody> + fun addComment(@Header("Authorization") authHeader:String,@Path("id") id:String,@Body rating: CommentReceive):Call<CommentSend> @GET("api/Post/posts/{id}/listcomments") fun getComments(@Header("Authorization") authHeader:String,@Path("id") id:String):Call<MutableList<CommentSend>> |