moving validation rules to restmodule.

This commit is contained in:
kay.one 2013-04-20 13:16:33 -07:00
parent cc11e3760a
commit 373a93be64
5 changed files with 10 additions and 33 deletions

View File

@ -3,7 +3,7 @@ using NzbDrone.Api.REST;
namespace NzbDrone.Api.QualityType namespace NzbDrone.Api.QualityType
{ {
public class QualitySizeResource : RestResource<QualitySizeResource> public class QualitySizeResource : RestResource
{ {
public String Name { get; set; } public String Name { get; set; }
public Int32 MinSize { get; set; } public Int32 MinSize { get; set; }

View File

@ -6,7 +6,7 @@ using NzbDrone.Api.Extensions;
namespace NzbDrone.Api.REST namespace NzbDrone.Api.REST
{ {
public abstract class RestModule<TResource> : NancyModule public abstract class RestModule<TResource> : NancyModule
where TResource : RestResource<TResource>, new() where TResource : RestResource, new()
{ {
private const string ROOT_ROUTE = "/"; private const string ROOT_ROUTE = "/";
private const string ID_ROUTE = "/{id}"; private const string ID_ROUTE = "/{id}";
@ -54,6 +54,10 @@ namespace NzbDrone.Api.REST
DeleteResource(options.Id); DeleteResource(options.Id);
return new Response { StatusCode = HttpStatusCode.OK }; return new Response { StatusCode = HttpStatusCode.OK };
}; };
} }
protected Action<int> DeleteResource { get; set; } protected Action<int> DeleteResource { get; set; }
@ -76,11 +80,11 @@ namespace NzbDrone.Api.REST
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase)) if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
{ {
resource.ValidateForPost(); //resource.ValidateForPost();
} }
else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase)) else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase))
{ {
resource.ValidateForPut(); //resource.ValidateForPut();
} }
return resource; return resource;

View File

@ -2,10 +2,8 @@
namespace NzbDrone.Api.REST namespace NzbDrone.Api.REST
{ {
public abstract class RestResource<T> public abstract class RestResource
where T : RestResource<T>, new()
{ {
public int Id { get; set; } public int Id { get; set; }
public virtual string ResourceName public virtual string ResourceName
@ -15,28 +13,5 @@ namespace NzbDrone.Api.REST
return GetType().Name.ToLower(); return GetType().Name.ToLower();
} }
} }
protected AbstractValidator<T> PostValidator { get; private set; }
protected AbstractValidator<T> PutValidator { get; private set; }
public RestResource()
{
PostValidator = new InlineValidator<T>();
PutValidator = new InlineValidator<T>();
PostValidator.RuleFor(m => m.Id).Equal(0);
PutValidator.RuleFor(m => m.Id).GreaterThan(0);
}
public void ValidateForPost()
{
PostValidator.ValidateAndThrow((T)this);
}
public void ValidateForPut()
{
PutValidator.ValidateAndThrow((T)this);
}
} }
} }

View File

@ -4,7 +4,7 @@ using NzbDrone.Api.REST;
namespace NzbDrone.Api.Series namespace NzbDrone.Api.Series
{ {
public class SeriesResource : RestResource<SeriesResource> public class SeriesResource : RestResource
{ {
//Todo: Sorters should be done completely on the client //Todo: Sorters should be done completely on the client
//Todo: Is there an easy way to keep IgnoreArticlesWhenSorting in sync between, Series, History, Missing? //Todo: Is there an easy way to keep IgnoreArticlesWhenSorting in sync between, Series, History, Missing?

View File

@ -3,10 +3,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Model;
using NzbDrone.Core.Download.Clients.Nzbget; using NzbDrone.Core.Download.Clients.Nzbget;
using NzbDrone.Core.Download.Clients.Sabnzbd; using NzbDrone.Core.Download.Clients.Sabnzbd;
using NzbDrone.Core.Providers;
namespace NzbDrone.Core.Configuration namespace NzbDrone.Core.Configuration
{ {