2017-09-04 02:20:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
using Nancy;
|
|
|
|
|
using NzbDrone.Core.Profiles.Delay;
|
|
|
|
|
using Lidarr.Http;
|
|
|
|
|
using Lidarr.Http.Extensions;
|
|
|
|
|
using Lidarr.Http.REST;
|
|
|
|
|
using Lidarr.Http.Validation;
|
|
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
|
namespace Lidarr.Api.V1.Profiles.Delay
|
2017-09-04 02:20:56 +00:00
|
|
|
|
{
|
|
|
|
|
public class DelayProfileModule : LidarrRestModule<DelayProfileResource>
|
|
|
|
|
{
|
|
|
|
|
private readonly IDelayProfileService _delayProfileService;
|
|
|
|
|
|
|
|
|
|
public DelayProfileModule(IDelayProfileService delayProfileService, DelayProfileTagInUseValidator tagInUseValidator)
|
|
|
|
|
{
|
|
|
|
|
_delayProfileService = delayProfileService;
|
|
|
|
|
|
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
|
GetResourceById = GetById;
|
|
|
|
|
UpdateResource = Update;
|
|
|
|
|
CreateResource = Create;
|
|
|
|
|
DeleteResource = DeleteProfile;
|
2019-09-12 20:32:51 +00:00
|
|
|
|
Put(@"/reorder/(?<id>[\d]{1,10})", options => Reorder(options.Id));
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2019-09-12 01:26:54 +00:00
|
|
|
|
SharedValidator.RuleFor(d => d).Custom((delayProfile, context) =>
|
2017-09-04 02:20:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (!delayProfile.EnableUsenet && !delayProfile.EnableTorrent)
|
|
|
|
|
{
|
2019-09-12 01:26:54 +00:00
|
|
|
|
context.AddFailure("Either Usenet or Torrent should be enabled");
|
2017-09-04 02:20:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Create(DelayProfileResource resource)
|
|
|
|
|
{
|
|
|
|
|
var model = resource.ToModel();
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var model = resource.ToModel();
|
|
|
|
|
_delayProfileService.Update(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DelayProfileResource GetById(int id)
|
|
|
|
|
{
|
|
|
|
|
return _delayProfileService.Get(id).ToResource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<DelayProfileResource> GetAll()
|
|
|
|
|
{
|
|
|
|
|
return _delayProfileService.All().ToResource();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
|
private object Reorder(int id)
|
2017-09-04 02:20:56 +00:00
|
|
|
|
{
|
|
|
|
|
ValidateId(id);
|
|
|
|
|
|
|
|
|
|
var afterIdQuery = Request.Query.After;
|
|
|
|
|
int? afterId = afterIdQuery.HasValue ? Convert.ToInt32(afterIdQuery.Value) : null;
|
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
|
return _delayProfileService.Reorder(id, afterId).ToResource();
|
2017-09-04 02:20:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|