aboutsummaryrefslogtreecommitdiff
path: root/Backend
diff options
context:
space:
mode:
authorbranislav.radivojevic <wafflemynxyt@gmail.com>2022-11-28 22:41:49 +0100
committerbranislav.radivojevic <wafflemynxyt@gmail.com>2022-11-28 22:41:49 +0100
commit6c44a9844b79aeb69befb277e3bd3daec44bc059 (patch)
tree8ea32aaf46edcd1ddc274b78e07a1b1861a586c4 /Backend
parent68b86d96142eb1f9e42ba2d87a4277d014759c46 (diff)
parentf2e816ab9e625b0575f6e81fffb0ee7fceddc84e (diff)
Merge branch 'develop' of http://gitlab.pmf.kg.ac.rs/BrzoDoLokacije2022/odyssey/brzodolokacije into develop
Diffstat (limited to 'Backend')
-rw-r--r--Backend/Api/Api/Controllers/UserController.cs6
-rw-r--r--Backend/Api/Api/Interfaces/IUserService.cs2
-rw-r--r--Backend/Api/Api/Services/UserService.cs79
3 files changed, 80 insertions, 7 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<ActionResult<Boolean>> 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<List<UserSend>> GetMyFollowings();
Task<Boolean> CheckIfAlreadyFollow(string id);
+ Task<Boolean> 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<Boolean> updateUserFollowerFollowingCount(List<string> followers,List <string> 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<Boolean> 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<string>();
- u.followers.Add(followerId);
- if (f.following == null)
- f.following = new List<string>();
- f.following.Add(id);
+ if (f.followers == null)
+ {
+ f.followers = new List<string>();
+ f.followersCount = 0;
+ }
+ f.followers.Add(id);
+ f.followersCount =f.followers.Count();
+
+
+ if (u.following == null)
+ {
+ u.following = new List<string>();
+ 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<bool> 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;
+ }
+ }
}