Lidarr/src/Lidarr.Api.V1/History/HistoryController.cs

116 lines
4.5 KiB
C#
Raw Normal View History

2017-09-04 02:20:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2017-10-31 01:28:29 +00:00
using Lidarr.Api.V1.Albums;
using Lidarr.Api.V1.Artist;
using Lidarr.Api.V1.Tracks;
2017-09-04 02:20:56 +00:00
using Lidarr.Http;
using Lidarr.Http.Extensions;
2021-08-04 20:42:40 +00:00
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.DecisionEngine.Specifications;
using NzbDrone.Core.Download;
using NzbDrone.Core.History;
2017-09-04 02:20:56 +00:00
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.History
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
[V1ApiController]
public class HistoryController : Controller
2017-09-04 02:20:56 +00:00
{
private readonly IHistoryService _historyService;
private readonly IUpgradableSpecification _upgradableSpecification;
private readonly IFailedDownloadService _failedDownloadService;
2021-08-04 20:42:40 +00:00
public HistoryController(IHistoryService historyService,
2017-09-04 02:20:56 +00:00
IUpgradableSpecification upgradableSpecification,
IFailedDownloadService failedDownloadService)
{
_historyService = historyService;
_upgradableSpecification = upgradableSpecification;
_failedDownloadService = failedDownloadService;
}
protected HistoryResource MapToResource(EntityHistory model, bool includeArtist, bool includeAlbum, bool includeTrack)
2017-09-04 02:20:56 +00:00
{
var resource = model.ToResource();
2017-10-02 03:05:28 +00:00
if (includeArtist)
{
resource.Artist = model.Artist.ToResource();
}
2017-10-02 03:05:28 +00:00
if (includeAlbum)
{
resource.Album = model.Album.ToResource();
}
2017-10-02 03:05:28 +00:00
if (includeTrack)
{
resource.Track = model.Track.ToResource();
}
2017-09-04 02:20:56 +00:00
if (model.Artist != null)
{
resource.QualityCutoffNotMet = _upgradableSpecification.QualityCutoffNotMet(model.Artist.QualityProfile.Value, model.Quality);
2017-09-04 02:20:56 +00:00
}
return resource;
}
2021-08-04 20:42:40 +00:00
[HttpGet]
public PagingResource<HistoryResource> GetHistory(bool includeArtist = false, bool includeAlbum = false, bool includeTrack = false)
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
var pagingResource = Request.ReadPagingResourceFromRequest<HistoryResource>();
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, EntityHistory>("date", SortDirection.Descending);
2017-09-04 02:20:56 +00:00
2018-03-15 01:28:46 +00:00
var eventTypeFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "eventType");
var albumIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "albumId");
var downloadIdFilter = pagingResource.Filters.FirstOrDefault(f => f.Key == "downloadId");
2018-03-15 01:28:46 +00:00
if (eventTypeFilter != null)
2017-09-04 02:20:56 +00:00
{
var filterValue = (EntityHistoryEventType)Convert.ToInt32(eventTypeFilter.Value);
2018-03-15 01:28:46 +00:00
pagingSpec.FilterExpressions.Add(v => v.EventType == filterValue);
2017-09-04 02:20:56 +00:00
}
2018-03-15 01:28:46 +00:00
if (albumIdFilter != null)
2017-09-04 02:20:56 +00:00
{
2018-03-15 01:28:46 +00:00
var albumId = Convert.ToInt32(albumIdFilter.Value);
pagingSpec.FilterExpressions.Add(h => h.AlbumId == albumId);
2017-09-04 02:20:56 +00:00
}
if (downloadIdFilter != null)
{
var downloadId = downloadIdFilter.Value;
pagingSpec.FilterExpressions.Add(h => h.DownloadId == downloadId);
}
2021-08-04 20:42:40 +00:00
return pagingSpec.ApplyToPage(_historyService.Paged, h => MapToResource(h, includeArtist, includeAlbum, includeTrack));
2017-09-04 02:20:56 +00:00
}
2021-08-04 20:42:40 +00:00
[HttpGet("since")]
public List<HistoryResource> GetHistorySince(DateTime date, EntityHistoryEventType? eventType = null, bool includeArtist = false, bool includeAlbum = false, bool includeTrack = false)
{
return _historyService.Since(date, eventType).Select(h => MapToResource(h, includeArtist, includeAlbum, includeTrack)).ToList();
}
2021-08-04 20:42:40 +00:00
[HttpGet("artist")]
public List<HistoryResource> GetArtistHistory(int artistId, int? albumId = null, EntityHistoryEventType? eventType = null, bool includeArtist = false, bool includeAlbum = false, bool includeTrack = false)
{
2021-08-04 20:42:40 +00:00
if (albumId.HasValue)
{
2021-08-04 20:42:40 +00:00
return _historyService.GetByAlbum(albumId.Value, eventType).Select(h => MapToResource(h, includeArtist, includeAlbum, includeTrack)).ToList();
}
return _historyService.GetByArtist(artistId, eventType).Select(h => MapToResource(h, includeArtist, includeAlbum, includeTrack)).ToList();
}
2021-08-04 20:42:40 +00:00
[HttpPost("failed")]
public object MarkAsFailed([FromBody] int id)
2017-09-04 02:20:56 +00:00
{
_failedDownloadService.MarkAsFailed(id);
return new { };
2017-09-04 02:20:56 +00:00
}
}
}