From aae690368dc3f60d18c3564cd4c1603820defec1 Mon Sep 17 00:00:00 2001 From: "branislav.radivojevic" Date: Mon, 5 Dec 2022 17:10:49 +0100 Subject: Najpopularniji post za svaku lokaciju u radiusu --- Backend/Api/Api/Controllers/LocationController.cs | 22 +++++++++++++++++++- Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Services/LocationService.cs | 2 ++ Backend/Api/Api/Services/PostService.cs | 25 +++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Backend/Api/Api/Controllers/LocationController.cs b/Backend/Api/Api/Controllers/LocationController.cs index ba9c9a8..ac093bb 100644 --- a/Backend/Api/Api/Controllers/LocationController.cs +++ b/Backend/Api/Api/Controllers/LocationController.cs @@ -12,9 +12,11 @@ namespace Api.Controllers public class LocationController : ControllerBase { private readonly ILocationService _locationService; - public LocationController(ILocationService locationService) + private readonly IPostService _postService; + public LocationController(ILocationService locationService, IPostService postService) { _locationService = locationService; + _postService = postService; } [HttpPost("add")] @@ -74,5 +76,23 @@ namespace Api.Controllers return Ok(ret); } } + + [HttpGet("searchradius")] + [Authorize(Roles = "User")] + public async Task>> bestPostsForLocationInRadius(double latitude, double longitude, double radius) + { + Coords coords = new Coords(); + if (latitude != null && longitude != null) + { + coords.latitude = (double)latitude; + coords.longitude = (double)longitude; + } + List ret = new List(); + ret = await _postService.BestPostForAllLocationsInRadius(coords, radius); + if (ret != null && ret.Count > 0) + return Ok(ret); + return BadRequest(); + + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 69846b0..84d32ef 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -29,5 +29,6 @@ namespace Api.Interfaces Task> Recommended(string userid); Task addRemoveFavourite(string postId); Task> TrendingTags(); + Task> BestPostForAllLocationsInRadius(Coords coords, double radius); } } \ No newline at end of file diff --git a/Backend/Api/Api/Services/LocationService.cs b/Backend/Api/Api/Services/LocationService.cs index afb3b5b..55a52e7 100644 --- a/Backend/Api/Api/Services/LocationService.cs +++ b/Backend/Api/Api/Services/LocationService.cs @@ -99,5 +99,7 @@ namespace Api.Services } return tosend; } + + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 777389b..6f7c455 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -626,6 +626,31 @@ namespace Api.Services return tosend; } + + public async Task> BestPostForAllLocationsInRadius(Coords coords, double radius) + { + if (coords == null) + return null; + var lista = await _locations.Find(_ => true).ToListAsync(); + var inradius = new List(); + var tosend = new List(); + if (lista != null) + { + foreach (var elem in lista) + { + if (Math.Abs(elem.latitude - coords.latitude) < radius && Math.Abs(elem.longitude - coords.longitude) < radius) + inradius.Add(elem); + } + foreach (var elem in inradius) + { + var locposts = await SearchPosts(elem._id, 0, 1, 1); + var best = locposts.posts.Take(1).FirstOrDefault(); + if(best != null) + tosend.Add(best); + } + } + return tosend; + } } } -- cgit v1.2.3