Files
Readarr/src/Lidarr.Api.V1/Artist/ArtistEditorModule.cs
T
Qstick 6581b3a2c5 New: UI Updates, Tag manager, More custom filters (#437)
* New: UI Updates, Tag manager, More custom filters

* fixup! Fix ScanFixture Unit Tests

* Fixed: Sentry Errors from UI don't have release, branch, environment

* Changed: Bump Mobile Detect for New Device Detection

* Fixed: Build on changes to package.json

* fixup! Add MetadataProfile filter option

* fixup! Tag Note, Blacklist, Manual Import

* fixup: Remove connectSection

* fixup: root folder comment
2018-08-07 20:57:15 -04:00

117 lines
3.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Nancy;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Music;
using NzbDrone.Core.Music.Commands;
using Lidarr.Http.Extensions;
namespace Lidarr.Api.V1.Artist
{
public class ArtistEditorModule : LidarrV1Module
{
private readonly IArtistService _artistService;
private readonly IManageCommandQueue _commandQueueManager;
public ArtistEditorModule(IArtistService artistService, IManageCommandQueue commandQueueManager)
: base("/artist/editor")
{
_artistService = artistService;
_commandQueueManager = commandQueueManager;
Put["/"] = artist => SaveAll();
Delete["/"] = artist => DeleteArtist();
}
private Response SaveAll()
{
var resource = Request.Body.FromJson<ArtistEditorResource>();
var artistToUpdate = _artistService.GetArtists(resource.ArtistIds);
var artistToMove = new List<BulkMoveArtist>();
foreach (var artist in artistToUpdate)
{
if (resource.Monitored.HasValue)
{
artist.Monitored = resource.Monitored.Value;
}
if (resource.QualityProfileId.HasValue)
{
artist.ProfileId = resource.QualityProfileId.Value;
}
if (resource.LanguageProfileId.HasValue)
{
artist.LanguageProfileId = resource.LanguageProfileId.Value;
}
if (resource.MetadataProfileId.HasValue)
{
artist.MetadataProfileId = resource.MetadataProfileId.Value;
}
if (resource.AlbumFolder.HasValue)
{
artist.AlbumFolder = resource.AlbumFolder.Value;
}
if (resource.RootFolderPath.IsNotNullOrWhiteSpace())
{
artist.RootFolderPath = resource.RootFolderPath;
artistToMove.Add(new BulkMoveArtist
{
ArtistId = artist.Id,
SourcePath = artist.Path
});
}
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;
}
}
}
if (resource.MoveFiles && artistToMove.Any())
{
_commandQueueManager.Push(new BulkMoveArtistCommand
{
DestinationRootFolder = resource.RootFolderPath,
Artist = artistToMove
});
}
return _artistService.UpdateArtists(artistToUpdate, !resource.MoveFiles)
.ToResource()
.AsResponse(HttpStatusCode.Accepted);
}
private Response DeleteArtist()
{
var resource = Request.Body.FromJson<ArtistEditorResource>();
foreach (var artistId in resource.ArtistIds)
{
_artistService.DeleteArtist(artistId, false);
}
return new object().AsResponse();
}
}
}