Files
lidarr/src/Lidarr.Api.V1/Profiles/Delay/DelayProfileController.cs
T
Bogdan 826b8b5933 Parameter binding for API requests
(cherry picked from commit 074b293d62b56bf02d82ac12ad726777b5d1e478)
2025-10-14 21:51:14 -05:00

84 lines
2.7 KiB
C#

using System.Collections.Generic;
using FluentValidation;
using Lidarr.Http;
using Lidarr.Http.REST;
using Lidarr.Http.REST.Attributes;
using Lidarr.Http.Validation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Profiles.Delay;
namespace Lidarr.Api.V1.Profiles.Delay
{
[V1ApiController]
public class DelayProfileController : RestController<DelayProfileResource>
{
private readonly IDelayProfileService _delayProfileService;
public DelayProfileController(IDelayProfileService delayProfileService, DelayProfileTagInUseValidator tagInUseValidator)
{
_delayProfileService = delayProfileService;
SharedValidator.RuleFor(d => d.Tags).NotEmpty().When(d => d.Id != 1);
SharedValidator.RuleFor(d => d.Tags).EmptyCollection<DelayProfileResource, int>().When(d => d.Id == 1);
SharedValidator.RuleFor(d => d.Tags).SetValidator(tagInUseValidator);
SharedValidator.RuleFor(d => d.UsenetDelay).GreaterThanOrEqualTo(0);
SharedValidator.RuleFor(d => d.TorrentDelay).GreaterThanOrEqualTo(0);
SharedValidator.RuleFor(d => d).Custom((delayProfile, context) =>
{
if (!delayProfile.EnableUsenet && !delayProfile.EnableTorrent)
{
context.AddFailure("Either Usenet or Torrent should be enabled");
}
});
}
[RestPostById]
public ActionResult<DelayProfileResource> Create([FromBody] DelayProfileResource resource)
{
var model = resource.ToModel();
model = _delayProfileService.Add(model);
return Created(model.Id);
}
[RestDeleteById]
public void DeleteProfile(int id)
{
if (id == 1)
{
throw new MethodNotAllowedException("Cannot delete global delay profile");
}
_delayProfileService.Delete(id);
}
[RestPutById]
public ActionResult<DelayProfileResource> Update([FromBody] DelayProfileResource resource)
{
var model = resource.ToModel();
_delayProfileService.Update(model);
return Accepted(model.Id);
}
public override DelayProfileResource GetResourceById(int id)
{
return _delayProfileService.Get(id).ToResource();
}
[HttpGet]
public List<DelayProfileResource> GetAll()
{
return _delayProfileService.All().ToResource();
}
[HttpPut("reorder/{id:int}")]
public object Reorder(int id, [FromQuery] int? afterId = null)
{
ValidateId(id);
return _delayProfileService.Reorder(id, afterId).ToResource();
}
}
}