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 ++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 7 deletions(-) (limited to 'Backend/Api') 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; + } + } } -- cgit v1.2.3