diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 23:35:55 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 23:35:55 +0100 |
commit | cf3c3a0f9e3beb73b3d7a8f1f49dfe76b458a60b (patch) | |
tree | a2e1db00f1b22915227d66a0a839fc0a40eac25e /Backend/Api | |
parent | 6c44a9844b79aeb69befb277e3bd3daec44bc059 (diff) |
Prva iteracije recommended algoritma
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 7 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 6 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 63 |
4 files changed, 77 insertions, 0 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 88c1c99..6970d28 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -171,5 +171,12 @@ namespace Api.Controllers return Ok(await _postService.Get10Best()); } + [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 a9adb05..96786bd 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -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 78b4705..dd007ec 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -108,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 c57cfec..855b231 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -467,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; + } } } |