Fixed api blacklist, history, wanted/missing and wanted/cutoff requiring sortKey, now defaulting to an appropriate value (airDateUtc/date desc for most).

fixes #1452
This commit is contained in:
Taloth Saldono 2016-09-10 23:05:34 +02:00
parent 4bf3ef45b0
commit 713e109bc9
7 changed files with 40 additions and 38 deletions

View File

@ -17,13 +17,7 @@ namespace NzbDrone.Api.Blacklist
private PagingResource<BlacklistResource> GetBlacklist(PagingResource<BlacklistResource> pagingResource) private PagingResource<BlacklistResource> GetBlacklist(PagingResource<BlacklistResource> pagingResource)
{ {
var pagingSpec = new PagingSpec<Core.Blacklisting.Blacklist> var pagingSpec = pagingResource.MapToPagingSpec<BlacklistResource, Core.Blacklisting.Blacklist>("id", SortDirection.Ascending);
{
Page = pagingResource.Page,
PageSize = pagingResource.PageSize,
SortKey = pagingResource.SortKey,
SortDirection = pagingResource.SortDirection
};
return ApplyToPage(_blacklistService.Paged, pagingSpec, BlacklistResourceMapper.MapToResource); return ApplyToPage(_blacklistService.Paged, pagingSpec, BlacklistResourceMapper.MapToResource);
} }
@ -33,4 +27,4 @@ namespace NzbDrone.Api.Blacklist
_blacklistService.Delete(id); _blacklistService.Delete(id);
} }
} }
} }

View File

@ -47,13 +47,7 @@ namespace NzbDrone.Api.History
{ {
var episodeId = Request.Query.EpisodeId; var episodeId = Request.Query.EpisodeId;
var pagingSpec = new PagingSpec<Core.History.History> var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, Core.History.History>("date", SortDirection.Descending);
{
Page = pagingResource.Page,
PageSize = pagingResource.PageSize,
SortKey = pagingResource.SortKey,
SortDirection = pagingResource.SortDirection
};
if (pagingResource.FilterKey == "eventType") if (pagingResource.FilterKey == "eventType")
{ {
@ -77,4 +71,4 @@ namespace NzbDrone.Api.History
return new object().AsResponse(); return new object().AsResponse();
} }
} }
} }

View File

@ -17,15 +17,26 @@ namespace NzbDrone.Api
public static class PagingResourceMapper public static class PagingResourceMapper
{ {
public static PagingSpec<TModel> MapToPagingSpec<TResource, TModel>(this PagingResource<TResource> pagingSpec) public static PagingSpec<TModel> MapToPagingSpec<TResource, TModel>(this PagingResource<TResource> pagingResource, string defaultSortKey = "Id", SortDirection defaultSortDirection = SortDirection.Ascending)
{ {
return new PagingSpec<TModel> var pagingSpec = new PagingSpec<TModel>
{ {
Page = pagingSpec.Page, Page = pagingResource.Page,
PageSize = pagingSpec.PageSize, PageSize = pagingResource.PageSize,
SortKey = pagingSpec.SortKey, SortKey = pagingResource.SortKey,
SortDirection = pagingSpec.SortDirection, SortDirection = pagingResource.SortDirection,
}; };
if (pagingResource.SortKey == null)
{
pagingSpec.SortKey = defaultSortKey;
if(pagingResource.SortDirection == SortDirection.Default)
{
pagingSpec.SortDirection = defaultSortDirection;
}
}
return pagingSpec;
} }
} }
} }

View File

@ -23,13 +23,7 @@ namespace NzbDrone.Api.Wanted
private PagingResource<EpisodeResource> GetCutoffUnmetEpisodes(PagingResource<EpisodeResource> pagingResource) private PagingResource<EpisodeResource> GetCutoffUnmetEpisodes(PagingResource<EpisodeResource> pagingResource)
{ {
var pagingSpec = new PagingSpec<Episode> var pagingSpec = pagingResource.MapToPagingSpec<EpisodeResource, Episode>("airDateUtc", SortDirection.Descending);
{
Page = pagingResource.Page,
PageSize = pagingResource.PageSize,
SortKey = pagingResource.SortKey,
SortDirection = pagingResource.SortDirection
};
if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false") if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false")
{ {
@ -45,4 +39,4 @@ namespace NzbDrone.Api.Wanted
return resource; return resource;
} }
} }
} }

View File

@ -19,13 +19,7 @@ namespace NzbDrone.Api.Wanted
private PagingResource<EpisodeResource> GetMissingEpisodes(PagingResource<EpisodeResource> pagingResource) private PagingResource<EpisodeResource> GetMissingEpisodes(PagingResource<EpisodeResource> pagingResource)
{ {
var pagingSpec = new PagingSpec<Episode> var pagingSpec = pagingResource.MapToPagingSpec<EpisodeResource, Episode>("airDateUtc", SortDirection.Descending);
{
Page = pagingResource.Page,
PageSize = pagingResource.PageSize,
SortKey = pagingResource.SortKey,
SortDirection = pagingResource.SortDirection
};
if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false") if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false")
{ {
@ -41,4 +35,4 @@ namespace NzbDrone.Api.Wanted
return resource; return resource;
} }
} }
} }

View File

@ -8,6 +8,20 @@ namespace NzbDrone.Core.Test.Datastore.PagingSpecExtensionsTests
{ {
public class ToSortDirectionFixture public class ToSortDirectionFixture
{ {
[Test]
public void should_convert_default_to_asc()
{
var pagingSpec = new PagingSpec<Episode>
{
Page = 1,
PageSize = 10,
SortDirection = SortDirection.Default,
SortKey = "AirDate"
};
pagingSpec.ToSortDirection().Should().Be(Marr.Data.QGen.SortDirection.Asc);
}
[Test] [Test]
public void should_convert_ascending_to_asc() public void should_convert_ascending_to_asc()
{ {

View File

@ -17,6 +17,7 @@ namespace NzbDrone.Core.Datastore
public enum SortDirection public enum SortDirection
{ {
Default,
Ascending, Ascending,
Descending Descending
} }