1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-19 17:32:38 +01:00

Paging params in API docs

(cherry picked from commit bfaa7291e14a8d3847ef2154a52c363944560803)

Closes #9248
This commit is contained in:
Mark McDowall 2023-10-09 20:37:31 -07:00 committed by Bogdan
parent 8cb6295ddc
commit a01328dc8c
8 changed files with 45 additions and 99 deletions

View File

@ -159,6 +159,8 @@ public void ConfigureServices(IServiceCollection services)
{ {
{ apikeyQuery, Array.Empty<string>() } { apikeyQuery, Array.Empty<string>() }
}); });
c.DescribeAllParametersInCamelCase();
}); });
services services

View File

@ -102,7 +102,7 @@ public List<TResource> All(Dictionary<string, object> queryParams = null)
return Get<List<TResource>>(request); return Get<List<TResource>>(request);
} }
public PagingResource<TResource> GetPaged(int pageNumber, int pageSize, string sortKey, string sortDir, string filterKey = null, string filterValue = null) public PagingResource<TResource> GetPaged(int pageNumber, int pageSize, string sortKey, string sortDir, string filterKey = null, object filterValue = null)
{ {
var request = BuildRequest(); var request = BuildRequest();
request.AddParameter("page", pageNumber); request.AddParameter("page", pageNumber);
@ -112,8 +112,7 @@ public PagingResource<TResource> GetPaged(int pageNumber, int pageSize, string s
if (filterKey != null && filterValue != null) if (filterKey != null && filterValue != null)
{ {
request.AddParameter("filterKey", filterKey); request.AddParameter(filterKey, filterValue);
request.AddParameter("filterValue", filterValue);
} }
return Get<PagingResource<TResource>>(request); return Get<PagingResource<TResource>>(request);

View File

@ -25,9 +25,9 @@ public BlocklistController(IBlocklistService blocklistService,
[HttpGet] [HttpGet]
[Produces("application/json")] [Produces("application/json")]
public PagingResource<BlocklistResource> GetBlocklist() public PagingResource<BlocklistResource> GetBlocklist([FromQuery] PagingRequestResource paging)
{ {
var pagingResource = Request.ReadPagingResourceFromRequest<BlocklistResource>(); var pagingResource = new PagingResource<BlocklistResource>(paging);
var pagingSpec = pagingResource.MapToPagingSpec<BlocklistResource, NzbDrone.Core.Blocklisting.Blocklist>("date", SortDirection.Descending); var pagingSpec = pagingResource.MapToPagingSpec<BlocklistResource, NzbDrone.Core.Blocklisting.Blocklist>("date", SortDirection.Descending);
return pagingSpec.ApplyToPage(_blocklistService.Paged, model => BlocklistResourceMapper.MapToResource(model, _formatCalculator)); return pagingSpec.ApplyToPage(_blocklistService.Paged, model => BlocklistResourceMapper.MapToResource(model, _formatCalculator));

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.CustomFormats; using NzbDrone.Core.CustomFormats;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.DecisionEngine.Specifications; using NzbDrone.Core.DecisionEngine.Specifications;
@ -59,23 +60,20 @@ protected HistoryResource MapToResource(MovieHistory model, bool includeMovie)
} }
[HttpGet] [HttpGet]
public PagingResource<HistoryResource> GetHistory(bool includeMovie) [Produces("application/json")]
public PagingResource<HistoryResource> GetHistory([FromQuery] PagingRequestResource paging, bool includeMovie, int? eventType, string downloadId)
{ {
var pagingResource = Request.ReadPagingResourceFromRequest<HistoryResource>(); var pagingResource = new PagingResource<HistoryResource>(paging);
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, MovieHistory>("date", SortDirection.Descending); var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, MovieHistory>("date", SortDirection.Descending);
var eventTypeFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "eventType"); if (eventType.HasValue)
var downloadIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "downloadId");
if (eventTypeFilter != null)
{ {
var filterValue = (MovieHistoryEventType)Convert.ToInt32(eventTypeFilter.Value); var filterValue = (MovieHistoryEventType)eventType.Value;
pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue); pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue);
} }
if (downloadIdFilter != null) if (downloadId.IsNotNullOrWhiteSpace())
{ {
var downloadId = downloadIdFilter.Value;
pagingSpec.FilterExpressions.Add(h => h.DownloadId == downloadId); pagingSpec.FilterExpressions.Add(h => h.DownloadId == downloadId);
} }

View File

@ -1,5 +1,5 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Instrumentation;
using Radarr.Http; using Radarr.Http;
using Radarr.Http.Extensions; using Radarr.Http.Extensions;
@ -17,9 +17,10 @@ public LogController(ILogService logService)
} }
[HttpGet] [HttpGet]
public PagingResource<LogResource> GetLogs() [Produces("application/json")]
public PagingResource<LogResource> GetLogs([FromQuery] PagingRequestResource paging, string level)
{ {
var pagingResource = Request.ReadPagingResourceFromRequest<LogResource>(); var pagingResource = new PagingResource<LogResource>(paging);
var pageSpec = pagingResource.MapToPagingSpec<LogResource, Log>(); var pageSpec = pagingResource.MapToPagingSpec<LogResource, Log>();
if (pageSpec.SortKey == "time") if (pageSpec.SortKey == "time")
@ -27,11 +28,9 @@ public PagingResource<LogResource> GetLogs()
pageSpec.SortKey = "id"; pageSpec.SortKey = "id";
} }
var levelFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "level"); if (level.IsNotNullOrWhiteSpace())
if (levelFilter != null)
{ {
switch (levelFilter.Value) switch (level)
{ {
case "fatal": case "fatal":
pageSpec.FilterExpressions.Add(h => h.Level == "Fatal"); pageSpec.FilterExpressions.Add(h => h.Level == "Fatal");

View File

@ -129,9 +129,10 @@ public object RemoveMany([FromBody] QueueBulkResource resource, [FromQuery] bool
} }
[HttpGet] [HttpGet]
public PagingResource<QueueResource> GetQueue(bool includeUnknownMovieItems = false, bool includeMovie = false) [Produces("application/json")]
public PagingResource<QueueResource> GetQueue([FromQuery] PagingRequestResource paging, bool includeUnknownMovieItems = false, bool includeMovie = false)
{ {
var pagingResource = Request.ReadPagingResourceFromRequest<QueueResource>(); var pagingResource = new PagingResource<QueueResource>(paging);
var pagingSpec = pagingResource.MapToPagingSpec<QueueResource, NzbDrone.Core.Queue.Queue>("timeleft", SortDirection.Ascending); var pagingSpec = pagingResource.MapToPagingSpec<QueueResource, NzbDrone.Core.Queue.Queue>("timeleft", SortDirection.Ascending);
return pagingSpec.ApplyToPage((spec) => GetQueue(spec, includeUnknownMovieItems), (q) => MapToResource(q, includeMovie)); return pagingSpec.ApplyToPage((spec) => GetQueue(spec, includeUnknownMovieItems), (q) => MapToResource(q, includeMovie));

View File

@ -4,7 +4,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Exceptions;
namespace Radarr.Http.Extensions namespace Radarr.Http.Extensions
{ {
@ -52,80 +51,6 @@ public static bool GetBooleanQueryParameter(this HttpRequest request, string par
return defaultValue; return defaultValue;
} }
public static PagingResource<TResource> ReadPagingResourceFromRequest<TResource>(this HttpRequest request)
{
if (!int.TryParse(request.Query["PageSize"].ToString(), out var pageSize))
{
pageSize = 10;
}
if (!int.TryParse(request.Query["Page"].ToString(), out var page))
{
page = 1;
}
var pagingResource = new PagingResource<TResource>
{
PageSize = pageSize,
Page = page,
Filters = new List<PagingResourceFilter>()
};
if (request.Query["SortKey"].Any())
{
var sortKey = request.Query["SortKey"].ToString();
if (!VALID_SORT_KEYS.Contains(sortKey) &&
!TableMapping.Mapper.IsValidSortKey(sortKey))
{
throw new BadRequestException($"Invalid sort key {sortKey}");
}
pagingResource.SortKey = sortKey;
if (request.Query["SortDirection"].Any())
{
pagingResource.SortDirection = request.Query["SortDirection"].ToString()
.Equals("ascending", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
}
}
// For backwards compatibility with v2
if (request.Query["FilterKey"].Any())
{
var filter = new PagingResourceFilter
{
Key = request.Query["FilterKey"].ToString()
};
if (request.Query["FilterValue"].Any())
{
filter.Value = request.Query["FilterValue"].ToString();
}
pagingResource.Filters.Add(filter);
}
// v3 uses filters in key=value format
foreach (var pair in request.Query)
{
if (EXCLUDED_KEYS.Contains(pair.Key))
{
continue;
}
pagingResource.Filters.Add(new PagingResourceFilter
{
Key = pair.Key,
Value = pair.Value.ToString()
});
}
return pagingResource;
}
public static PagingResource<TResource> ApplyToPage<TResource, TModel>(this PagingSpec<TModel> pagingSpec, Func<PagingSpec<TModel>, PagingSpec<TModel>> function, Converter<TModel, TResource> mapper) public static PagingResource<TResource> ApplyToPage<TResource, TModel>(this PagingSpec<TModel> pagingSpec, Func<PagingSpec<TModel>, PagingSpec<TModel>> function, Converter<TModel, TResource> mapper)
{ {
pagingSpec = function(pagingSpec); pagingSpec = function(pagingSpec);

View File

@ -1,17 +1,39 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
namespace Radarr.Http namespace Radarr.Http
{ {
public class PagingRequestResource
{
[DefaultValue(1)]
public int? Page { get; set; }
[DefaultValue(10)]
public int? PageSize { get; set; }
public string SortKey { get; set; }
public SortDirection? SortDirection { get; set; }
}
public class PagingResource<TResource> public class PagingResource<TResource>
{ {
public int Page { get; set; } public int Page { get; set; }
public int PageSize { get; set; } public int PageSize { get; set; }
public string SortKey { get; set; } public string SortKey { get; set; }
public SortDirection SortDirection { get; set; } public SortDirection SortDirection { get; set; }
public List<PagingResourceFilter> Filters { get; set; }
public int TotalRecords { get; set; } public int TotalRecords { get; set; }
public List<TResource> Records { get; set; } public List<TResource> Records { get; set; }
public PagingResource()
{
}
public PagingResource(PagingRequestResource requestResource)
{
Page = requestResource.Page ?? 1;
PageSize = requestResource.PageSize ?? 10;
SortKey = requestResource.SortKey;
SortDirection = requestResource.SortDirection ?? SortDirection.Descending;
}
} }
public static class PagingResourceMapper public static class PagingResourceMapper