mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-15 01:23:53 -07:00
New: Custom Formats
Co-Authored-By: ta264 <ta264@users.noreply.github.com>
This commit is contained in:
parent
86e44731bb
commit
9fe13a2d14
187 changed files with 6957 additions and 902 deletions
115
src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs
Normal file
115
src/Lidarr.Api.V1/CustomFormats/CustomFormatController.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation;
|
||||
using Lidarr.Api.V1.CustomFormats;
|
||||
using Lidarr.Http;
|
||||
using Lidarr.Http.REST;
|
||||
using Lidarr.Http.REST.Attributes;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.CustomFormats;
|
||||
|
||||
namespace Sonarr.Api.V3.CustomFormats
|
||||
{
|
||||
[V1ApiController]
|
||||
public class CustomFormatController : RestController<CustomFormatResource>
|
||||
{
|
||||
private readonly ICustomFormatService _formatService;
|
||||
private readonly List<ICustomFormatSpecification> _specifications;
|
||||
|
||||
public CustomFormatController(ICustomFormatService formatService,
|
||||
List<ICustomFormatSpecification> specifications)
|
||||
{
|
||||
_formatService = formatService;
|
||||
_specifications = specifications;
|
||||
|
||||
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
||||
SharedValidator.RuleFor(c => c.Name)
|
||||
.Must((v, c) => !_formatService.All().Any(f => f.Name == c && f.Id != v.Id)).WithMessage("Must be unique.");
|
||||
SharedValidator.RuleFor(c => c.Specifications).NotEmpty();
|
||||
SharedValidator.RuleFor(c => c).Custom((customFormat, context) =>
|
||||
{
|
||||
if (!customFormat.Specifications.Any())
|
||||
{
|
||||
context.AddFailure("Must contain at least one Condition");
|
||||
}
|
||||
|
||||
if (customFormat.Specifications.Any(s => s.Name.IsNullOrWhiteSpace()))
|
||||
{
|
||||
context.AddFailure("Condition name(s) cannot be empty or consist of only spaces");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override CustomFormatResource GetResourceById(int id)
|
||||
{
|
||||
return _formatService.GetById(id).ToResource(true);
|
||||
}
|
||||
|
||||
[RestPostById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<CustomFormatResource> Create(CustomFormatResource customFormatResource)
|
||||
{
|
||||
var model = customFormatResource.ToModel(_specifications);
|
||||
return Created(_formatService.Insert(model).Id);
|
||||
}
|
||||
|
||||
[RestPutById]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<CustomFormatResource> Update(CustomFormatResource resource)
|
||||
{
|
||||
var model = resource.ToModel(_specifications);
|
||||
_formatService.Update(model);
|
||||
|
||||
return Accepted(model.Id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces("application/json")]
|
||||
public List<CustomFormatResource> GetAll()
|
||||
{
|
||||
return _formatService.All().ToResource(true);
|
||||
}
|
||||
|
||||
[RestDeleteById]
|
||||
public void DeleteFormat(int id)
|
||||
{
|
||||
_formatService.Delete(id);
|
||||
}
|
||||
|
||||
[HttpGet("schema")]
|
||||
public object GetTemplates()
|
||||
{
|
||||
var schema = _specifications.OrderBy(x => x.Order).Select(x => x.ToSchema()).ToList();
|
||||
|
||||
var presets = GetPresets();
|
||||
|
||||
foreach (var item in schema)
|
||||
{
|
||||
item.Presets = presets.Where(x => x.GetType().Name == item.Implementation).Select(x => x.ToSchema()).ToList();
|
||||
}
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
private IEnumerable<ICustomFormatSpecification> GetPresets()
|
||||
{
|
||||
yield return new ReleaseTitleSpecification
|
||||
{
|
||||
Name = "Preferred Words",
|
||||
Value = @"\b(SPARKS|Framestor)\b"
|
||||
};
|
||||
|
||||
var formats = _formatService.All();
|
||||
foreach (var format in formats)
|
||||
{
|
||||
foreach (var condition in format.Specifications)
|
||||
{
|
||||
var preset = condition.Clone();
|
||||
preset.Name = $"{format.Name}: {preset.Name}";
|
||||
yield return preset;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue