From 2694e5d853c4ee9aacdd88cb98d2edf1f0983762 Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Fri, 19 Aug 2022 17:48:46 +0200 Subject: [PATCH] feature: add endpoint to get all series within a filter mostly useful for debugging. --- .../API/v3/Controllers/TreeController.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Shoko.Server/API/v3/Controllers/TreeController.cs b/Shoko.Server/API/v3/Controllers/TreeController.cs index 3510f0be7..66f623d04 100644 --- a/Shoko.Server/API/v3/Controllers/TreeController.cs +++ b/Shoko.Server/API/v3/Controllers/TreeController.cs @@ -99,6 +99,49 @@ public ActionResult> GetFilteredGroups([FromRoute] int filterI .ToListResult(group => new Group(HttpContext, group, randomImages), page, pageSize); } + /// + /// Get a paginated list of all the within a . + /// + /// + /// Pass a of 0 to disable filter. + /// + /// ID + /// The page size. Set to 0 to disable pagination. + /// The page index. + /// Randomise images shown for each . + /// + [HttpGet("Filter/{filterID}/Series")] + public ActionResult> GetSeriesInFilteredGroup([FromRoute] int filterID, [FromQuery] [Range(0, 100)] int pageSize = 50, [FromQuery] [Range(1, int.MaxValue)] int page = 1, [FromQuery] bool randomImages = false) + { + // Return the series with no group filter applied. + if (filterID == 0) + return RepoFactory.AnimeSeries.GetAll() + .Where(series => User.AllowedSeries(series)) + .OrderBy(series => series.GetSeriesName().ToLowerInvariant()) + .ToListResult(series => new Series(HttpContext, series, randomImages), page, pageSize); + + // Check if the group filter exists. + var groupFilter = RepoFactory.GroupFilter.GetByID(filterID); + if (groupFilter == null) + return NotFound(FilterController.FilterNotFound); + + // Return all series if group filter is not applied to series. + if (groupFilter.ApplyToSeries != 1) + return RepoFactory.AnimeSeries.GetAll() + .Where(series => User.AllowedSeries(series)) + .OrderBy(series => series.GetSeriesName().ToLowerInvariant()) + .ToListResult(series => new Series(HttpContext, series, randomImages), page, pageSize); + + // Return early if every series will be filtered out. + if (!groupFilter.SeriesIds.TryGetValue(User.JMMUserID, out var seriesIDs)) + return new ListResult(); + + return seriesIDs.Select(id => RepoFactory.AnimeSeries.GetByID(id)) + .Where(series => series != null && User.AllowedSeries(series)) + .OrderBy(series => series.GetSeriesName().ToLowerInvariant()) + .ToListResult(series => new Series(HttpContext, series, randomImages), page, pageSize); + } + /// /// Get a list of all the sub-s belonging to the with the given and which are present within the with the given . ///