From f2e816ab9e625b0575f6e81fffb0ee7fceddc84e Mon Sep 17 00:00:00 2001 From: TAMARA JERINIC Date: Mon, 28 Nov 2022 22:26:51 +0100 Subject: Omogućeno praćenje i prestanak praćenja korisnika na front-u i back-u. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Api/Api/Controllers/UserController.cs | 6 + Backend/Api/Api/Interfaces/IUserService.cs | 2 + Backend/Api/Api/Services/UserService.cs | 79 ++++++++++-- .../Activities/ActivityUserProfile.kt | 138 ++++++++++++++++----- .../brzodolokacije/Interfaces/IBackendApi.kt | 3 + .../src/main/res/layout/activity_user_profile.xml | 33 ++++- 6 files changed, 216 insertions(+), 45 deletions(-) diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index ada0f35..cc45737 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -110,5 +110,11 @@ namespace Api.Controllers return Ok(await _userService.CheckIfAlreadyFollow(id)); } + [HttpGet("{id}/unfollow")] + [Authorize(Roles = "User")] + public async Task> Unfollow(string id) + { + return Ok(await _userService.Unfollow(id)); + } } } diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index 21dcdc0..5f99733 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -33,5 +33,7 @@ namespace Api.Interfaces Task> GetMyFollowings(); Task CheckIfAlreadyFollow(string id); + Task Unfollow(string id); + } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 7bfc24a..cc75533 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -6,6 +6,7 @@ using System.Security.Claims; using MimeKit; using MailKit.Net.Smtp; using DnsClient; +using MongoDB.Bson; namespace Api.Services { @@ -380,6 +381,18 @@ namespace Api.Services return tosend; } + public async Task updateUserFollowerFollowingCount(List followers,List followings,string userId) + { + User u = await _users.Find(user => user._id == userId).FirstOrDefaultAsync(); + if(u!= null) + { + u.followersCount = followers.Count(); + u.followingCount = followings.Count(); + return true; + } + return false; + } + public async Task AddFollower(string followerId) { string id = null; @@ -392,14 +405,29 @@ namespace Api.Services if (id != null && followerId!=null) { - if (u.followers == null) - u.followers = new List(); - u.followers.Add(followerId); - if (f.following == null) - f.following = new List(); - f.following.Add(id); + if (f.followers == null) + { + f.followers = new List(); + f.followersCount = 0; + } + f.followers.Add(id); + f.followersCount =f.followers.Count(); + + + if (u.following == null) + { + u.following = new List(); + u.followingCount = 0; + } + u.following.Add(followerId); + u.followingCount =u.following.Count(); + _users.ReplaceOne(user=>user._id==id, u); _users.ReplaceOne(user => user._id == followerId, f); + + // updateUserFollowerFollowingCount(u.followers, u.following, u._id); + //updateUserFollowerFollowingCount(f.followers, f.following, f._id); + return true; } @@ -543,5 +571,42 @@ namespace Api.Services return false; } - } + + public async Task Unfollow(string id) + { + string myId = null; + + if (_httpContext.HttpContext.User.FindFirstValue("id") != null) + { + myId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + } + + User u = await _users.Find(user => user._id == myId).FirstOrDefaultAsync(); + User f = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + + if (u != null) + { + if (u.following != null && f.followers!=null) + { + u.following.Remove(f._id); + u.followingCount=u.following.Count(); + u.followersCount = u.followers.Count(); + + + f.followers.Remove(u._id); + f.followersCount =f.followers.Count(); + f.followingCount =f.following.Count(); + + _users.ReplaceOne(user => user._id == myId, u); + _users.ReplaceOne(user => user._id == id, f); + + //updateUserFollowerFollowingCount(u.followers, u.following, u._id); + //updateUserFollowerFollowingCount(f.followers, f.following, f._id); + return true; + } + + } + return false; + } + } } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt index c03dbd3..4f48dc7 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt @@ -1,19 +1,15 @@ package com.example.brzodolokacije.Activities import android.annotation.SuppressLint -import android.graphics.Color -import android.graphics.PorterDuff -import android.graphics.drawable.Drawable import android.os.Bundle import android.util.Log import android.widget.* import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat +import androidx.core.view.isVisible import androidx.fragment.app.Fragment import com.bumptech.glide.Glide import com.example.brzodolokacije.Fragments.FragmentShowUserPosts import com.example.brzodolokacije.Models.UserReceive -import com.example.brzodolokacije.R import com.example.brzodolokacije.R.* import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper @@ -21,7 +17,6 @@ import com.google.gson.Gson import retrofit2.Call import retrofit2.Callback import retrofit2.Response -import java.security.AccessController.getContext class ActivityUserProfile : AppCompatActivity() { @@ -35,9 +30,10 @@ class ActivityUserProfile : AppCompatActivity() { private lateinit var fragmentContainer: FrameLayout private lateinit var userObject:UserReceive private lateinit var openChat:ImageButton - + private lateinit var unfollowUser:Button private lateinit var followersList: MutableList private lateinit var followingList: MutableList + private var follow:Boolean=false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -49,6 +45,7 @@ class ActivityUserProfile : AppCompatActivity() { followingNumber=findViewById(id.tvActivityUserProfileFollowNo) profilePicture=findViewById(id.tvActivityProfileProfilePicture) followUser=findViewById(id.btnActivityUserProfileFollow) + unfollowUser=findViewById(id.btnActivityUserProfileUnFollow) showUserPosts=findViewById(id.btnActivityUserProfileShowPosts) fragmentContainer=findViewById(id.flActivityProfileFragmentContainer) openChat=findViewById(id.activityUserProfileOpenChat) @@ -74,31 +71,68 @@ class ActivityUserProfile : AppCompatActivity() { } } + checkIfAlreadyFollow() + updateUserData() followUser.setOnClickListener{ + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.addFollower("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + unfollowUser.isVisible=true + unfollowUser.isClickable=true + unfollowUser.isEnabled=true + followUser.isVisible=false + followUser.isClickable=false + followUser.isEnabled=false + updateUserData() + + Toast.makeText( + this@ActivityUserProfile, "PRATITE KORISNIKA", Toast.LENGTH_LONG + ).show(); + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) - checkIfAlreadyFollow() - + } + unfollowUser.setOnClickListener { + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.unfollow("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + unfollowUser.isVisible=false + unfollowUser.isClickable=false + unfollowUser.isEnabled=false + followUser.isVisible=true + followUser.isClickable=true + followUser.isEnabled=true + updateUserData() + Toast.makeText( + this@ActivityUserProfile, "VIŠE NE PRATITE KORISNIKA", Toast.LENGTH_LONG + ).show(); + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) - val api = RetrofitHelper.getInstance() - val token= SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) - var data=api.addFollower("Bearer "+token,userObject._id); - data.enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - Toast.makeText( - this@ActivityUserProfile, "PRATITE KORISNIKA", Toast.LENGTH_LONG - ).show(); - } - override fun onFailure(call: Call, t: Throwable) { - Toast.makeText( - this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG - ).show(); - } - }) } @@ -132,15 +166,34 @@ class ActivityUserProfile : AppCompatActivity() { } var follow = response.body()!! if(follow){ + Log.d("success","follow") - followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_remove_24,0,0,0) + /*followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_remove_24,0,0,0) followUser.text="Ne prati više" - + follow=false + */ + unfollowUser.isVisible=true + unfollowUser.isClickable=true + unfollowUser.isEnabled=true + followUser.isVisible=false + followUser.isClickable=false + followUser.isEnabled=false } - if(!follow){ + else{ Log.d("success","not follow") - followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_add_alt_24,0,0,0) + /*followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_add_alt_24,0,0,0) followUser.text="Prati" + follow=true + */ + + unfollowUser.isVisible=false + unfollowUser.isClickable=false + unfollowUser.isEnabled=false + followUser.isVisible=true + followUser.isClickable=true + followUser.isEnabled=true + + } } @@ -148,7 +201,28 @@ class ActivityUserProfile : AppCompatActivity() { } - fun checkIfAlreadyFollowChangeButton(){ + fun updateUserData(){ + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.getProfileFromId("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + var userData=response.body()!! + name.text=userData.name + postsNumber.text=userData.postNumber.toString() + followersNumber.text=userData.followersCount.toString() + followingNumber.text=userData.followingCount.toString() + } + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) } + } \ No newline at end of file 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 ce8d7e3..bc8c7ae 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 @@ -101,4 +101,7 @@ interface IBackendApi { @GET("/api/user/{id}/checkIfAlreadyFollow") fun checkIfAlreadyFollow(@Header("Authorization") authHeader:String,@Path("id") id:String):Call + @GET("/api/user/{id}/unfollow") + fun unfollow(@Header("Authorization") authHeader:String,@Path("id") id:String):Call + } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml index a446f9a..0e90fc7 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml @@ -228,22 +228,44 @@