1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-26 01:38:24 +00:00

Introducing Fancy.Nx

This commit is contained in:
Keivan Beigi 2013-04-19 17:05:28 -07:00
parent 8a52b8da02
commit 55ece3d80d
4 changed files with 135 additions and 3 deletions

View file

@ -90,6 +90,8 @@
<Compile Include="Resolvers\EndTimeResolver.cs" />
<Compile Include="Resolvers\NextAiringResolver.cs" />
<Compile Include="Resolvers\NullableDatetimeToString.cs" />
<Compile Include="REST\RestModule.cs" />
<Compile Include="REST\RestResource.cs" />
<Compile Include="RootFolders\RootFolderModule.cs" />
<Compile Include="Seasons\SeasonModule.cs" />
<Compile Include="Series\SeriesResource.cs" />

View file

@ -1,11 +1,10 @@
using System;
using System.Linq;
using NzbDrone.Api.REST;
namespace NzbDrone.Api.QualityType
{
public class QualitySizeResource
public class QualitySizeResource : RestResource<QualitySizeResource>
{
public Int32 Id { get; set; }
public String Name { get; set; }
public Int32 MinSize { get; set; }
public Int32 MaxSize { get; set; }

View file

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using Nancy;
using NzbDrone.Api.Extensions;
namespace NzbDrone.Api.REST
{
public abstract class RestModule<TResource> : NancyModule
where TResource : RestResource<TResource>, new()
{
private const string ROOT_ROUTE = "/";
private const string ID_ROUTE = "/{id}";
protected RestModule()
: this(new TResource().ResourceName)
{
}
protected RestModule(string modulePath)
: base(modulePath)
{
Get[ROOT_ROUTE] = options =>
{
EnsureImplementation(GetResourceAll);
var resource = GetResourceAll();
return resource.AsResponse();
};
Get[ID_ROUTE] = options =>
{
EnsureImplementation(GetResourceById);
var resource = GetResourceById(options.Id);
return resource.AsResponse();
};
Post[ROOT_ROUTE] = options =>
{
EnsureImplementation(CreateResource);
var resource = CreateResource(ReadFromRequest());
return resource.AsResponse(HttpStatusCode.Created);
};
Put[ROOT_ROUTE] = options =>
{
EnsureImplementation(UpdateResource);
var resource = UpdateResource(ReadFromRequest());
return resource.AsResponse(HttpStatusCode.Accepted);
};
Delete[ID_ROUTE] = options =>
{
EnsureImplementation(DeleteResource);
DeleteResource(options.Id);
return new Response { StatusCode = HttpStatusCode.OK };
};
}
protected Action<int> DeleteResource { get; set; }
protected Func<int, TResource> GetResourceById { get; set; }
protected Func<List<TResource>> GetResourceAll { get; set; }
protected Func<TResource, TResource> CreateResource { get; set; }
protected Func<TResource, TResource> UpdateResource { get; set; }
private void EnsureImplementation(Delegate implementation)
{
if (implementation == null)
{
throw new NotImplementedException();
}
}
private TResource ReadFromRequest()
{
var resource = Request.Body.FromJson<TResource>();
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
{
resource.ValidateForPost();
}
else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase))
{
resource.ValidateForPut();
}
return resource;
}
}
}

View file

@ -0,0 +1,42 @@
using FluentValidation;
namespace NzbDrone.Api.REST
{
public abstract class RestResource<T>
where T : RestResource<T>, new()
{
public int Id { get; set; }
public virtual string ResourceName
{
get
{
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);
}
}
}