From 4c6172aa80d5b8a8d12f45d1d91ce4e778d9412a Mon Sep 17 00:00:00 2001 From: TAMARA JERINIC Date: Mon, 21 Nov 2022 23:53:25 +0100 Subject: Dodato praćenje korisnika na back-u. Dodati fragmenti za praćenje korisnika na front-u. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Api/Api/Controllers/UserController.cs | 24 ++++++++ Backend/Api/Api/Interfaces/IUserService.cs | 4 ++ Backend/Api/Api/Models/User.cs | 13 +++++ Backend/Api/Api/Services/UserService.cs | 82 +++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index dd081cb..6cc41ea 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -74,5 +74,29 @@ namespace Api.Controllers return Ok(rez); return BadRequest(); } + + [HttpGet("{id}/followers")] + [Authorize(Roles = "User")] + public async Task>> GetFollowers(string id) + { + return Ok(await _userService.GetFollowers(id)); + } + + [HttpGet("{id}/following")] + [Authorize(Roles = "User")] + public async Task>> GetFollowing(string id) + { + return Ok(await _userService.GetFollowing(id)); + } + + [HttpGet("{id}/addFollower")] + [Authorize(Roles = "User")] + public async Task>> AddFollower(string userId, string followerId) + { + return Ok(await _userService.AddFollower(userId, followerId)); + } + + + } } diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index 313e909..5770de1 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -26,5 +26,9 @@ namespace Api.Interfaces Task AddOrChangePfp(string userid, IFormFile image); Task GetUserData(string username); Task GetSelfUserData(string id); + + Task AddFollower(string userId,string followerId); + Task> GetFollowers(string id); + Task> GetFollowing(string id); } } diff --git a/Backend/Api/Api/Models/User.cs b/Backend/Api/Api/Models/User.cs index 2e323a8..6c777e7 100644 --- a/Backend/Api/Api/Models/User.cs +++ b/Backend/Api/Api/Models/User.cs @@ -16,6 +16,11 @@ namespace Api.Models public String password { get; set; } public DateTime creationDate { get; set; } public File? pfp { get; set; } + + public List followers { get; set; } + public List following { get; set; } + public int followersCount { get; set; } + public int followingCount { get; set; } } public class Login @@ -58,5 +63,13 @@ namespace Api.Models public DateTime creationDate { get; set; } public File? pfp { get; set; } public int postcount { get; set; } + + public List followers{ get; set; } + public List following { get; set; } + + public int followersCount { get; set; } + public int followingCount { get; set; } + + } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 33e6d11..64a7f7a 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http; using System.Security.Claims; using MimeKit; using MailKit.Net.Smtp; +using DnsClient; namespace Api.Services { @@ -16,6 +17,8 @@ namespace Api.Services private readonly IJwtService _jwtService; private IConfiguration _configuration; private readonly IFileService _fileService; + + private readonly IMongoCollection _usersSend; public UserService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IFileService fileService) { var database = mongoClient.GetDatabase(settings.DatabaseName); @@ -35,6 +38,7 @@ namespace Api.Services return -2; //username already // user.password = hashPassword(user.password); + await _users.InsertOneAsync(user); return 1; } @@ -375,5 +379,83 @@ namespace Api.Services tosend.postcount = userposts.Count(); return tosend; } + + public async Task AddFollower(string userId,string followerId) + { + UserSend u = await _usersSend.Find(user => user._id==userId).FirstOrDefaultAsync(); + UserSend f = await _usersSend.Find(user => user._id == followerId).FirstOrDefaultAsync(); + + if (userId != null && followerId!=null) + { + u.followers.Add(followerId); + f.following.Add(userId); + return true; + } + + + return false; + } + + public async Task> GetFollowers(string id) + { + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + List followers = new List(); + if (u != null) + { + //List followers = u.followers; + if (u.followers.Count() > 0) + { + foreach (string userid in u.followers) + { + User utemp = await _users.Find(user => user._id == userid).FirstOrDefaultAsync(); + if (utemp == null) + { + continue; + } + UserSend follower = new UserSend(); + follower.pfp = utemp.pfp; + follower.username = utemp.username; + follower.email = utemp.username; + follower.followers = utemp.followers; + follower._id = utemp._id; + + followers.Add((UserSend)follower); + } + } + return followers; + } + return null; + } + + public async Task> GetFollowing(string id) + { + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + List following = new List(); + if (u != null) + { + + if (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 follower = new UserSend(); + follower.pfp = utemp.pfp; + follower.username = utemp.username; + follower.email = utemp.username; + follower.followers = utemp.followers; + follower._id = utemp._id; + + following.Add((UserSend)follower); + } + } + return following; + } + return null; + } } } -- cgit v1.2.3