2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
2019-09-02 20:27:49 +00:00
|
|
|
using FluentValidation;
|
2015-07-17 06:08:32 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-10-28 04:37:35 +00:00
|
|
|
using NzbDrone.Core.Restrictions;
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
namespace Radarr.Http.RESTrictions
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
public class RestrictionModule : RadarrRestModule<RestrictionResource>
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
|
|
|
private readonly IRestrictionService _restrictionService;
|
|
|
|
|
|
|
|
public RestrictionModule(IRestrictionService restrictionService)
|
|
|
|
{
|
|
|
|
_restrictionService = restrictionService;
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
GetResourceById = GetRestriction;
|
|
|
|
GetResourceAll = GetAllRestrictions;
|
|
|
|
CreateResource = CreateRestriction;
|
|
|
|
UpdateResource = UpdateRestriction;
|
|
|
|
DeleteResource = DeleteRestriction;
|
2015-07-17 06:08:32 +00:00
|
|
|
|
2019-09-02 20:27:49 +00:00
|
|
|
SharedValidator.RuleFor(r => r).Custom((restriction, context) =>
|
2015-07-17 06:08:32 +00:00
|
|
|
{
|
|
|
|
if (restriction.Ignored.IsNullOrWhiteSpace() && restriction.Required.IsNullOrWhiteSpace())
|
|
|
|
{
|
2019-09-02 20:27:49 +00:00
|
|
|
context.AddFailure("Either 'Must contain' or 'Must not contain' is required");
|
2015-07-17 06:08:32 +00:00
|
|
|
}
|
|
|
|
});
|
2014-10-28 04:37:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
private RestrictionResource GetRestriction(int id)
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _restrictionService.Get(id).ToResource();
|
2014-10-28 04:37:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
private List<RestrictionResource> GetAllRestrictions()
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _restrictionService.All().ToResource();
|
2014-10-28 04:37:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
private int CreateRestriction(RestrictionResource resource)
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
return _restrictionService.Add(resource.ToModel()).Id;
|
2014-10-28 04:37:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
private void UpdateRestriction(RestrictionResource resource)
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
_restrictionService.Update(resource.ToModel());
|
2014-10-28 04:37:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
private void DeleteRestriction(int id)
|
2014-10-28 04:37:35 +00:00
|
|
|
{
|
|
|
|
_restrictionService.Delete(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|