You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/Sonarr.Api.V3/Profiles/Delay/DelayProfileController.cs

87 lines
2.8 KiB

7 years ago
using System.Collections.Generic;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
7 years ago
using NzbDrone.Core.Profiles.Delay;
using Sonarr.Http;
using Sonarr.Http.REST;
using Sonarr.Http.REST.Attributes;
7 years ago
using Sonarr.Http.Validation;
namespace Sonarr.Api.V3.Profiles.Delay
{
[V3ApiController]
public class DelayProfileController : RestController<DelayProfileResource>
7 years ago
{
private readonly IDelayProfileService _delayProfileService;
public DelayProfileController(IDelayProfileService delayProfileService, DelayProfileTagInUseValidator tagInUseValidator)
7 years ago
{
_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) =>
7 years ago
{
if (!delayProfile.EnableUsenet && !delayProfile.EnableTorrent)
{
context.AddFailure("Either Usenet or Torrent should be enabled");
7 years ago
}
});
}
[RestPostById]
2 years ago
[Consumes("application/json")]
public ActionResult<DelayProfileResource> Create(DelayProfileResource resource)
7 years ago
{
var model = resource.ToModel();
model = _delayProfileService.Add(model);
return Created(model.Id);
7 years ago
}
[RestDeleteById]
public void DeleteProfile(int id)
7 years ago
{
if (id == 1)
{
throw new MethodNotAllowedException("Cannot delete global delay profile");
}
_delayProfileService.Delete(id);
}
[RestPutById]
2 years ago
[Consumes("application/json")]
public ActionResult<DelayProfileResource> Update(DelayProfileResource resource)
7 years ago
{
var model = resource.ToModel();
_delayProfileService.Update(model);
return Accepted(model.Id);
7 years ago
}
protected override DelayProfileResource GetResourceById(int id)
7 years ago
{
return _delayProfileService.Get(id).ToResource();
}
[HttpGet]
2 years ago
[Produces("application/json")]
public List<DelayProfileResource> GetAll()
7 years ago
{
return _delayProfileService.All().ToResource();
}
[HttpPut("reorder/{id}")]
public List<DelayProfileResource> Reorder([FromRoute] int id, int? after)
7 years ago
{
ValidateId(id);
return _delayProfileService.Reorder(id, after).ToResource();
7 years ago
}
}
3 years ago
}