diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-12-05 16:45:48 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-12-05 16:45:48 +0100 |
commit | ebd729276cdbe9212230e04c5716ba8cc54cb443 (patch) | |
tree | 8cc22b35119414e93732fbe6dfc5d97b0dc10ee6 /Backend | |
parent | f3c3fcaeb4ece508e018286c59a57da1d9ea0469 (diff) |
pretraga po tagovima i imenu lokacije, trending
Diffstat (limited to 'Backend')
-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 | 7 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 75 |
4 files changed, 88 insertions, 2 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 01a1c64..61a4f48 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -192,5 +192,12 @@ namespace Api.Controllers { return Ok(await _postService.addRemoveFavourite(id)); } + + [HttpGet("trending")] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<Trending>>> Trending() + { + return Ok(await _postService.TrendingTags()); + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index d5f489e..69846b0 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -28,5 +28,6 @@ namespace Api.Interfaces Task<List<PostSend>> Get10Newest(); Task<List<PostSend>> Recommended(string userid); Task<Boolean> addRemoveFavourite(string postId); + Task<List<Trending>> TrendingTags(); } }
\ No newline at end of file diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 22ed62e..9c0c429 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -115,5 +115,12 @@ namespace Api.Models { public int counter { get; set; } public string tag { get; set; } + public int? views { get; set; } + } + + public class Trending + { + public TagR tagr { get; set; } + public PostSendPage page { get; set; } } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index fd42d08..777389b 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -15,6 +15,7 @@ namespace Api.Services private readonly IFileService _fileService; private readonly ILocationService _locationService; private readonly IMongoCollection<User> _users; + private readonly IMongoCollection<Location> _locations; public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService,ILocationService locationService) { var database = mongoClient.GetDatabase(settings.DatabaseName); @@ -23,6 +24,7 @@ namespace Api.Services _httpContext = httpContext; _fileService = fileService; _locationService = locationService; + _locations = database.GetCollection<Location>(settings.LocationCollectionName); } public async Task<PostSend> addPost(PostReceive post) @@ -347,7 +349,27 @@ namespace Api.Services var lista = new List<Post>(); var ls = new List<PostSend>(); var xd = new List<PostSend>(); - lista = await _posts.Find(x => x.locationId == locid).ToListAsync(); + if(ObjectId.TryParse(locid, out _)) + lista = await _posts.Find(x => x.locationId == locid).ToListAsync(); + else + { + lista = await _posts.Find(x => x.tags != null && x.tags.Any(y => y.ToLower().Contains(locid.ToLower()))).ToListAsync(); + if (lista.Count==0) + { + var locs = await _locations.Find(x => x.city.ToLower().Contains(locid.ToLower()) || x.name.ToLower().Contains(locid.ToLower())).ToListAsync(); + foreach(var loc in locs) + { + var posts =await _posts.Find(x => x.locationId == loc._id).ToListAsync(); + if(posts != null) + { + foreach(var p in posts) + { + lista.Add(p); + } + } + } + } + } if (lista != null) { foreach (var elem in lista) @@ -472,7 +494,7 @@ namespace Api.Services 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 + //TODO-LIMIT RECOMMENDED FOR POSTS FROM THIS MONTH List<TagR> tags = new List<TagR>(); foreach (var post in posts) { @@ -555,6 +577,55 @@ namespace Api.Services return result; } + + public async Task<List<Trending>> TrendingTags() + { + var all = await _posts.Find(_ => true).ToListAsync(); + var posts = new List<PostSend>(); + foreach (var elem in all) + { + if ((DateTime.Now - elem.createdAt).TotalDays < 7) + posts.Add(await postToPostSend(elem)); + } + 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; + newtag.views = post.views; + tags.Add(newtag); + } + else + { + var replace = tags.Find(x => x.tag == tagitem); + tags.Remove(replace); + replace.counter += 1; + replace.views += post.views; + tags.Add(replace); + } + } + } + } + var top10tags = tags.OrderByDescending(x => x.views).Take(10).ToList(); + + var tosend = new List<Trending>(); + foreach(var trending in top10tags) + { + var novi = new Trending(); + novi.tagr = trending; + novi.page = await SearchPosts(trending.tag, 0, 1, 5); + tosend.Add(novi); + } + + return tosend; + } } } |