diff options
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 24 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IUserService.cs | 4 | ||||
-rw-r--r-- | Backend/Api/Api/Models/User.cs | 13 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 82 |
4 files changed, 123 insertions, 0 deletions
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<ActionResult<List<UserSend>>> GetFollowers(string id) + { + return Ok(await _userService.GetFollowers(id)); + } + + [HttpGet("{id}/following")] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<UserSend>>> GetFollowing(string id) + { + return Ok(await _userService.GetFollowing(id)); + } + + [HttpGet("{id}/addFollower")] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<UserSend>>> 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<Boolean> AddOrChangePfp(string userid, IFormFile image); Task<UserSend> GetUserData(string username); Task<UserSend> GetSelfUserData(string id); + + Task<Boolean> AddFollower(string userId,string followerId); + Task<List<UserSend>> GetFollowers(string id); + Task<List<UserSend>> 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<string> followers { get; set; } + public List<string> 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<String> followers{ get; set; } + public List<String> 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<UserSend> _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<Boolean> 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<List<UserSend>> GetFollowers(string id) + { + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + List<UserSend> followers = new List<UserSend>(); + if (u != null) + { + //List<UserSend> 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<List<UserSend>> GetFollowing(string id) + { + User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + List<UserSend> following = new List<UserSend>(); + 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; + } } } |