aboutsummaryrefslogtreecommitdiff
path: root/Backend
diff options
context:
space:
mode:
Diffstat (limited to 'Backend')
-rw-r--r--Backend/Api/Api/Api.csproj1
-rw-r--r--Backend/Api/Api/Controllers/LocationController.cs9
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs68
-rw-r--r--Backend/Api/Api/Controllers/UserController.cs31
-rw-r--r--Backend/Api/Api/Interfaces/IFileService.cs1
-rw-r--r--Backend/Api/Api/Interfaces/IPostService.cs12
-rw-r--r--Backend/Api/Api/Interfaces/IUserService.cs9
-rw-r--r--Backend/Api/Api/Models/Post.cs14
-rw-r--r--Backend/Api/Api/Services/FileService.cs21
-rw-r--r--Backend/Api/Api/Services/PostService.cs186
-rw-r--r--Backend/Api/Api/Services/UserService.cs223
11 files changed, 539 insertions, 36 deletions
diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj
index 24c41b7..0e541f9 100644
--- a/Backend/Api/Api/Api.csproj
+++ b/Backend/Api/Api/Api.csproj
@@ -10,6 +10,7 @@
<PackageReference Include="Geocoding.Core" Version="4.0.1" />
<PackageReference Include="Geocoding.Google" Version="4.0.1" />
<PackageReference Include="Geocoding.MapQuest" Version="4.0.1" />
+ <PackageReference Include="Magick.NET-Q16-AnyCPU" Version="12.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
diff --git a/Backend/Api/Api/Controllers/LocationController.cs b/Backend/Api/Api/Controllers/LocationController.cs
index c9ef9ba..ba9c9a8 100644
--- a/Backend/Api/Api/Controllers/LocationController.cs
+++ b/Backend/Api/Api/Controllers/LocationController.cs
@@ -51,10 +51,15 @@ namespace Api.Controllers
return BadRequest();
}
- [HttpPost("search")]
+ [HttpGet("search")]
[Authorize(Roles = "User")]
- public async Task<ActionResult<List<Location>>> searchLocation(int searchtype ,string? query,Coords? coords)
+ public async Task<ActionResult<List<Location>>> searchLocation(int searchtype ,string? query,double? latitude,double? longitude)
{
+ Coords coords = new Coords();
+ if (latitude!=null && longitude!=null) {
+ coords.latitude = (double)latitude;
+ coords.longitude = (double)longitude;
+ }
List<Location> ret = new List<Location>();
switch (searchtype)
{
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs
index dc48c73..03c3f81 100644
--- a/Backend/Api/Api/Controllers/PostController.cs
+++ b/Backend/Api/Api/Controllers/PostController.cs
@@ -56,7 +56,18 @@ namespace Api.Controllers
}
return BadRequest();
}
-
+ [HttpGet("posts/delete/{id}")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<string>> deletePost(string id)
+ {
+ var userid = await _userService.UserIdFromJwt();
+ var res = await _postService.deletePost(id, userid);
+ if (res)
+ {
+ return Ok(res);
+ }
+ return BadRequest("Post ne postoji ili vi niste vlasnik");
+ }
[HttpGet("image/{id}")]
//[Authorize(Roles = "User")]
public async Task<ActionResult> getImage(string id)
@@ -66,20 +77,30 @@ namespace Api.Controllers
return BadRequest("Slika ne postoji");
return File(System.IO.File.ReadAllBytes(f.path), "image/*", Path.GetFileName(f.path));
}
+ [HttpGet("image/compress/{id}")]
+ //[Authorize(Roles = "User")]
+ public async Task<ActionResult> getCompressedImage(string id)
+ {
+ Byte[] f = await _fileService.getCompressedImage(id);
+ if (f == null)
+ return BadRequest("Slika ne postoji");
+ return File(f, "image/*", "tempcompress");
+ }
[HttpPost("posts/{id}/addrating")]
[Authorize(Roles = "User")]
public async Task<ActionResult> addRating([FromBody] RatingReceive rating,string id)
{
var userid = await _userService.UserIdFromJwt();
- if (await _postService.AddOrReplaceRating(rating, userid))
- return Ok();
+ var rez = await _postService.AddOrReplaceRating(rating, userid);
+ if(rez != null)
+ return Ok(rez);
return BadRequest();
}
[HttpDelete("posts/{id}/removerating")]
[Authorize(Roles = "User")]
- public async Task<ActionResult> removeRating(string id)
+ public async Task<ActionResult<int>> removeRating(string id)
{
var userid = await _userService.UserIdFromJwt();
if (await _postService.RemoveRating(id,userid))
@@ -89,7 +110,7 @@ namespace Api.Controllers
[HttpPost("posts/{id}/addcomment")]
[Authorize(Roles = "User")]
- public async Task<ActionResult<Comment>> addComment([FromBody] CommentReceive cmnt,string id)
+ public async Task<ActionResult<CommentSend>> addComment([FromBody] CommentReceive cmnt,string id)
{
var userid = await _userService.UserIdFromJwt();
var c = await _postService.AddComment(cmnt, userid, id);
@@ -128,5 +149,42 @@ namespace Api.Controllers
}
return BadRequest();
}
+
+ [HttpGet("posts/get10MostViewed")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> Get10MostViewed()
+ {
+ return Ok(await _postService.Get10MostViewed());
+ }
+
+ [HttpGet("posts/get10Newest")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> Get10Newest()
+ {
+ return Ok(await _postService.Get10Newest());
+ }
+
+ [HttpGet("posts/get10Best")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> Get10Best()
+ {
+ return Ok(await _postService.Get10Best());
+ }
+
+
+ [HttpGet("posts/{id}/getUserPosts")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> GetUsersPosts(string id)
+ {
+ return Ok(await _postService.GetUsersPosts(id));
+ }
+
+ [HttpGet("posts/recommended")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> Recommended()
+ {
+ var userid = await _userService.UserIdFromJwt();
+ return Ok(await _postService.Recommended(userid));
+ }
}
}
diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs
index 7764af1..4d7905a 100644
--- a/Backend/Api/Api/Controllers/UserController.cs
+++ b/Backend/Api/Api/Controllers/UserController.cs
@@ -89,14 +89,39 @@ 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));
+ }
+ [HttpGet("{id}/unfollow")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<Boolean>> Unfollow(string id)
+ {
+ return Ok(await _userService.Unfollow(id));
+ }
+
+ [HttpGet("{id}/myFollowers")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<UserSend>>> GetMyFollowers()
+ {
+ return Ok(await _userService.GetMyFollowers());
+ }
}
}
diff --git a/Backend/Api/Api/Interfaces/IFileService.cs b/Backend/Api/Api/Interfaces/IFileService.cs
index e736305..33ff5d9 100644
--- a/Backend/Api/Api/Interfaces/IFileService.cs
+++ b/Backend/Api/Api/Interfaces/IFileService.cs
@@ -4,5 +4,6 @@
{
Task<Models.File> add(Models.File file);
Task<Models.File> getById(string id);
+ Task<Byte[]> getCompressedImage(string id);
}
} \ No newline at end of file
diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs
index 12a5fe8..96786bd 100644
--- a/Backend/Api/Api/Interfaces/IPostService.cs
+++ b/Backend/Api/Api/Interfaces/IPostService.cs
@@ -5,12 +5,13 @@ namespace Api.Interfaces
public interface IPostService
{
Task<PostSend> addPost(PostReceive post);
+ Task<Boolean> deletePost(string postid, string userid);
Task<List<PostSend>> getAllPosts();
Task<PostSend> getPostById(string id,string userid);
Task<PostSend> postToPostSend(Post post);
- Task<Boolean> AddOrReplaceRating(RatingReceive rating, string userid);
+ Task<RatingSend> AddOrReplaceRating(RatingReceive rating, string userid);
Task<Boolean> RemoveRating(string postid, string userid);
- Task<Comment> AddComment(CommentReceive cmnt, string userid, string postid);
+ Task<CommentSend> AddComment(CommentReceive cmnt, string userid, string postid);
Task<List<CommentSend>> ListComments(string postid);
Task<List<CommentSend>> CascadeComments(string parentid, Post p);
Task<Boolean> DeleteComments(string postid, string cmntid,string userid);
@@ -19,5 +20,12 @@ namespace Api.Interfaces
int DateEnumToDays(int filterdate);
Task<List<PostSend>> GetUsersPosts(string id);
Task<List<PostSend>> UserHistory(string userid);
+
+ Task<List<PostSend>> Get10Best();
+
+ Task<List<PostSend>> Get10MostViewed();
+
+ Task<List<PostSend>> Get10Newest();
+ Task<List<PostSend>> Recommended(string userid);
}
} \ No newline at end of file
diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs
index 5770de1..95dd46d 100644
--- a/Backend/Api/Api/Interfaces/IUserService.cs
+++ b/Backend/Api/Api/Interfaces/IUserService.cs
@@ -27,8 +27,15 @@ 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);
+ Task<Boolean> Unfollow(string id);
+
+ Task<List<UserSend>> GetMyFollowers();
+
}
}
diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs
index 1a4129f..dd007ec 100644
--- a/Backend/Api/Api/Models/Post.cs
+++ b/Backend/Api/Api/Models/Post.cs
@@ -38,15 +38,23 @@ namespace Api.Models
public DateTime createdAt { get; set; }
public int views { get; set; }
public double ratings { get; set; }
+ public int ratingscount { get; set; }
public List<CommentSend> comments { get; set; }
public List<File> images { get; set; }
public List<string>? tags { get; set; }
+ public DateTime? lastViewed { get; set; }
}
public class Rating
{
public string userId { get; set; }
public int rating { get; set; }
}
+ public class RatingSend
+ {
+ public int ratingscount { get; set; }
+ public double ratings { get; set; }
+ public int myrating { get; set; }
+ }
public class Comment
{
[BsonId]
@@ -100,4 +108,10 @@ namespace Api.Models
public int totalposts { get; set; }
public List<PostSend> posts { get; set; }
}
+
+ public class TagR
+ {
+ public int counter { get; set; }
+ public string tag { get; set; }
+ }
}
diff --git a/Backend/Api/Api/Services/FileService.cs b/Backend/Api/Api/Services/FileService.cs
index 1937c10..440b24f 100644
--- a/Backend/Api/Api/Services/FileService.cs
+++ b/Backend/Api/Api/Services/FileService.cs
@@ -1,5 +1,6 @@
using Api.Interfaces;
using Api.Models;
+using ImageMagick;
using MongoDB.Driver;
using File = Api.Models.File;
@@ -24,5 +25,25 @@ namespace Api.Services
{
return await _files.Find(file => file._id == id).FirstOrDefaultAsync();
}
+ public async Task<Byte[]> getCompressedImage(string id)
+ {
+ Byte[] res = null;
+ Models.File f = await getById(id);
+ if (f == null || !System.IO.File.Exists(f.path))
+ return res;
+ if (System.IO.File.Exists(f.path + "-compress"))
+ return System.IO.File.ReadAllBytes(f.path + "-compress");
+ using (MagickImage image = new MagickImage(f.path))
+ {
+ image.Format = image.Format;
+ image.Quality = 30;
+ res= image.ToByteArray();
+ image.Write(f.path + "-compress");
+ }
+
+
+ return res;
+
+ }
}
}
diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs
index bdf150b..855b231 100644
--- a/Backend/Api/Api/Services/PostService.cs
+++ b/Backend/Api/Api/Services/PostService.cs
@@ -39,7 +39,10 @@ namespace Api.Services
p.comments = new List<Comment>();
p.images = new List<Models.File>();
p.createdAt = DateTime.Now.ToUniversalTime();
- var tags = post.tags.Split("|").ToList();
+ List<String> tags;
+ if (post.tags != "none")
+ tags = post.tags.Remove(post.tags.Length-1,1).Split("|").ToList();
+ else tags = null;
p.tags = tags;
var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", p.ownerId);
if (!Directory.Exists(folderPath))
@@ -85,6 +88,7 @@ namespace Api.Services
p.views = post.views.Count();
p.createdAt = post.createdAt;
p.tags = post.tags;
+ p.ratingscount = post.ratings.Count();
if (post.ratings.Count() > 0)
{
List<int> ratings = new List<int>();
@@ -100,6 +104,18 @@ namespace Api.Services
return p;
}
+ public async Task<Boolean> deletePost(string postid,string userid)
+ {
+ var p = await _posts.Find(x => x._id == postid).FirstOrDefaultAsync();
+ if (p == null || p.ownerId != userid)
+ return false;
+ foreach (var image in p.images)
+ System.IO.File.Delete(image.path);
+
+ await _posts.DeleteOneAsync(postid);
+ return true;
+ }
+
public async Task<List<PostSend>> getAllPosts()
{
List<Post> posts = await _posts.Find(_ => true).ToListAsync();
@@ -116,22 +132,50 @@ namespace Api.Services
Post p = await _posts.Find(post => post._id == id).FirstOrDefaultAsync();
if (p != null)
{
- if (!p.views.Any(x => x == userid))
+ if (!p.views.Any(x => x.Split("|")[0] == userid))
{
- p.views.Add(userid);
+ p.views.Add(userid + "|" + DateTime.Now.ToUniversalTime().ToString());
+ await _posts.ReplaceOneAsync(x => x._id == id, p);
+ }
+ else
+ {
+ var v = p.views.Find(x => x.Split("|")[0] == userid);
+ p.views.Remove(v);
+ p.views.Add(userid + "|" + DateTime.Now.ToUniversalTime().ToString());
await _posts.ReplaceOneAsync(x => x._id == id, p);
}
}
return await postToPostSend(p);
}
- public async Task<Boolean> AddOrReplaceRating(RatingReceive rating,string userid)
+ public async Task<RatingSend> AddOrReplaceRating(RatingReceive rating,string userid) //0 return existing flag , -1 rating failed flag
{
Post p = await _posts.Find(post => post._id == rating.postId).FirstOrDefaultAsync();
if (p != null)
{
+ var tosend = new RatingSend();
+ var ps = await postToPostSend(p);
+ tosend.ratings = ps.ratings;
+ tosend.ratingscount = ps.ratingscount;
+
if (p.ownerId == userid)
- return false;
+ return null;
+ if(rating.rating == 0)// ako nema rating staviti 0
+ {
+ var r = p.ratings.Find(x => x.userId == userid);
+ if(r != null)
+ {
+ tosend.myrating=r.rating;
+ return tosend;
+ }
+ else
+ {
+ tosend.myrating = 0;
+ return tosend;
+ }
+ }
+ if(rating.rating<1 || rating.rating>5)
+ return null;
if(!p.ratings.Any(x => x.userId == userid))
{
Rating r = new Rating();
@@ -139,6 +183,7 @@ namespace Api.Services
r.userId = userid;
p.ratings.Add(r);
await _posts.ReplaceOneAsync(x => x._id == p._id, p);
+ tosend.myrating=rating.rating;
}
else
{
@@ -147,10 +192,15 @@ namespace Api.Services
r.rating = rating.rating;
p.ratings.Add(r);
await _posts.ReplaceOneAsync(x => x._id == p._id, p);
+ tosend.myrating = rating.rating;
}
- return true;
+ p = await _posts.Find(post => post._id == rating.postId).FirstOrDefaultAsync();
+ ps = await postToPostSend(p);
+ tosend.ratings = ps.ratings;
+ tosend.ratingscount = ps.ratingscount;
+ return tosend;
}
- return false;
+ return null;
}
public async Task<Boolean> RemoveRating(string postid, string userid)
{
@@ -167,20 +217,30 @@ namespace Api.Services
}
return false;
}
- public async Task<Comment> AddComment(CommentReceive cmnt,string userid,string postid)
+ public async Task<CommentSend> AddComment(CommentReceive cmnt,string userid,string postid)
{
Post p = await _posts.Find(post => post._id == postid).FirstOrDefaultAsync();
if (p != null)
{
- Comment c= new Comment();
+ Comment c = new Comment();
+ CommentSend c1= new CommentSend();
c.parentId = cmnt.parentId;
+ c1.parentId = cmnt.parentId;
c.userId = userid;
+ c1.userId = userid;
c.comment = cmnt.comment;
+ c1.comment = cmnt.comment;
c.timestamp = DateTime.Now.ToUniversalTime();
+ c1.timestamp = c.timestamp;
c._id = ObjectId.GenerateNewId().ToString();
+ c1._id = c._id;
+ var user = await _users.Find(x => x._id == c.userId).FirstOrDefaultAsync();
+ if (user != null)
+ c1.username = user.username;
+ else c1.username = "Deleted user";
p.comments.Add(c);
await _posts.ReplaceOneAsync(x => x._id == postid, p);
- return c;
+ return c1;
}
return null;
}
@@ -308,7 +368,7 @@ namespace Api.Services
xd = ls.OrderByDescending(x => x.createdAt).ToList();
break;
default:
-
+ xd = ls.OrderByDescending(x => x.views).ToList();
break;
}
if(xd != null)
@@ -360,13 +420,115 @@ namespace Api.Services
var tosend = new List<PostSend>();
foreach (var post in posts)
{
- if (post.views.Any(x => x.Equals(userid)))
+ if (post.views.Any(x => x.Split("|")[0] == userid))
{
+ var t = post.views.Find(x => x.Split("|")[0] == userid);
var x = await postToPostSend(post);
+ x.lastViewed = DateTime.Parse(t.Split("|")[1]).ToUniversalTime();
tosend.Add(x);
}
}
+ tosend = tosend.OrderByDescending(x => x.lastViewed).ToList();
return tosend;
}
+
+ public async Task<List<PostSend>> Get10Best()
+ {
+ List<Post> posts = await _posts.Find(_ => true).ToListAsync();
+ List<PostSend> temp = new List<PostSend>();
+ foreach (var post in posts)
+ {
+ temp.Add(await postToPostSend(post));
+ }
+ List<PostSend> best = temp.OrderByDescending(o => o.ratings).Take(10).ToList();
+ return best;
+ }
+
+ public async Task<List<PostSend>> Get10MostViewed()
+ {
+ List<Post> posts = await _posts.Find(_ => true).ToListAsync();
+ List<PostSend> temp = new List<PostSend>();
+ foreach (var post in posts)
+ {
+ temp.Add(await postToPostSend(post));
+ }
+ List<PostSend> mostViewed = temp.OrderByDescending(o => o.views).Take(10).ToList();
+ return mostViewed;
+ }
+
+ public async Task<List<PostSend>> Get10Newest()
+ {
+ List<Post> posts = await _posts.Find(_ => true).ToListAsync();
+ List<PostSend> temp = new List<PostSend>();
+ foreach (var post in posts)
+ {
+ temp.Add(await postToPostSend(post));
+ }
+ List<PostSend> newest = temp.OrderByDescending(o => o.createdAt).Take(10).ToList();
+ return newest;
+ }
+
+ public async Task<List<PostSend>> Recommended(string userid) // momgodb bloat bleh
+ {
+ List<PostSend> posts = await UserHistory(userid);
+ //TODO-LIMIT RECOMMENDED FOR POSTS FROM THIS MONTH ONLY
+ List<TagR> tags = new List<TagR>();
+ foreach (var post in posts)
+ {
+ if (post.tags != null)
+ {
+
+ foreach (var tagitem in post.tags)
+ {
+ if (!tags.Any(x => x.tag == tagitem))
+ {
+ var newtag = new TagR();
+ newtag.tag = tagitem;
+ newtag.counter = 1;
+ tags.Add(newtag);
+ }
+ else
+ {
+ var replace = tags.Find(x => x.tag == tagitem);
+ tags.Remove(replace);
+ replace.counter += 1;
+ tags.Add(replace);
+ }
+ }
+ }
+ }
+ var top5tags = tags.OrderByDescending(x => x.counter).Take(5).ToList();
+
+ var all = await _posts.Find(_ => true).ToListAsync();
+ var recent30 = new List<PostSend>();
+ var fiveoftop5tags = new List<PostSend>();
+ foreach (var elem in all)
+ {
+ if ((DateTime.Now - elem.createdAt).TotalDays < 30)
+ recent30.Add(await postToPostSend(elem));
+ }
+ recent30 = recent30.OrderByDescending(x => x.createdAt).ToList();
+ foreach (var tag in top5tags)
+ {
+ var five = new List<PostSend>();
+ foreach (var elem in recent30)
+ {
+ if (elem.tags != null)
+ {
+ if (elem.tags.Any(x => x == tag.tag))
+ five.Add(elem);
+ }
+ }
+ five = five.Take(5).ToList();
+ foreach(var elem in five)
+ {
+ fiveoftop5tags.Add(elem);
+ }
+ }
+
+ var taggedposts = new List<PostSend>();
+ taggedposts = fiveoftop5tags.Distinct().OrderByDescending(x => x.createdAt).ToList();
+ return taggedposts;
+ }
}
}
diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs
index f616d99..ec67729 100644
--- a/Backend/Api/Api/Services/UserService.cs
+++ b/Backend/Api/Api/Services/UserService.cs
@@ -6,6 +6,7 @@ using System.Security.Claims;
using MimeKit;
using MailKit.Net.Smtp;
using DnsClient;
+using MongoDB.Bson;
namespace Api.Services
{
@@ -380,21 +381,60 @@ namespace Api.Services
return tosend;
}
- public async Task<Boolean> AddFollower(string userId,string followerId)
+ public async Task<Boolean> updateUserFollowerFollowingCount(List<string> followers,List <string> followings,string userId)
{
- User u = await _users.Find(user => user._id==userId).FirstOrDefaultAsync();
- User f = await _users.Find(user => user._id == followerId).FirstOrDefaultAsync();
+ User u = await _users.Find(user => user._id == userId).FirstOrDefaultAsync();
+ if(u!= null)
+ {
+ u.followersCount = followers.Count();
+ u.followingCount = followings.Count();
+ return true;
+ }
+ return false;
+ }
- if (userId != null && followerId!=null)
+ public async Task<Boolean> AddFollower(string followerId)
+ {
+
+ string id = null;
+ if (_httpContext.HttpContext.User.FindFirstValue("id") != null)
+ {
+ id = _httpContext.HttpContext.User.FindFirstValue("id").ToString();
+ }
+
+ if (followerId == id)
{
- 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);
+ return false;
+ }
+ User f = await _users.Find(user => user._id == followerId).FirstOrDefaultAsync();
+ User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync();
+
+ if (id != null && followerId!=null)
+ {
+ if (f.followers == null)
+ {
+ f.followers = new List<string>();
+ f.followersCount = 0;
+ }
+ f.followers.Add(id);
+ f.followersCount =f.followers.Count();
+
_users.ReplaceOne(user => user._id == followerId, f);
+
+ if (u.following == null)
+ {
+ u.following = new List<string>();
+ u.followingCount = 0;
+ }
+ u.following.Add(followerId);
+ u.followingCount =u.following.Count();
+
+ _users.ReplaceOne(user=>user._id==id, u);
+
+
+ // updateUserFollowerFollowingCount(u.followers, u.following, u._id);
+ //updateUserFollowerFollowingCount(f.followers, f.following, f._id);
+
return true;
}
@@ -419,15 +459,19 @@ namespace Api.Services
continue;
}
UserSend follower = new UserSend();
+ follower.creationDate = utemp.creationDate;
+ follower.name = utemp.name;
follower.pfp = utemp.pfp;
follower.username = utemp.username;
follower.email = utemp.username;
+ follower.following = utemp.following;
follower.followers = utemp.followers;
follower._id = utemp._id;
followers.Add((UserSend)follower);
}
}
+ u.followersCount=followers.Count() ;
return followers;
}
return null;
@@ -450,18 +494,175 @@ namespace Api.Services
continue;
}
UserSend follower = new UserSend();
+ follower.creationDate = utemp.creationDate;
+ follower.name = utemp.name;
follower.pfp = utemp.pfp;
follower.username = utemp.username;
follower.email = utemp.username;
+ follower.following = utemp.following;
follower.followers = utemp.followers;
follower._id = utemp._id;
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.creationDate = utemp.creationDate;
+ following.name = utemp.name;
+ following.pfp = utemp.pfp;
+ following.username = utemp.username;
+ following.email = utemp.username;
+ following.following = utemp.following;
+ 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;
+ }
+
+ public async Task<bool> Unfollow(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 && f.followers!=null)
+ {
+ u.following.Remove(f._id);
+ u.followingCount=u.following.Count();
+ u.followersCount = u.followers.Count();
+
+
+ f.followers.Remove(u._id);
+ f.followersCount =f.followers.Count();
+ f.followingCount =f.following.Count();
+
+ _users.ReplaceOne(user => user._id == myId, u);
+ _users.ReplaceOne(user => user._id == id, f);
+
+ //updateUserFollowerFollowingCount(u.followers, u.following, u._id);
+ //updateUserFollowerFollowingCount(f.followers, f.following, f._id);
+ return true;
+ }
+
+ }
+ return false;
+ }
+
+ public async Task<List<UserSend>> GetMyFollowers()
+ {
+
+ 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> myfollowers = new List<UserSend>();
+
+ if (u!=null && 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.creationDate = utemp.creationDate;
+ follower.name = utemp.name;
+ follower.pfp = utemp.pfp;
+ follower.username = utemp.username;
+ follower.email = utemp.username;
+ follower.following = utemp.following;
+ follower.followers = utemp.followers;
+ follower._id = utemp._id;
+
+ myfollowers.Add((UserSend)follower);
+ }
+ return myfollowers;
+ }
+
+ return null;
+ }
}
+
}