Paging params in API docs

(cherry picked from commit bfaa7291e14a8d3847ef2154a52c363944560803)
pull/1889/head
Mark McDowall 1 year ago committed by Bogdan
parent bada5fe309
commit 34464160cb

@ -158,6 +158,8 @@ namespace NzbDrone.Host
{
{ apikeyQuery, Array.Empty<string>() }
});
c.DescribeAllParametersInCamelCase();
});
services

@ -93,7 +93,7 @@ namespace NzbDrone.Integration.Test.Client
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 @@ namespace NzbDrone.Integration.Test.Client
if (filterKey != null && filterValue != null)
{
request.AddParameter("filterKey", filterKey);
request.AddParameter("filterValue", filterValue);
request.AddParameter(filterKey, filterValue);
}
return Get<PagingResource<TResource>>(request);

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.History;
using Prowlarr.Http;
@ -21,30 +22,25 @@ namespace Prowlarr.Api.V1.History
[HttpGet]
[Produces("application/json")]
public PagingResource<HistoryResource> GetHistory()
public PagingResource<HistoryResource> GetHistory([FromQuery] PagingRequestResource paging, int? eventType, bool? successful, string downloadId)
{
var pagingResource = Request.ReadPagingResourceFromRequest<HistoryResource>();
var pagingResource = new PagingResource<HistoryResource>(paging);
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, NzbDrone.Core.History.History>("date", SortDirection.Descending);
var eventTypeFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "eventType");
var successfulFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "successful");
var downloadIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "downloadId");
if (eventTypeFilter != null)
if (eventType.HasValue)
{
var filterValue = (HistoryEventType)Convert.ToInt32(eventTypeFilter.Value);
var filterValue = (HistoryEventType)eventType.Value;
pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue);
}
if (successfulFilter != null)
if (successful.HasValue)
{
var filterValue = bool.Parse(successfulFilter.Value);
var filterValue = successful.Value;
pagingSpec.FilterExpressions.Add(v => v.Successful == filterValue);
}
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 Prowlarr.Http;
using Prowlarr.Http.Extensions;
@ -18,9 +18,9 @@ namespace Prowlarr.Api.V1.Logs
[HttpGet]
[Produces("application/json")]
public PagingResource<LogResource> GetLogs()
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")
@ -28,11 +28,9 @@ namespace Prowlarr.Api.V1.Logs
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");

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Http;
@ -27,69 +26,6 @@ namespace Prowlarr.Http.Extensions
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();
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)
{
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 Prowlarr.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 Prowlarr.Http
{
public class PagingResourceFilter
{
public string Key { get; set; }
public string Value { get; set; }
}
}
Loading…
Cancel
Save