diff options
Diffstat (limited to 'Backend/Api/Api/Services/UserService.cs')
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 33e6d11..f616d99 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,89 @@ namespace Api.Services tosend.postcount = userposts.Count(); return tosend; } + + public async Task<Boolean> AddFollower(string userId,string followerId) + { + User u = await _users.Find(user => user._id==userId).FirstOrDefaultAsync(); + User f = await _users.Find(user => user._id == followerId).FirstOrDefaultAsync(); + + if (userId != 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); + _users.ReplaceOne(user => user._id == followerId, f); + 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!=null &&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!=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 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; + } } } |