diff options
author | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-29 00:45:39 +0100 |
---|---|---|
committer | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-29 00:45:39 +0100 |
commit | da08742d1be98466afe2f8c408600a5c0ff80d0e (patch) | |
tree | 231f721f00cdbc0ef3f9098d45124c7bc4ba9d96 /Backend/Api | |
parent | 10de5c6aa1baf0eafc2cb60054658709b76a0269 (diff) | |
parent | cf3c3a0f9e3beb73b3d7a8f1f49dfe76b458a60b (diff) |
Merge branch 'develop' of http://gitlab.pmf.kg.ac.rs/BrzoDoLokacije2022/odyssey/brzodolokacije into develop
# Conflicts:
# Backend/Api/Api/Controllers/PostController.cs
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 16 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 3 | ||||
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 12 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 98 |
4 files changed, 121 insertions, 8 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 7fe9961..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,11 +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/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/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; + } } } |