mirror of
https://github.com/Readarr/Readarr
synced 2026-02-24 15:43:21 +01:00
Paging params in API docs
(cherry picked from commit bfaa7291e14a8d3847ef2154a52c363944560803) Closes #2975 Closes #2991
This commit is contained in:
parent
9d27c172ac
commit
9e3dfc510d
13 changed files with 56 additions and 125 deletions
|
|
@ -158,6 +158,8 @@ public void ConfigureServices(IServiceCollection services)
|
|||
{
|
||||
{ apikeyQuery, Array.Empty<string>() }
|
||||
});
|
||||
|
||||
c.DescribeAllParametersInCamelCase();
|
||||
});
|
||||
|
||||
services
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ public void cutoff_should_have_unmonitored_items()
|
|||
var author = EnsureAuthor("14586394", "43765115", "Andrew Hunter Murray", false);
|
||||
EnsureBookFile(author, 1, "43765115", Quality.MOBI);
|
||||
|
||||
var result = WantedCutoffUnmet.GetPaged(0, 15, "releaseDate", "desc", "monitored", "false");
|
||||
var result = WantedCutoffUnmet.GetPaged(0, 15, "releaseDate", "desc", "monitored", false);
|
||||
|
||||
result.Records.Should().NotBeEmpty();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ public void missing_should_have_unmonitored_items()
|
|||
{
|
||||
EnsureAuthor("14586394", "43765115", "Andrew Hunter Murray", false);
|
||||
|
||||
var result = WantedMissing.GetPaged(0, 15, "releaseDate", "desc", "monitored", "false");
|
||||
var result = WantedMissing.GetPaged(0, 15, "releaseDate", "desc", "monitored", false);
|
||||
|
||||
result.Records.Should().NotBeEmpty();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public List<TResource> All()
|
|||
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();
|
||||
request.AddParameter("page", pageNumber);
|
||||
|
|
@ -103,8 +103,7 @@ public PagingResource<TResource> GetPaged(int pageNumber, int pageSize, string s
|
|||
|
||||
if (filterKey != null && filterValue != null)
|
||||
{
|
||||
request.AddParameter("filterKey", filterKey);
|
||||
request.AddParameter("filterValue", filterValue);
|
||||
request.AddParameter(filterKey, filterValue);
|
||||
}
|
||||
|
||||
return Get<PagingResource<TResource>>(request);
|
||||
|
|
|
|||
|
|
@ -22,9 +22,10 @@ public BlocklistController(IBlocklistService blocklistService,
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public PagingResource<BlocklistResource> GetBlocklist()
|
||||
[Produces("application/json")]
|
||||
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);
|
||||
|
||||
return pagingSpec.ApplyToPage(_blocklistService.Paged, model => BlocklistResourceMapper.MapToResource(model, _formatCalculator));
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.CustomFormats;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
|
@ -60,30 +61,25 @@ protected HistoryResource MapToResource(EntityHistory model, bool includeAuthor,
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public PagingResource<HistoryResource> GetHistory(bool includeAuthor = false, bool includeBook = false)
|
||||
[Produces("application/json")]
|
||||
public PagingResource<HistoryResource> GetHistory([FromQuery] PagingRequestResource paging, bool includeAuthor, bool includeBook, [FromQuery(Name = "eventType")] int[] eventTypes, int? bookId, string downloadId)
|
||||
{
|
||||
var pagingResource = Request.ReadPagingResourceFromRequest<HistoryResource>();
|
||||
var pagingResource = new PagingResource<HistoryResource>(paging);
|
||||
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, EntityHistory>("date", SortDirection.Descending);
|
||||
|
||||
var eventTypeFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "eventType");
|
||||
var bookIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "bookId");
|
||||
var downloadIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "downloadId");
|
||||
|
||||
if (eventTypeFilter != null)
|
||||
if (eventTypes != null && eventTypes.Any())
|
||||
{
|
||||
var filterValue = (EntityHistoryEventType)Convert.ToInt32(eventTypeFilter.Value);
|
||||
pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue);
|
||||
var filterValues = eventTypes.Cast<EntityHistoryEventType>().ToArray();
|
||||
pagingSpec.FilterExpressions.Add(v => filterValues.Contains(v.EventType));
|
||||
}
|
||||
|
||||
if (bookIdFilter != null)
|
||||
if (bookId.HasValue)
|
||||
{
|
||||
var bookId = Convert.ToInt32(bookIdFilter.Value);
|
||||
pagingSpec.FilterExpressions.Add(h => h.BookId == bookId);
|
||||
}
|
||||
|
||||
if (downloadIdFilter != null)
|
||||
if (downloadId.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
var downloadId = downloadIdFilter.Value;
|
||||
pagingSpec.FilterExpressions.Add(h => h.DownloadId == downloadId);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Instrumentation;
|
||||
using Readarr.Http;
|
||||
using Readarr.Http.Extensions;
|
||||
|
|
@ -17,9 +17,10 @@ public LogController(ILogService logService)
|
|||
}
|
||||
|
||||
[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>();
|
||||
|
||||
if (pageSpec.SortKey == "time")
|
||||
|
|
@ -27,11 +28,9 @@ public PagingResource<LogResource> GetLogs()
|
|||
pageSpec.SortKey = "id";
|
||||
}
|
||||
|
||||
var levelFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "level");
|
||||
|
||||
if (levelFilter != null)
|
||||
if (level.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
switch (levelFilter.Value)
|
||||
switch (level)
|
||||
{
|
||||
case "fatal":
|
||||
pageSpec.FilterExpressions.Add(h => h.Level == "Fatal");
|
||||
|
|
|
|||
|
|
@ -100,9 +100,10 @@ public object RemoveMany([FromBody] QueueBulkResource resource, [FromQuery] bool
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public PagingResource<QueueResource> GetQueue(bool includeUnknownAuthorItems = false, bool includeAuthor = false, bool includeBook = false)
|
||||
[Produces("application/json")]
|
||||
public PagingResource<QueueResource> GetQueue([FromQuery] PagingRequestResource paging, bool includeUnknownAuthorItems = false, bool includeAuthor = false, bool includeBook = false)
|
||||
{
|
||||
var pagingResource = Request.ReadPagingResourceFromRequest<QueueResource>();
|
||||
var pagingResource = new PagingResource<QueueResource>(paging);
|
||||
var pagingSpec = pagingResource.MapToPagingSpec<QueueResource, NzbDrone.Core.Queue.Queue>("timeleft", SortDirection.Ascending);
|
||||
|
||||
return pagingSpec.ApplyToPage((spec) => GetQueue(spec, includeUnknownAuthorItems), (q) => MapToResource(q, includeAuthor, includeBook));
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Core.AuthorStats;
|
||||
using NzbDrone.Core.Books;
|
||||
|
|
@ -30,9 +29,9 @@ public CutoffController(IBookCutoffService bookCutoffService,
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public PagingResource<BookResource> GetCutoffUnmetBooks(bool includeAuthor = false)
|
||||
public PagingResource<BookResource> GetCutoffUnmetBooks([FromQuery] PagingRequestResource paging, bool includeAuthor = false, bool monitored = true)
|
||||
{
|
||||
var pagingResource = Request.ReadPagingResourceFromRequest<BookResource>();
|
||||
var pagingResource = new PagingResource<BookResource>(paging);
|
||||
var pagingSpec = new PagingSpec<Book>
|
||||
{
|
||||
Page = pagingResource.Page,
|
||||
|
|
@ -41,9 +40,7 @@ public PagingResource<BookResource> GetCutoffUnmetBooks(bool includeAuthor = fal
|
|||
SortDirection = pagingResource.SortDirection
|
||||
};
|
||||
|
||||
var filter = pagingResource.Filters.FirstOrDefault(f => f.Key == "monitored");
|
||||
|
||||
if (filter != null && filter.Value == "false")
|
||||
if (monitored)
|
||||
{
|
||||
pagingSpec.FilterExpressions.Add(v => v.Monitored == false || v.Author.Value.Monitored == false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Core.AuthorStats;
|
||||
using NzbDrone.Core.Books;
|
||||
|
|
@ -26,9 +25,9 @@ public MissingController(IBookService bookService,
|
|||
}
|
||||
|
||||
[HttpGet]
|
||||
public PagingResource<BookResource> GetMissingBooks(bool includeAuthor = false)
|
||||
public PagingResource<BookResource> GetMissingBooks([FromQuery] PagingRequestResource paging, bool includeAuthor = false, bool monitored = true)
|
||||
{
|
||||
var pagingResource = Request.ReadPagingResourceFromRequest<BookResource>();
|
||||
var pagingResource = new PagingResource<BookResource>(paging);
|
||||
var pagingSpec = new PagingSpec<Book>
|
||||
{
|
||||
Page = pagingResource.Page,
|
||||
|
|
@ -37,9 +36,7 @@ public PagingResource<BookResource> GetMissingBooks(bool includeAuthor = false)
|
|||
SortDirection = pagingResource.SortDirection
|
||||
};
|
||||
|
||||
var monitoredFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "monitored");
|
||||
|
||||
if (monitoredFilter != null && monitoredFilter.Value == "false")
|
||||
if (monitored)
|
||||
{
|
||||
pagingSpec.FilterExpressions.Add(v => v.Monitored == false || v.Author.Value.Monitored == false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
|
||||
namespace Readarr.Http.Extensions
|
||||
{
|
||||
|
|
@ -52,80 +51,6 @@ public static bool GetBooleanQueryParameter(this HttpRequest request, string par
|
|||
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)
|
||||
{
|
||||
pagingSpec = function(pagingSpec);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,39 @@
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace Readarr.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 int Page { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public string SortKey { get; set; }
|
||||
public SortDirection SortDirection { get; set; }
|
||||
public List<PagingResourceFilter> Filters { get; set; }
|
||||
public int TotalRecords { 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
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
namespace Readarr.Http
|
||||
{
|
||||
public class PagingResourceFilter
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue