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.
212 lines
7.5 KiB
212 lines
7.5 KiB
7 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
5 years ago
|
using FluentValidation;
|
||
3 years ago
|
using Lidarr.Http;
|
||
5 years ago
|
using Lidarr.Http.Extensions;
|
||
3 years ago
|
using Lidarr.Http.REST.Attributes;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
5 years ago
|
using NzbDrone.Common.Extensions;
|
||
|
using NzbDrone.Core.ArtistStats;
|
||
7 years ago
|
using NzbDrone.Core.Datastore.Events;
|
||
6 years ago
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
||
6 years ago
|
using NzbDrone.Core.Download;
|
||
6 years ago
|
using NzbDrone.Core.MediaCover;
|
||
5 years ago
|
using NzbDrone.Core.MediaFiles;
|
||
5 years ago
|
using NzbDrone.Core.MediaFiles.Events;
|
||
|
using NzbDrone.Core.Messaging.Events;
|
||
|
using NzbDrone.Core.Music;
|
||
|
using NzbDrone.Core.Music.Events;
|
||
5 years ago
|
using NzbDrone.Core.Validation;
|
||
5 years ago
|
using NzbDrone.Core.Validation.Paths;
|
||
|
using NzbDrone.SignalR;
|
||
7 years ago
|
|
||
7 years ago
|
namespace Lidarr.Api.V1.Albums
|
||
7 years ago
|
{
|
||
3 years ago
|
[V1ApiController]
|
||
|
public class AlbumController : AlbumControllerWithSignalR,
|
||
6 years ago
|
IHandle<AlbumGrabbedEvent>,
|
||
|
IHandle<AlbumEditedEvent>,
|
||
5 years ago
|
IHandle<AlbumUpdatedEvent>,
|
||
6 years ago
|
IHandle<AlbumImportedEvent>,
|
||
5 years ago
|
IHandle<TrackImportedEvent>,
|
||
|
IHandle<TrackFileDeletedEvent>
|
||
7 years ago
|
{
|
||
4 years ago
|
protected readonly IArtistService _artistService;
|
||
6 years ago
|
protected readonly IReleaseService _releaseService;
|
||
5 years ago
|
protected readonly IAddAlbumService _addAlbumService;
|
||
6 years ago
|
|
||
3 years ago
|
public AlbumController(IArtistService artistService,
|
||
4 years ago
|
IAlbumService albumService,
|
||
5 years ago
|
IAddAlbumService addAlbumService,
|
||
6 years ago
|
IReleaseService releaseService,
|
||
|
IArtistStatisticsService artistStatisticsService,
|
||
6 years ago
|
IMapCoversToLocal coverMapper,
|
||
6 years ago
|
IUpgradableSpecification upgradableSpecification,
|
||
5 years ago
|
IBroadcastSignalRMessage signalRBroadcaster,
|
||
5 years ago
|
QualityProfileExistsValidator qualityProfileExistsValidator,
|
||
5 years ago
|
MetadataProfileExistsValidator metadataProfileExistsValidator)
|
||
|
|
||
6 years ago
|
: base(albumService, artistStatisticsService, coverMapper, upgradableSpecification, signalRBroadcaster)
|
||
7 years ago
|
{
|
||
4 years ago
|
_artistService = artistService;
|
||
6 years ago
|
_releaseService = releaseService;
|
||
5 years ago
|
_addAlbumService = addAlbumService;
|
||
|
|
||
|
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty();
|
||
5 years ago
|
PostValidator.RuleFor(s => s.Artist.QualityProfileId).SetValidator(qualityProfileExistsValidator);
|
||
5 years ago
|
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).SetValidator(metadataProfileExistsValidator);
|
||
|
PostValidator.RuleFor(s => s.Artist.RootFolderPath).IsValidPath().When(s => s.Artist.Path.IsNullOrWhiteSpace());
|
||
|
PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty();
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
[HttpGet]
|
||
|
public List<AlbumResource> GetAlbums([FromQuery]int? artistId,
|
||
|
[FromQuery] List<int> albumIds,
|
||
|
[FromQuery]string foreignAlbumId,
|
||
|
[FromQuery]bool includeAllArtistAlbums = false)
|
||
7 years ago
|
{
|
||
3 years ago
|
if (!artistId.HasValue && !albumIds.Any() && foreignAlbumId.IsNullOrWhiteSpace())
|
||
7 years ago
|
{
|
||
4 years ago
|
var albums = _albumService.GetAllAlbums();
|
||
|
|
||
|
var artists = _artistService.GetAllArtists().ToDictionary(x => x.ArtistMetadataId);
|
||
|
var releases = _releaseService.GetAllReleases().GroupBy(x => x.AlbumId).ToDictionary(x => x.Key, y => y.ToList());
|
||
|
|
||
|
foreach (var album in albums)
|
||
|
{
|
||
|
album.Artist = artists[album.ArtistMetadataId];
|
||
|
if (releases.TryGetValue(album.Id, out var albumReleases))
|
||
|
{
|
||
|
album.AlbumReleases = albumReleases;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
album.AlbumReleases = new List<AlbumRelease>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return MapToResource(albums, false);
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
if (artistId.HasValue)
|
||
7 years ago
|
{
|
||
3 years ago
|
return MapToResource(_albumService.GetAlbumsByArtist(artistId.Value), false);
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
if (foreignAlbumId.IsNotNullOrWhiteSpace())
|
||
7 years ago
|
{
|
||
6 years ago
|
var album = _albumService.FindById(foreignAlbumId);
|
||
|
|
||
5 years ago
|
if (album == null)
|
||
|
{
|
||
|
return MapToResource(new List<Album>(), false);
|
||
|
}
|
||
|
|
||
3 years ago
|
if (includeAllArtistAlbums)
|
||
6 years ago
|
{
|
||
|
return MapToResource(_albumService.GetAlbumsByArtist(album.ArtistId), false);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return MapToResource(new List<Album> { album }, false);
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
return MapToResource(_albumService.GetAlbums(albumIds), false);
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
[RestPostById]
|
||
|
public ActionResult<AlbumResource> AddAlbum(AlbumResource albumResource)
|
||
5 years ago
|
{
|
||
|
var album = _addAlbumService.AddAlbum(albumResource.ToModel());
|
||
|
|
||
3 years ago
|
return Created(album.Id);
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
[RestPutById]
|
||
|
public ActionResult<AlbumResource> UpdateAlbum(AlbumResource albumResource)
|
||
7 years ago
|
{
|
||
7 years ago
|
var album = _albumService.GetAlbum(albumResource.Id);
|
||
|
|
||
|
var model = albumResource.ToModel(album);
|
||
|
|
||
|
_albumService.UpdateAlbum(model);
|
||
6 years ago
|
_releaseService.UpdateMany(model.AlbumReleases.Value);
|
||
7 years ago
|
|
||
6 years ago
|
BroadcastResourceChange(ModelAction.Updated, model.Id);
|
||
3 years ago
|
|
||
|
return Accepted(model.Id);
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
[RestDeleteById]
|
||
|
public void DeleteAlbum(int id)
|
||
5 years ago
|
{
|
||
|
var deleteFiles = Request.GetBooleanQueryParameter("deleteFiles");
|
||
|
var addImportListExclusion = Request.GetBooleanQueryParameter("addImportListExclusion");
|
||
|
|
||
|
_albumService.DeleteAlbum(id, deleteFiles, addImportListExclusion);
|
||
|
}
|
||
|
|
||
3 years ago
|
[HttpPut("monitor")]
|
||
|
public IActionResult SetAlbumsMonitored([FromBody]AlbumsMonitoredResource resource)
|
||
7 years ago
|
{
|
||
|
_albumService.SetMonitored(resource.AlbumIds, resource.Monitored);
|
||
|
|
||
3 years ago
|
return Accepted(MapToResource(_albumService.GetAlbums(resource.AlbumIds), false));
|
||
7 years ago
|
}
|
||
6 years ago
|
|
||
3 years ago
|
[NonAction]
|
||
6 years ago
|
public void Handle(AlbumGrabbedEvent message)
|
||
|
{
|
||
|
foreach (var album in message.Album.Albums)
|
||
|
{
|
||
|
var resource = album.ToResource();
|
||
|
resource.Grabbed = true;
|
||
|
|
||
|
BroadcastResourceChange(ModelAction.Updated, resource);
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
3 years ago
|
[NonAction]
|
||
6 years ago
|
public void Handle(AlbumEditedEvent message)
|
||
|
{
|
||
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
||
|
}
|
||
|
|
||
3 years ago
|
[NonAction]
|
||
5 years ago
|
public void Handle(AlbumUpdatedEvent message)
|
||
|
{
|
||
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
||
|
}
|
||
|
|
||
3 years ago
|
[NonAction]
|
||
5 years ago
|
public void Handle(AlbumDeletedEvent message)
|
||
|
{
|
||
|
BroadcastResourceChange(ModelAction.Deleted, message.Album.ToResource());
|
||
|
}
|
||
|
|
||
3 years ago
|
[NonAction]
|
||
6 years ago
|
public void Handle(AlbumImportedEvent message)
|
||
|
{
|
||
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
||
|
}
|
||
|
|
||
3 years ago
|
[NonAction]
|
||
6 years ago
|
public void Handle(TrackImportedEvent message)
|
||
|
{
|
||
|
BroadcastResourceChange(ModelAction.Updated, message.TrackInfo.Album.ToResource());
|
||
|
}
|
||
5 years ago
|
|
||
3 years ago
|
[NonAction]
|
||
5 years ago
|
public void Handle(TrackFileDeletedEvent message)
|
||
|
{
|
||
5 years ago
|
if (message.Reason == DeleteMediaFileReason.Upgrade)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.TrackFile.Album.Value, true));
|
||
|
}
|
||
7 years ago
|
}
|
||
|
}
|