Lidarr/src/Lidarr.Api.V1/RootFolders/RootFolderController.cs

96 lines
3.4 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2017-09-04 02:20:56 +00:00
using FluentValidation;
using Lidarr.Http;
2020-02-09 19:15:43 +00:00
using Lidarr.Http.REST;
2021-08-04 20:42:40 +00:00
using Lidarr.Http.REST.Attributes;
using Microsoft.AspNetCore.Mvc;
2017-09-04 02:20:56 +00:00
using NzbDrone.Core.RootFolders;
2020-02-09 19:15:43 +00:00
using NzbDrone.Core.Validation;
2017-09-04 02:20:56 +00:00
using NzbDrone.Core.Validation.Paths;
using NzbDrone.SignalR;
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.RootFolders
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
[V1ApiController]
public class RootFolderController : RestControllerWithSignalR<RootFolderResource, RootFolder>
2017-09-04 02:20:56 +00:00
{
private readonly IRootFolderService _rootFolderService;
2021-08-04 20:42:40 +00:00
public RootFolderController(IRootFolderService rootFolderService,
2017-09-04 02:20:56 +00:00
IBroadcastSignalRMessage signalRBroadcaster,
RootFolderValidator rootFolderValidator,
PathExistsValidator pathExistsValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator,
StartupFolderValidator startupFolderValidator,
SystemFolderValidator systemFolderValidator,
2020-02-09 19:15:43 +00:00
FolderWritableValidator folderWritableValidator,
QualityProfileExistsValidator qualityProfileExistsValidator,
MetadataProfileExistsValidator metadataProfileExistsValidator)
2017-09-04 02:20:56 +00:00
: base(signalRBroadcaster)
{
_rootFolderService = rootFolderService;
SharedValidator.RuleFor(c => c.Path)
2020-02-09 19:15:43 +00:00
.Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(mappedNetworkDriveValidator)
.SetValidator(startupFolderValidator)
.SetValidator(pathExistsValidator)
.SetValidator(systemFolderValidator)
.SetValidator(folderWritableValidator);
PostValidator.RuleFor(c => c.Path)
.SetValidator(rootFolderValidator);
SharedValidator.RuleFor(c => c.Name)
.NotEmpty();
SharedValidator.RuleFor(c => c.DefaultMetadataProfileId)
.SetValidator(metadataProfileExistsValidator);
SharedValidator.RuleFor(c => c.DefaultQualityProfileId)
.SetValidator(qualityProfileExistsValidator);
2017-09-04 02:20:56 +00:00
}
2021-08-04 20:42:40 +00:00
public override RootFolderResource GetResourceById(int id)
2017-09-04 02:20:56 +00:00
{
return _rootFolderService.Get(id).ToResource();
}
2021-08-04 20:42:40 +00:00
[RestPostById]
public ActionResult<RootFolderResource> CreateRootFolder(RootFolderResource rootFolderResource)
2017-09-04 02:20:56 +00:00
{
var model = rootFolderResource.ToModel();
2021-08-04 20:42:40 +00:00
return Created(_rootFolderService.Add(model).Id);
2017-09-04 02:20:56 +00:00
}
2021-08-04 20:42:40 +00:00
[RestPutById]
public ActionResult<RootFolderResource> UpdateRootFolder(RootFolderResource rootFolderResource)
2020-02-09 19:15:43 +00:00
{
var model = rootFolderResource.ToModel();
if (model.Path != rootFolderResource.Path)
{
throw new BadRequestException("Cannot edit root folder path");
}
_rootFolderService.Update(model);
2021-08-04 20:42:40 +00:00
return Accepted(model.Id);
2020-02-09 19:15:43 +00:00
}
2021-08-04 20:42:40 +00:00
[HttpGet]
public List<RootFolderResource> GetRootFolders()
2017-09-04 02:20:56 +00:00
{
2020-02-09 19:15:43 +00:00
return _rootFolderService.AllWithSpaceStats().ToResource();
2017-09-04 02:20:56 +00:00
}
2021-08-04 20:42:40 +00:00
[RestDeleteById]
public void DeleteFolder(int id)
2017-09-04 02:20:56 +00:00
{
_rootFolderService.Remove(id);
}
}
}