From 74936fc8083a6d0ff242d5328786822471dfc58f Mon Sep 17 00:00:00 2001 From: TAMARA JERINIC Date: Mon, 28 Nov 2022 16:34:41 +0100 Subject: Omogućeno praćenje korisnika. Dodata provera pratilaca na back-u. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Api/Api/Controllers/UserController.cs | 18 +++++- Backend/Api/Api/Interfaces/IUserService.cs | 5 +- Backend/Api/Api/Services/UserService.cs | 92 +++++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 10 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index 7764af1..ada0f35 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -89,14 +89,26 @@ namespace Api.Controllers return Ok(await _userService.GetFollowing(id)); } - [HttpGet("addFollower")] + [HttpGet("{id}/addFollower")] [Authorize(Roles = "User")] - public async Task>> AddFollower(string userId, string followerId) + public async Task> AddFollower(string id) { - return Ok(await _userService.AddFollower(userId, followerId)); + return Ok(await _userService.AddFollower(id)); } + [HttpGet("{id}/myFollowings")] + [Authorize(Roles = "User")] + public async Task>> GetMyFollowings() + { + return Ok(await _userService.GetMyFollowings()); + } + [HttpGet("{id}/checkIfAlreadyFollow")] + [Authorize(Roles = "User")] + public async Task> CheckIfAlreadyFollow(String id) + { + return Ok(await _userService.CheckIfAlreadyFollow(id)); + } } } diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index 5770de1..21dcdc0 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -27,8 +27,11 @@ namespace Api.Interfaces Task GetUserData(string username); Task GetSelfUserData(string id); - Task AddFollower(string userId,string followerId); + Task AddFollower(string followerId); Task> GetFollowers(string id); Task> GetFollowing(string id); + Task> GetMyFollowings(); + + Task CheckIfAlreadyFollow(string id); } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index f616d99..7bfc24a 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -380,20 +380,25 @@ namespace Api.Services return tosend; } - public async Task AddFollower(string userId,string followerId) + public async Task AddFollower(string followerId) { - User u = await _users.Find(user => user._id==userId).FirstOrDefaultAsync(); + string id = null; + if (_httpContext.HttpContext.User.FindFirstValue("id") != null) + { + id = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + } User f = await _users.Find(user => user._id == followerId).FirstOrDefaultAsync(); - - if (userId != null && followerId!=null) + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + + 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(userId); - _users.ReplaceOne(user=>user._id==userId, u); + f.following.Add(id); + _users.ReplaceOne(user=>user._id==id, u); _users.ReplaceOne(user => user._id == followerId, f); return true; } @@ -428,6 +433,7 @@ namespace Api.Services followers.Add((UserSend)follower); } } + u.followersCount=followers.Count() ; return followers; } return null; @@ -459,9 +465,83 @@ namespace Api.Services following.Add((UserSend)follower); } } + u.followersCount = following.Count(); return following; } return null; } + + public async Task> GetMyFollowings() + { + string id = null; + + if (_httpContext.HttpContext.User.FindFirstValue("id") != null) + { + id = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + } + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + List myFollowings = new List(); + if (u != null) + { + + if (u.following != null && u.following.Count() > 0) + { + foreach (string userid in u.following) + { + User utemp = await _users.Find(user => user._id == userid).FirstOrDefaultAsync(); + if (utemp == null) + { + continue; + } + UserSend following = new UserSend(); + following.pfp = utemp.pfp; + following.username = utemp.username; + following.email = utemp.username; + following.followers = utemp.followers; + following._id = utemp._id; + + myFollowings.Add((UserSend)following); + } + } + return myFollowings; + } + return null; + } + + public async Task CheckIfAlreadyFollow(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 && u.following.Count() > 0) + { + foreach (string userid in u.following) + { + User utemp = await _users.Find(user => user._id == userid).FirstOrDefaultAsync(); + if (utemp == null) + { + continue; + } + if (utemp._id == f._id) + { + return true; + } + } + } + + } + + return false; + } } } -- cgit v1.2.3