1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Moved Missing and History to Fancy

This commit is contained in:
Mark McDowall 2013-05-12 23:12:19 -07:00
parent 2f4ccff0a2
commit 9d96df9c2e
10 changed files with 121 additions and 80 deletions

View File

@ -13,48 +13,27 @@
namespace NzbDrone.Api.History namespace NzbDrone.Api.History
{ {
public class HistoryModule : NzbDroneApiModule public class HistoryModule : NzbDroneRestModule<HistoryResource>
{ {
private readonly IHistoryService _historyService; private readonly IHistoryService _historyService;
public HistoryModule(IHistoryService historyService) public HistoryModule(IHistoryService historyService)
: base("/history")
{ {
_historyService = historyService; _historyService = historyService;
Get["/"] = x => GetHistory(); GetResourcePaged = GetHistory;
} }
private Response GetHistory() private PagingResource<HistoryResource> GetHistory(PagingResource<HistoryResource> pagingResource)
{ {
//TODO: common page parsing logic should be done somewhere else
int pageSize;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.PageSize), out pageSize);
if (pageSize == 0) pageSize = 20;
int page;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page);
if (page == 0) page = 1;
var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey);
if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate";
var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir)
.Equals("Asc", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
var pagingSpec = new PagingSpec<Core.History.History> var pagingSpec = new PagingSpec<Core.History.History>
{ {
Page = page, Page = pagingResource.Page,
PageSize = pageSize, PageSize = pagingResource.PageSize,
SortKey = sortKey, SortKey = pagingResource.SortKey,
SortDirection = sortDirection SortDirection = pagingResource.SortDirection
}; };
var result = _historyService.Paged(pagingSpec); return ApplyToPage(_historyService.Paged, pagingSpec);
return Mapper.Map<PagingSpec<Core.History.History>, PagingResource<HistoryResource>>(result).AsResponse();
} }
} }
} }

View File

@ -1,59 +1,29 @@
using System; using NzbDrone.Core.Datastore;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using AutoMapper;
using Nancy;
using NzbDrone.Api.Episodes;
using NzbDrone.Api.Extensions;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Api.Missing namespace NzbDrone.Api.Missing
{ {
public class MissingModule : NzbDroneApiModule public class MissingModule : NzbDroneRestModule<MissingResource>
{ {
private readonly IEpisodeService _episodeService; private readonly IEpisodeService _episodeService;
public MissingModule(IEpisodeService episodeService) public MissingModule(IEpisodeService episodeService)
: base("/missing")
{ {
_episodeService = episodeService; _episodeService = episodeService;
Get["/"] = x => GetMissingEpisodes(); GetResourcePaged = GetMissingEpisodes;
} }
private Response GetMissingEpisodes() private PagingResource<MissingResource> GetMissingEpisodes(PagingResource<MissingResource> pagingResource)
{ {
bool includeSpecials;
Boolean.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.IncludeSpecials), out includeSpecials);
int pageSize;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.PageSize), out pageSize);
if (pageSize == 0) pageSize = 20;
int page;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page);
if (page == 0) page = 1;
var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey);
if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate";
var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir)
.Equals("Asc", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
var pagingSpec = new PagingSpec<Episode> var pagingSpec = new PagingSpec<Episode>
{ {
Page = page, Page = pagingResource.Page,
PageSize = pageSize, PageSize = pagingResource.PageSize,
SortKey = sortKey, SortKey = pagingResource.SortKey,
SortDirection = sortDirection SortDirection = pagingResource.SortDirection
}; };
var result = _episodeService.EpisodesWithoutFiles(pagingSpec, includeSpecials); return ApplyToPage(_episodeService.EpisodesWithoutFiles, pagingSpec);
return Mapper.Map<PagingSpec<Episode>, PagingResource<EpisodeResource>>(result).AsResponse();
} }
} }
} }

View File

@ -0,0 +1,34 @@
using System;
using NzbDrone.Api.REST;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Model;
using NzbDrone.Core.Tv;
namespace NzbDrone.Api.Missing
{
public class MissingResource : RestResource
{
public Int32 Id { get; set; }
public Int32 SeriesId { get; set; }
public Int32 EpisodeFileId { get; set; }
public Int32 SeasonNumber { get; set; }
public Int32 EpisodeNumber { get; set; }
public String Title { get; set; }
public DateTime? AirDate { get; set; }
public EpisodeStatuses Status { get; set; }
public String Overview { get; set; }
public EpisodeFile EpisodeFile { get; set; }
public Boolean HasFile { get; set; }
public Boolean Ignored { get; set; }
public Int32 SceneEpisodeNumber { get; set; }
public Int32 SceneSeasonNumber { get; set; }
public Int32 TvDbEpisodeId { get; set; }
public Int32? AbsoluteEpisodeNumber { get; set; }
public DateTime? EndTime { get; set; }
public DateTime? GrabDate { get; set; }
public PostDownloadStatusType PostDownloadStatus { get; set; }
public Core.Tv.Series Series { get; set; }
public String SeriesTitle { get; set; }
}
}

View File

@ -109,6 +109,7 @@
<Compile Include="Mapping\MappingValidation.cs" /> <Compile Include="Mapping\MappingValidation.cs" />
<Compile Include="Mapping\ResourceMappingException.cs" /> <Compile Include="Mapping\ResourceMappingException.cs" />
<Compile Include="Mapping\ValueInjectorExtensions.cs" /> <Compile Include="Mapping\ValueInjectorExtensions.cs" />
<Compile Include="Missing\MissingResource.cs" />
<Compile Include="Missing\MissingModule.cs" /> <Compile Include="Missing\MissingModule.cs" />
<Compile Include="NzbDroneRestModule.cs" /> <Compile Include="NzbDroneRestModule.cs" />
<Compile Include="PagingResource.cs" /> <Compile Include="PagingResource.cs" />

View File

@ -48,5 +48,19 @@ protected NzbDroneRestModule(string resource)
return model.InjectTo<TResource>(); return model.InjectTo<TResource>();
} }
protected PagingResource<TResource> ApplyToPage<TModel>(Func<PagingSpec<TModel>, PagingSpec<TModel>> function, PagingSpec<TModel> pagingSpec) where TModel : ModelBase, new()
{
pagingSpec = function(pagingSpec);
return new PagingResource<TResource>
{
Page = pagingSpec.Page,
PageSize = pagingSpec.PageSize,
SortDirection = pagingSpec.SortDirection,
SortKey = pagingSpec.SortKey,
TotalRecords = pagingSpec.TotalRecords,
Records = pagingSpec.Records.InjectTo<List<TResource>>()
};
}
} }
} }

View File

@ -2,13 +2,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Api namespace NzbDrone.Api
{ {
public class PagingResource<TModel> public class PagingResource<TModel>
{ {
public int Page { get; set; } public int Page { get; set; }
public int PageSize { get; set; }
public string SortKey { get; set; } public string SortKey { get; set; }
public SortDirection SortDirection { get; set; }
public int TotalRecords { get; set; } public int TotalRecords { get; set; }
public List<TModel> Records { get; set; } public List<TModel> Records { get; set; }
} }

View File

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using AutoMapper;
using FluentValidation; using FluentValidation;
using Nancy; using Nancy;
using NzbDrone.Api.Extensions; using NzbDrone.Api.Extensions;
using System.Linq; using System.Linq;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Api.REST namespace NzbDrone.Api.REST
{ {
@ -16,6 +18,7 @@ public abstract class RestModule<TResource> : NancyModule
private Action<int> _deleteResource; private Action<int> _deleteResource;
private Func<int, TResource> _getResourceById; private Func<int, TResource> _getResourceById;
private Func<List<TResource>> _getResourceAll; private Func<List<TResource>> _getResourceAll;
private Func<PagingResource<TResource>, PagingResource<TResource>> _getResourcePaged;
private Func<TResource> _getResourceSingle; private Func<TResource> _getResourceSingle;
private Func<TResource, TResource> _createResource; private Func<TResource, TResource> _createResource;
private Func<TResource, TResource> _updateResource; private Func<TResource, TResource> _updateResource;
@ -24,7 +27,6 @@ public abstract class RestModule<TResource> : NancyModule
protected ResourceValidator<TResource> PutValidator { get; private set; } protected ResourceValidator<TResource> PutValidator { get; private set; }
protected ResourceValidator<TResource> SharedValidator { get; private set; } protected ResourceValidator<TResource> SharedValidator { get; private set; }
protected void ValidateId(int id) protected void ValidateId(int id)
{ {
if (id <= 0) if (id <= 0)
@ -33,7 +35,6 @@ protected void ValidateId(int id)
} }
} }
protected RestModule(string modulePath) protected RestModule(string modulePath)
: base(modulePath) : base(modulePath)
{ {
@ -88,6 +89,21 @@ protected Func<List<TResource>> GetResourceAll
} }
} }
protected Func<PagingResource<TResource>, PagingResource<TResource>> GetResourcePaged
{
private get { return _getResourcePaged; }
set
{
_getResourcePaged = value;
Get[ROOT_ROUTE] = options =>
{
var resource = GetResourcePaged(ReadPagingResourceFromRequest());
return resource.AsResponse();
};
}
}
protected Func<TResource> GetResourceSingle protected Func<TResource> GetResourceSingle
{ {
private get { return _getResourceSingle; } private get { return _getResourceSingle; }
@ -132,7 +148,6 @@ protected Func<TResource, TResource> UpdateResource
} }
} }
private TResource ReadFromRequest() private TResource ReadFromRequest()
{ {
//TODO: handle when request is null //TODO: handle when request is null
@ -140,7 +155,6 @@ private TResource ReadFromRequest()
var errors = SharedValidator.Validate(resource).Errors.ToList(); var errors = SharedValidator.Validate(resource).Errors.ToList();
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase)) if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
{ {
errors.AddRange(PostValidator.Validate(resource).Errors); errors.AddRange(PostValidator.Validate(resource).Errors);
@ -157,5 +171,34 @@ private TResource ReadFromRequest()
return resource; return resource;
} }
private PagingResource<TResource> ReadPagingResourceFromRequest()
{
int pageSize;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.PageSize), out pageSize);
if (pageSize == 0) pageSize = 10;
int page;
Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page);
if (page == 0) page = 1;
var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey);
if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate";
var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir)
.Equals("Asc", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
var pagingResource = new PagingResource<TResource>
{
PageSize = pageSize,
Page = page,
SortKey = sortKey,
SortDirection = sortDirection
};
return pagingResource;
}
} }
} }

View File

@ -21,7 +21,7 @@ public interface IEpisodeService
Episode GetEpisode(int seriesId, DateTime date); Episode GetEpisode(int seriesId, DateTime date);
List<Episode> GetEpisodeBySeries(int seriesId); List<Episode> GetEpisodeBySeries(int seriesId);
List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber); List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials); PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec);
List<Episode> GetEpisodesByFileId(int episodeFileId); List<Episode> GetEpisodesByFileId(int episodeFileId);
List<Episode> EpisodesWithFiles(); List<Episode> EpisodesWithFiles();
void RefreshEpisodeInfo(Series series); void RefreshEpisodeInfo(Series series);
@ -93,9 +93,9 @@ public List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
return _episodeRepository.GetEpisodes(seriesId, seasonNumber); return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
} }
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials) public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec)
{ {
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, includeSpecials); var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);
return episodeResult; return episodeResult;
} }

View File

@ -2,7 +2,6 @@
<FileVersion>1</FileVersion> <FileVersion>1</FileVersion>
<AutoEnableOnStartup>False</AutoEnableOnStartup> <AutoEnableOnStartup>False</AutoEnableOnStartup>
<AllowParallelTestExecution>true</AllowParallelTestExecution> <AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit> <FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio> <FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec> <FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec>

View File

@ -16,8 +16,6 @@ define(['app', 'AddSeries/RootFolders/RootFolderCollection', 'AddSeries/New/Sear
initialize: function () { initialize: function () {
this.collection = new NzbDrone.AddSeries.Collection(); this.collection = new NzbDrone.AddSeries.Collection();
model.id = undefined;
NzbDrone.AddSeries.New.AddNewSeriesContext = this; NzbDrone.AddSeries.New.AddNewSeriesContext = this;
NzbDrone.vent.on(NzbDrone.Events.SeriesAdded, function (options) { NzbDrone.vent.on(NzbDrone.Events.SeriesAdded, function (options) {