diff options
author | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-28 16:34:41 +0100 |
---|---|---|
committer | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-28 16:34:41 +0100 |
commit | 74936fc8083a6d0ff242d5328786822471dfc58f (patch) | |
tree | 4c1c17e2aaf5a593a0ff75c8e46c78da9bc7ed81 /Backend | |
parent | a0c779057e0c1d3c223593e38372adfc00e85d7d (diff) |
Omogućeno praćenje korisnika. Dodata provera pratilaca na back-u.
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 18 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IUserService.cs | 5 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 92 |
3 files changed, 105 insertions, 10 deletions
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<ActionResult<List<UserSend>>> AddFollower(string userId, string followerId) + public async Task<ActionResult<Boolean>> 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<ActionResult<List<UserSend>>> GetMyFollowings() + { + return Ok(await _userService.GetMyFollowings()); + } + [HttpGet("{id}/checkIfAlreadyFollow")] + [Authorize(Roles = "User")] + public async Task<ActionResult<Boolean>> 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<UserSend> GetUserData(string username); Task<UserSend> GetSelfUserData(string id); - Task<Boolean> AddFollower(string userId,string followerId); + Task<Boolean> AddFollower(string followerId); Task<List<UserSend>> GetFollowers(string id); Task<List<UserSend>> GetFollowing(string id); + Task<List<UserSend>> GetMyFollowings(); + + Task<Boolean> 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<Boolean> AddFollower(string userId,string followerId) + public async Task<Boolean> 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<string>(); u.followers.Add(followerId); if (f.following == null) f.following = new List<string>(); - 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<List<UserSend>> 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<UserSend> myFollowings = new List<UserSend>(); + 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<bool> 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; + } } } |