Radarr/src/NzbDrone.Api/History/HistoryModule.cs

80 lines
2.9 KiB
C#
Raw Normal View History

using System;
using Nancy;
using NzbDrone.Api.Episodes;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Series;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
2013-05-03 06:53:32 +00:00
using NzbDrone.Core.History;
namespace NzbDrone.Api.History
{
2013-05-13 06:12:19 +00:00
public class HistoryModule : NzbDroneRestModule<HistoryResource>
2013-05-03 06:53:32 +00:00
{
private readonly IHistoryService _historyService;
private readonly IQualityUpgradableSpecification _qualityUpgradableSpecification;
private readonly IFailedDownloadService _failedDownloadService;
2013-05-03 06:53:32 +00:00
public HistoryModule(IHistoryService historyService,
IQualityUpgradableSpecification qualityUpgradableSpecification,
IFailedDownloadService failedDownloadService)
2013-05-03 06:53:32 +00:00
{
_historyService = historyService;
_qualityUpgradableSpecification = qualityUpgradableSpecification;
_failedDownloadService = failedDownloadService;
2013-05-13 06:12:19 +00:00
GetResourcePaged = GetHistory;
Post["/failed"] = x => MarkAsFailed();
2013-05-03 06:53:32 +00:00
}
protected HistoryResource MapToResource(Core.History.History model)
{
var resource = model.ToResource();
resource.Series = model.Series.ToResource();
resource.Episode = model.Episode.ToResource();
if (model.Series != null)
{
resource.QualityCutoffNotMet = _qualityUpgradableSpecification.CutoffNotMet(model.Series.Profile.Value, model.Quality);
}
return resource;
}
2013-05-13 06:12:19 +00:00
private PagingResource<HistoryResource> GetHistory(PagingResource<HistoryResource> pagingResource)
2013-05-03 06:53:32 +00:00
{
var episodeId = Request.Query.EpisodeId;
2013-05-10 22:33:04 +00:00
var pagingSpec = new PagingSpec<Core.History.History>
2013-05-03 06:53:32 +00:00
{
2013-05-13 06:12:19 +00:00
Page = pagingResource.Page,
PageSize = pagingResource.PageSize,
SortKey = pagingResource.SortKey,
SortDirection = pagingResource.SortDirection
2013-05-03 06:53:32 +00:00
};
if (pagingResource.FilterKey == "eventType")
{
var filterValue = (HistoryEventType)Convert.ToInt32(pagingResource.FilterValue);
pagingSpec.FilterExpression = v => v.EventType == filterValue;
}
if (episodeId.HasValue)
{
int i = (int)episodeId;
pagingSpec.FilterExpression = h => h.EpisodeId == i;
}
return ApplyToPage(_historyService.Paged, pagingSpec, MapToResource);
2013-05-03 06:53:32 +00:00
}
private Response MarkAsFailed()
{
var id = (int)Request.Form.Id;
_failedDownloadService.MarkAsFailed(id);
return new object().AsResponse();
}
2013-05-03 06:53:32 +00:00
}
}