2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
2017-12-30 03:23:04 +00:00
|
|
|
using System.Linq;
|
2017-09-04 02:20:56 +00:00
|
|
|
using Nancy;
|
|
|
|
using NzbDrone.Common.Extensions;
|
2017-12-30 03:23:04 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2017-09-04 02:20:56 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2017-12-30 03:23:04 +00:00
|
|
|
using NzbDrone.Core.Music.Commands;
|
2017-09-04 02:20:56 +00:00
|
|
|
using Lidarr.Http.Extensions;
|
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.Artist
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2017-10-31 01:28:29 +00:00
|
|
|
public class ArtistEditorModule : LidarrV1Module
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
private readonly IArtistService _artistService;
|
2017-12-30 03:23:04 +00:00
|
|
|
private readonly IManageCommandQueue _commandQueueManager;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-12-30 03:23:04 +00:00
|
|
|
public ArtistEditorModule(IArtistService artistService, IManageCommandQueue commandQueueManager)
|
2017-09-04 02:20:56 +00:00
|
|
|
: base("/artist/editor")
|
|
|
|
{
|
|
|
|
_artistService = artistService;
|
2017-12-30 03:23:04 +00:00
|
|
|
_commandQueueManager = commandQueueManager;
|
2017-09-04 02:20:56 +00:00
|
|
|
Put["/"] = artist => SaveAll();
|
2017-10-22 05:25:59 +00:00
|
|
|
Delete["/"] = artist => DeleteArtist();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Response SaveAll()
|
|
|
|
{
|
|
|
|
var resource = Request.Body.FromJson<ArtistEditorResource>();
|
|
|
|
var artistToUpdate = _artistService.GetArtists(resource.ArtistIds);
|
2017-12-30 03:23:04 +00:00
|
|
|
var artistToMove = new List<BulkMoveArtist>();
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
foreach (var artist in artistToUpdate)
|
|
|
|
{
|
|
|
|
if (resource.Monitored.HasValue)
|
|
|
|
{
|
|
|
|
artist.Monitored = resource.Monitored.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.QualityProfileId.HasValue)
|
|
|
|
{
|
|
|
|
artist.ProfileId = resource.QualityProfileId.Value;
|
|
|
|
}
|
|
|
|
|
2017-11-26 03:51:37 +00:00
|
|
|
if (resource.LanguageProfileId.HasValue)
|
|
|
|
{
|
|
|
|
artist.LanguageProfileId = resource.LanguageProfileId.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.MetadataProfileId.HasValue)
|
|
|
|
{
|
|
|
|
artist.MetadataProfileId = resource.MetadataProfileId.Value;
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
if (resource.AlbumFolder.HasValue)
|
|
|
|
{
|
|
|
|
artist.AlbumFolder = resource.AlbumFolder.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.RootFolderPath.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
artist.RootFolderPath = resource.RootFolderPath;
|
2017-12-30 03:23:04 +00:00
|
|
|
artistToMove.Add(new BulkMoveArtist
|
|
|
|
{
|
|
|
|
ArtistId = artist.Id,
|
|
|
|
SourcePath = artist.Path
|
|
|
|
});
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resource.Tags != null)
|
|
|
|
{
|
|
|
|
var newTags = resource.Tags;
|
|
|
|
var applyTags = resource.ApplyTags;
|
|
|
|
|
|
|
|
switch (applyTags)
|
|
|
|
{
|
|
|
|
case ApplyTags.Add:
|
|
|
|
newTags.ForEach(t => artist.Tags.Add(t));
|
|
|
|
break;
|
|
|
|
case ApplyTags.Remove:
|
|
|
|
newTags.ForEach(t => artist.Tags.Remove(t));
|
|
|
|
break;
|
|
|
|
case ApplyTags.Replace:
|
|
|
|
artist.Tags = new HashSet<int>(newTags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 03:23:04 +00:00
|
|
|
if (resource.MoveFiles && artistToMove.Any())
|
|
|
|
{
|
|
|
|
_commandQueueManager.Push(new BulkMoveArtistCommand
|
|
|
|
{
|
|
|
|
DestinationRootFolder = resource.RootFolderPath,
|
|
|
|
Artist = artistToMove
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
return _artistService.UpdateArtists(artistToUpdate)
|
|
|
|
.ToResource()
|
|
|
|
.AsResponse(HttpStatusCode.Accepted);
|
|
|
|
}
|
|
|
|
|
2017-10-22 05:25:59 +00:00
|
|
|
private Response DeleteArtist()
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
var resource = Request.Body.FromJson<ArtistEditorResource>();
|
|
|
|
|
|
|
|
foreach (var artistId in resource.ArtistIds)
|
|
|
|
{
|
|
|
|
_artistService.DeleteArtist(artistId, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new object().AsResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|