You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.5 KiB
101 lines
3.5 KiB
6 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using Nancy;
|
||
|
using NzbDrone.Core.Datastore;
|
||
|
using NzbDrone.Core.History;
|
||
4 years ago
|
using Prowlarr.Http;
|
||
|
using Prowlarr.Http.Extensions;
|
||
|
using Prowlarr.Http.REST;
|
||
6 years ago
|
|
||
4 years ago
|
namespace Prowlarr.Api.V1.History
|
||
6 years ago
|
{
|
||
4 years ago
|
public class HistoryModule : ProwlarrRestModule<HistoryResource>
|
||
6 years ago
|
{
|
||
|
private readonly IHistoryService _historyService;
|
||
4 years ago
|
|
||
|
public HistoryModule(IHistoryService historyService)
|
||
6 years ago
|
{
|
||
|
_historyService = historyService;
|
||
|
GetResourcePaged = GetHistory;
|
||
|
|
||
5 years ago
|
Get("/since", x => GetHistorySince());
|
||
|
Get("/movie", x => GetMovieHistory());
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
protected HistoryResource MapToResource(MovieHistory model, bool includeMovie)
|
||
6 years ago
|
{
|
||
4 years ago
|
var resource = model.ToResource();
|
||
6 years ago
|
|
||
|
return resource;
|
||
|
}
|
||
|
|
||
|
private PagingResource<HistoryResource> GetHistory(PagingResource<HistoryResource> pagingResource)
|
||
|
{
|
||
5 years ago
|
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, MovieHistory>("date", SortDirection.Descending);
|
||
6 years ago
|
var includeMovie = Request.GetBooleanQueryParameter("includeMovie");
|
||
|
|
||
|
var eventTypeFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "eventType");
|
||
5 years ago
|
var downloadIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "downloadId");
|
||
6 years ago
|
|
||
|
if (eventTypeFilter != null)
|
||
|
{
|
||
5 years ago
|
var filterValue = (MovieHistoryEventType)Convert.ToInt32(eventTypeFilter.Value);
|
||
6 years ago
|
pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue);
|
||
|
}
|
||
|
|
||
5 years ago
|
if (downloadIdFilter != null)
|
||
|
{
|
||
|
var downloadId = downloadIdFilter.Value;
|
||
|
pagingSpec.FilterExpressions.Add(h => h.DownloadId == downloadId);
|
||
|
}
|
||
|
|
||
6 years ago
|
return ApplyToPage(_historyService.Paged, pagingSpec, h => MapToResource(h, includeMovie));
|
||
|
}
|
||
|
|
||
|
private List<HistoryResource> GetHistorySince()
|
||
|
{
|
||
|
var queryDate = Request.Query.Date;
|
||
|
var queryEventType = Request.Query.EventType;
|
||
5 years ago
|
|
||
6 years ago
|
if (!queryDate.HasValue)
|
||
|
{
|
||
|
throw new BadRequestException("date is missing");
|
||
|
}
|
||
|
|
||
|
DateTime date = DateTime.Parse(queryDate.Value);
|
||
5 years ago
|
MovieHistoryEventType? eventType = null;
|
||
6 years ago
|
var includeMovie = Request.GetBooleanQueryParameter("includeMovie");
|
||
|
|
||
|
if (queryEventType.HasValue)
|
||
|
{
|
||
5 years ago
|
eventType = (MovieHistoryEventType)Convert.ToInt32(queryEventType.Value);
|
||
6 years ago
|
}
|
||
|
|
||
|
return _historyService.Since(date, eventType).Select(h => MapToResource(h, includeMovie)).ToList();
|
||
|
}
|
||
|
|
||
|
private List<HistoryResource> GetMovieHistory()
|
||
|
{
|
||
|
var queryMovieId = Request.Query.MovieId;
|
||
|
var queryEventType = Request.Query.EventType;
|
||
|
|
||
|
if (!queryMovieId.HasValue)
|
||
|
{
|
||
|
throw new BadRequestException("movieId is missing");
|
||
|
}
|
||
|
|
||
|
int movieId = Convert.ToInt32(queryMovieId.Value);
|
||
5 years ago
|
MovieHistoryEventType? eventType = null;
|
||
6 years ago
|
var includeMovie = Request.GetBooleanQueryParameter("includeMovie");
|
||
|
|
||
|
if (queryEventType.HasValue)
|
||
|
{
|
||
5 years ago
|
eventType = (MovieHistoryEventType)Convert.ToInt32(queryEventType.Value);
|
||
6 years ago
|
}
|
||
|
|
||
|
return _historyService.GetByMovieId(movieId, eventType).Select(h => MapToResource(h, includeMovie)).ToList();
|
||
|
}
|
||
|
}
|
||
|
}
|