Radarr/src/NzbDrone.Api/Restrictions/RestrictionModule.cs

61 lines
1.8 KiB
C#
Raw Normal View History

2018-11-23 07:03:32 +00:00
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Restrictions;
2018-11-23 07:03:32 +00:00
using Radarr.Http;
2018-11-23 07:03:32 +00:00
namespace Radarr.Http.RESTrictions
{
2018-11-23 07:03:32 +00:00
public class RestrictionModule : RadarrRestModule<RestrictionResource>
{
private readonly IRestrictionService _restrictionService;
public RestrictionModule(IRestrictionService restrictionService)
{
_restrictionService = restrictionService;
GetResourceById = GetRestriction;
GetResourceAll = GetAllRestrictions;
CreateResource = CreateRestriction;
UpdateResource = UpdateRestriction;
DeleteResource = DeleteRestriction;
SharedValidator.Custom(restriction =>
{
if (restriction.Ignored.IsNullOrWhiteSpace() && restriction.Required.IsNullOrWhiteSpace())
{
2015-07-17 15:06:41 +00:00
return new ValidationFailure("", "Either 'Must contain' or 'Must not contain' is required");
}
return null;
});
}
private RestrictionResource GetRestriction(int id)
{
return _restrictionService.Get(id).ToResource();
}
private List<RestrictionResource> GetAllRestrictions()
{
return _restrictionService.All().ToResource();
}
private int CreateRestriction(RestrictionResource resource)
{
return _restrictionService.Add(resource.ToModel()).Id;
}
private void UpdateRestriction(RestrictionResource resource)
{
_restrictionService.Update(resource.ToModel());
}
private void DeleteRestriction(int id)
{
_restrictionService.Delete(id);
}
}
}