2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
2014-11-24 00:07:46 +00:00
|
|
|
using FluentValidation;
|
|
|
|
using NzbDrone.Core.Profiles.Delay;
|
2018-11-23 07:03:32 +00:00
|
|
|
using Radarr.Http;
|
2019-12-22 22:08:53 +00:00
|
|
|
using Radarr.Http.REST;
|
|
|
|
using Radarr.Http.Validation;
|
2014-11-24 00:07:46 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Profiles.Delay
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
public class DelayProfileModule : RadarrRestModule<DelayProfileResource>
|
2014-11-24 00:07:46 +00:00
|
|
|
{
|
|
|
|
private readonly IDelayProfileService _delayProfileService;
|
|
|
|
|
|
|
|
public DelayProfileModule(IDelayProfileService delayProfileService, DelayProfileTagInUseValidator tagInUseValidator)
|
|
|
|
{
|
|
|
|
_delayProfileService = delayProfileService;
|
|
|
|
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
GetResourceById = GetById;
|
|
|
|
UpdateResource = Update;
|
|
|
|
CreateResource = Create;
|
|
|
|
DeleteResource = DeleteProfile;
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(d => d.Tags).NotEmpty().When(d => d.Id != 1);
|
2015-06-22 05:56:00 +00:00
|
|
|
SharedValidator.RuleFor(d => d.Tags).EmptyCollection<DelayProfileResource, int>().When(d => d.Id == 1);
|
2014-11-24 00:07:46 +00:00
|
|
|
SharedValidator.RuleFor(d => d.Tags).SetValidator(tagInUseValidator);
|
2014-12-03 01:18:17 +00:00
|
|
|
SharedValidator.RuleFor(d => d.UsenetDelay).GreaterThanOrEqualTo(0);
|
|
|
|
SharedValidator.RuleFor(d => d.TorrentDelay).GreaterThanOrEqualTo(0);
|
2016-03-25 00:56:29 +00:00
|
|
|
|
2019-09-02 20:27:49 +00:00
|
|
|
SharedValidator.RuleFor(d => d).Custom((delayProfile, context) =>
|
2016-03-25 00:56:29 +00:00
|
|
|
{
|
|
|
|
if (!delayProfile.EnableUsenet && !delayProfile.EnableTorrent)
|
|
|
|
{
|
2019-09-02 20:27:49 +00:00
|
|
|
context.AddFailure("Either Usenet or Torrent should be enabled");
|
2016-03-25 00:56:29 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-24 00:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private int Create(DelayProfileResource resource)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
var model = resource.ToModel();
|
2014-11-24 00:07:46 +00:00
|
|
|
model = _delayProfileService.Add(model);
|
|
|
|
|
|
|
|
return model.Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteProfile(int id)
|
|
|
|
{
|
|
|
|
if (id == 1)
|
|
|
|
{
|
|
|
|
throw new MethodNotAllowedException("Cannot delete global delay profile");
|
|
|
|
}
|
|
|
|
|
|
|
|
_delayProfileService.Delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Update(DelayProfileResource resource)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
var model = resource.ToModel();
|
|
|
|
_delayProfileService.Update(model);
|
2014-11-24 00:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private DelayProfileResource GetById(int id)
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _delayProfileService.Get(id).ToResource();
|
2014-11-24 00:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<DelayProfileResource> GetAll()
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _delayProfileService.All().ToResource();
|
2014-11-24 00:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|