diff options
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 10 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 27 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 73 |
4 files changed, 110 insertions, 1 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 9168462..c4156f0 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -199,12 +199,20 @@ namespace Api.Controllers { return Ok(await _postService.TrendingTags()); } + [HttpGet("userFavouritePosts")] [Authorize(Roles = "User")] public async Task<ActionResult<List<PostSend>>> getUserFavouritePosts() { return Ok(await _postService.userFavouritePosts()); } + + [HttpGet("posts/getAllPostsFilterSort")] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<PostSend>>> GetAllPostsFilterSort([FromBody] FilterSort fs) + { + return Ok(await _postService.GetAllPostsFilterSort(fs)); + } - } + } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index aafc61a..919f3cf 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -32,5 +32,6 @@ namespace Api.Interfaces Task<List<PostSend>> BestPostForAllLocationsInRadius(Coords coords, double radius); Task<UserStats> UserStats(string userid); Task<List<PostSend>> userFavouritePosts(); + Task<List<PostSend>> GetAllPostsFilterSort(FilterSort fs); } }
\ No newline at end of file diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 9c0c429..955431f 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -123,4 +123,31 @@ namespace Api.Models public TagR tagr { get; set; } public PostSendPage page { get; set; } } + + public class FilterSort + { + public List<PostSend> posts { get; set; } + public bool sort { get; set; } + public bool filter { get; set; } + public bool filterDate { get; set; } + public bool filterRating { get; set; } + public bool filterViews{ get; set; } + public DateTime filterDateFrom { get; set; } + public DateTime filterDateTo { get; set; } + public int filterRatingFrom { get; set; } + public int filterRatingTo { get; set; } + public int filterViewsFrom { get; set; } + public int filterViewsTo { get; set; } + + public bool sortLatest { get; set; } + public bool sortOldest { get; set; } + public bool sortBest{ get; set; } + public bool sortMostViews{ get; set; } + + + + + + + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 7ba5401..e6fa9fb 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -4,6 +4,7 @@ using Api.Models; using MongoDB.Driver; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; +using System.Diagnostics; namespace Api.Services { @@ -726,6 +727,78 @@ namespace Api.Services return posts; } + public async Task<List<PostSend>> GetAllPostsFilterSort(FilterSort fs) + { + List<PostSend> allPosts = fs.posts; + List<PostSend> filteredPosts = allPosts; + List<PostSend> fsPosts=allPosts; + + if (fs.filter) + { + if (fs.filterRatingFrom>=0) + { + filteredPosts =filteredPosts.FindAll(post => post.ratings >fs.filterRatingFrom); + } + if (fs.filterRatingTo >= 0) + { + filteredPosts = filteredPosts.FindAll(post => post.ratings < fs.filterRatingTo); + } + if(fs.filterViewsFrom >= 0) + { + filteredPosts = filteredPosts.FindAll(post => post.views > fs.filterViewsFrom); + } + if(fs.filterViewsTo >= 0) + { + filteredPosts = filteredPosts.FindAll(post => post.views < fs.filterViewsTo); + } + if(fs.filterDateFrom!=null) + { + filteredPosts = filteredPosts.FindAll(post => post.createdAt > fs.filterDateFrom); + } + if(fs.filterDateTo!=null) + { + filteredPosts = filteredPosts.FindAll(post => post.createdAt < fs.filterDateTo); + } + } + + if (fs.sort) + { + if (fs.sortBest) + { + fsPosts = filteredPosts.OrderByDescending(o => o.ratings).ToList(); + } + if (fs.sortLatest) + { + fsPosts = filteredPosts.OrderByDescending(o => o.createdAt).ToList(); + } + if (fs.sortMostViews) + { + fsPosts = filteredPosts.OrderByDescending(o => o.views).ToList(); + } + if (fs.sortOldest) + { + fsPosts = filteredPosts.OrderBy(p => p.createdAt).ToList(); + } + } + + if(!fs.filter && !fs.sort) + { + return allPosts; + } + else if(!fs.filter && fs.sort) + { + return fsPosts; + } + else if(fs.filter && !fs.sort) + { + return filteredPosts; + } + else + { + return filteredPosts; + } + + } } } |