diff options
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 22 | ||||
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 6 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 3 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IUserService.cs | 2 | ||||
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 12 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 98 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 79 |
7 files changed, 207 insertions, 15 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 3d7199c..03c3f81 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -92,14 +92,15 @@ namespace Api.Controllers 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)) @@ -170,5 +171,20 @@ namespace Api.Controllers 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 ada0f35..cc45737 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -110,5 +110,11 @@ namespace Api.Controllers 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)); + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 60781bb..96786bd 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -9,7 +9,7 @@ namespace Api.Interfaces 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<CommentSend> AddComment(CommentReceive cmnt, string userid, string postid); Task<List<CommentSend>> ListComments(string postid); @@ -26,5 +26,6 @@ namespace Api.Interfaces 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 21dcdc0..5f99733 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -33,5 +33,7 @@ namespace Api.Interfaces Task<List<UserSend>> GetMyFollowings(); Task<Boolean> CheckIfAlreadyFollow(string id); + Task<Boolean> Unfollow(string id); + } } diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 0dc8158..dd007ec 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -49,6 +49,12 @@ namespace Api.Models 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] @@ -102,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/PostService.cs b/Backend/Api/Api/Services/PostService.cs index dcfd5e0..855b231 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -148,13 +148,34 @@ namespace Api.Services 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(); @@ -162,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 { @@ -170,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) { @@ -440,5 +467,68 @@ namespace Api.Services 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 7bfc24a..cc75533 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,6 +381,18 @@ namespace Api.Services return tosend; } + public async Task<Boolean> updateUserFollowerFollowingCount(List<string> followers,List <string> followings,string userId) + { + 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; + } + public async Task<Boolean> AddFollower(string followerId) { string id = null; @@ -392,14 +405,29 @@ namespace Api.Services 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(id); + if (f.followers == null) + { + f.followers = new List<string>(); + f.followersCount = 0; + } + f.followers.Add(id); + f.followersCount =f.followers.Count(); + + + 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); _users.ReplaceOne(user => user._id == followerId, f); + + // updateUserFollowerFollowingCount(u.followers, u.following, u._id); + //updateUserFollowerFollowingCount(f.followers, f.following, f._id); + return true; } @@ -543,5 +571,42 @@ namespace Api.Services 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; + } + } } |