2017-09-04 02:20:56 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-01-03 12:49:24 +00:00
|
|
|
using FluentValidation;
|
|
|
|
using Lidarr.Http.Extensions;
|
2017-09-04 02:20:56 +00:00
|
|
|
using Nancy;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.ArtistStats;
|
2018-01-18 02:28:47 +00:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2019-02-23 22:39:11 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2019-02-16 14:49:24 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2019-05-07 23:10:09 +00:00
|
|
|
using NzbDrone.Core.MediaCover;
|
2019-10-29 21:27:27 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using NzbDrone.Core.Music.Events;
|
2019-12-16 21:21:32 +00:00
|
|
|
using NzbDrone.Core.Validation;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Validation.Paths;
|
|
|
|
using NzbDrone.SignalR;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.Albums
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
public class AlbumModule : AlbumModuleWithSignalR,
|
|
|
|
IHandle<AlbumGrabbedEvent>,
|
|
|
|
IHandle<AlbumEditedEvent>,
|
2019-12-16 21:21:32 +00:00
|
|
|
IHandle<AlbumUpdatedEvent>,
|
2019-02-16 14:49:24 +00:00
|
|
|
IHandle<AlbumImportedEvent>,
|
2019-10-29 21:27:27 +00:00
|
|
|
IHandle<TrackImportedEvent>,
|
|
|
|
IHandle<TrackFileDeletedEvent>
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
protected readonly IReleaseService _releaseService;
|
2019-12-16 21:21:32 +00:00
|
|
|
protected readonly IAddAlbumService _addAlbumService;
|
2019-05-07 23:10:09 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
public AlbumModule(IAlbumService albumService,
|
2019-12-16 21:21:32 +00:00
|
|
|
IAddAlbumService addAlbumService,
|
2018-12-15 00:02:43 +00:00
|
|
|
IReleaseService releaseService,
|
|
|
|
IArtistStatisticsService artistStatisticsService,
|
2019-05-07 23:10:09 +00:00
|
|
|
IMapCoversToLocal coverMapper,
|
2018-12-15 00:02:43 +00:00
|
|
|
IUpgradableSpecification upgradableSpecification,
|
2019-12-16 21:21:32 +00:00
|
|
|
IBroadcastSignalRMessage signalRBroadcaster,
|
|
|
|
ProfileExistsValidator profileExistsValidator,
|
|
|
|
MetadataProfileExistsValidator metadataProfileExistsValidator)
|
|
|
|
|
2019-05-07 23:10:09 +00:00
|
|
|
: base(albumService, artistStatisticsService, coverMapper, upgradableSpecification, signalRBroadcaster)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseService = releaseService;
|
2019-12-16 21:21:32 +00:00
|
|
|
_addAlbumService = addAlbumService;
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
GetResourceAll = GetAlbums;
|
2019-12-16 21:21:32 +00:00
|
|
|
CreateResource = AddAlbum;
|
2018-01-18 02:28:47 +00:00
|
|
|
UpdateResource = UpdateAlbum;
|
2019-12-16 21:21:32 +00:00
|
|
|
DeleteResource = DeleteAlbum;
|
2020-01-03 12:49:24 +00:00
|
|
|
Put("/monitor", x => SetAlbumsMonitored());
|
2019-12-16 21:21:32 +00:00
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty();
|
|
|
|
PostValidator.RuleFor(s => s.Artist.QualityProfileId).SetValidator(profileExistsValidator);
|
|
|
|
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();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<AlbumResource> GetAlbums()
|
|
|
|
{
|
2017-09-16 04:16:26 +00:00
|
|
|
var artistIdQuery = Request.Query.ArtistId;
|
|
|
|
var albumIdsQuery = Request.Query.AlbumIds;
|
2018-01-18 02:28:47 +00:00
|
|
|
var foreignIdQuery = Request.Query.ForeignAlbumId;
|
2019-04-11 06:38:05 +00:00
|
|
|
var includeAllArtistAlbumsQuery = Request.Query.IncludeAllArtistAlbums;
|
2017-09-16 04:16:26 +00:00
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
if (!Request.Query.ArtistId.HasValue && !albumIdsQuery.HasValue && !foreignIdQuery.HasValue)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2017-10-22 05:25:59 +00:00
|
|
|
return MapToResource(_albumService.GetAllAlbums(), false);
|
2017-09-16 04:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (artistIdQuery.HasValue)
|
|
|
|
{
|
|
|
|
int artistId = Convert.ToInt32(artistIdQuery.Value);
|
|
|
|
|
|
|
|
return MapToResource(_albumService.GetAlbumsByArtist(artistId), false);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
if (foreignIdQuery.HasValue)
|
|
|
|
{
|
2019-04-11 06:38:05 +00:00
|
|
|
string foreignAlbumId = foreignIdQuery.Value.ToString();
|
|
|
|
|
|
|
|
var album = _albumService.FindById(foreignAlbumId);
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
if (album == null)
|
|
|
|
{
|
|
|
|
return MapToResource(new List<Album>(), false);
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:38:05 +00:00
|
|
|
if (includeAllArtistAlbumsQuery.HasValue && Convert.ToBoolean(includeAllArtistAlbumsQuery.Value))
|
|
|
|
{
|
|
|
|
return MapToResource(_albumService.GetAlbumsByArtist(album.ArtistId), false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return MapToResource(new List<Album> { album }, false);
|
|
|
|
}
|
2018-01-18 02:28:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 22:38:31 +00:00
|
|
|
string albumIdsValue = albumIdsQuery.Value.ToString();
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-10-07 22:38:31 +00:00
|
|
|
var albumIds = albumIdsValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
2017-09-16 04:16:26 +00:00
|
|
|
.Select(e => Convert.ToInt32(e))
|
|
|
|
.ToList();
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-10-07 22:38:31 +00:00
|
|
|
return MapToResource(_albumService.GetAlbums(albumIds), false);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
private int AddAlbum(AlbumResource albumResource)
|
|
|
|
{
|
|
|
|
var album = _addAlbumService.AddAlbum(albumResource.ToModel());
|
|
|
|
|
|
|
|
return album.Id;
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:28:47 +00:00
|
|
|
private void UpdateAlbum(AlbumResource albumResource)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-01-18 02:28:47 +00:00
|
|
|
var album = _albumService.GetAlbum(albumResource.Id);
|
|
|
|
|
|
|
|
var model = albumResource.ToModel(album);
|
|
|
|
|
|
|
|
_albumService.UpdateAlbum(model);
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseService.UpdateMany(model.AlbumReleases.Value);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, model.Id);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
private void DeleteAlbum(int id)
|
|
|
|
{
|
|
|
|
var deleteFiles = Request.GetBooleanQueryParameter("deleteFiles");
|
|
|
|
var addImportListExclusion = Request.GetBooleanQueryParameter("addImportListExclusion");
|
|
|
|
|
|
|
|
_albumService.DeleteAlbum(id, deleteFiles, addImportListExclusion);
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
private object SetAlbumsMonitored()
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
var resource = Request.Body.FromJson<AlbumsMonitoredResource>();
|
|
|
|
|
|
|
|
_albumService.SetMonitored(resource.AlbumIds, resource.Monitored);
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
return ResponseWithCode(MapToResource(_albumService.GetAlbums(resource.AlbumIds), false), HttpStatusCode.Accepted);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
2019-02-16 14:49:24 +00:00
|
|
|
|
|
|
|
public void Handle(AlbumGrabbedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var album in message.Album.Albums)
|
|
|
|
{
|
|
|
|
var resource = album.ToResource();
|
|
|
|
resource.Grabbed = true;
|
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, resource);
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(AlbumEditedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
public void Handle(AlbumUpdatedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(AlbumDeletedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Deleted, message.Album.ToResource());
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(AlbumImportedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(TrackImportedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.TrackInfo.Album.ToResource());
|
|
|
|
}
|
2019-10-29 21:27:27 +00:00
|
|
|
|
|
|
|
public void Handle(TrackFileDeletedEvent message)
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (message.Reason == DeleteMediaFileReason.Upgrade)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-10-29 21:27:27 +00:00
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.TrackFile.Album.Value, true));
|
|
|
|
}
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|