Radarr/src/Radarr.Api.V3/Profiles/Delay/DelayProfileController.cs

76 lines
2.5 KiB
C#
Raw Normal View History

2018-11-23 07:03:32 +00:00
using System.Collections.Generic;
using FluentValidation;
2021-10-21 20:04:19 +00:00
using Microsoft.AspNetCore.Mvc;
2018-11-23 07:03:32 +00:00
using NzbDrone.Core.Profiles.Delay;
using Radarr.Http;
2019-12-22 22:08:53 +00:00
using Radarr.Http.REST;
2021-10-21 20:04:19 +00:00
using Radarr.Http.REST.Attributes;
2019-12-22 22:08:53 +00:00
using Radarr.Http.Validation;
2018-11-23 07:03:32 +00:00
namespace Radarr.Api.V3.Profiles.Delay
2018-11-23 07:03:32 +00:00
{
2021-10-21 20:04:19 +00:00
[V3ApiController]
public class DelayProfileController : RestController<DelayProfileResource>
2018-11-23 07:03:32 +00:00
{
private readonly IDelayProfileService _delayProfileService;
2021-10-21 20:04:19 +00:00
public DelayProfileController(IDelayProfileService delayProfileService, DelayProfileTagInUseValidator tagInUseValidator)
2018-11-23 07:03:32 +00:00
{
_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) =>
2018-11-23 07:03:32 +00:00
{
if (!delayProfile.EnableUsenet && !delayProfile.EnableTorrent)
{
context.AddFailure("Either Usenet or Torrent should be enabled");
2018-11-23 07:03:32 +00:00
}
});
}
2021-10-21 20:04:19 +00:00
[RestPostById]
public ActionResult<DelayProfileResource> Create(DelayProfileResource resource)
2018-11-23 07:03:32 +00:00
{
var model = resource.ToModel();
model = _delayProfileService.Add(model);
2021-10-21 20:04:19 +00:00
return Created(model.Id);
2018-11-23 07:03:32 +00:00
}
2021-10-21 20:04:19 +00:00
[RestDeleteById]
public void DeleteProfile(int id)
2018-11-23 07:03:32 +00:00
{
if (id == 1)
{
throw new MethodNotAllowedException("Cannot delete global delay profile");
}
_delayProfileService.Delete(id);
}
2021-10-21 20:04:19 +00:00
[RestPutById]
public ActionResult<DelayProfileResource> Update(DelayProfileResource resource)
2018-11-23 07:03:32 +00:00
{
var model = resource.ToModel();
_delayProfileService.Update(model);
2021-10-21 20:04:19 +00:00
return Accepted(model.Id);
2018-11-23 07:03:32 +00:00
}
protected override DelayProfileResource GetResourceById(int id)
2018-11-23 07:03:32 +00:00
{
return _delayProfileService.Get(id).ToResource();
}
2021-10-21 20:04:19 +00:00
[HttpGet]
public List<DelayProfileResource> GetAll()
2018-11-23 07:03:32 +00:00
{
return _delayProfileService.All().ToResource();
}
}
}