1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Api/NzbDroneRestModule.cs

66 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-05-22 02:58:57 +02:00
using Nancy.Security;
using NzbDrone.Api.REST;
2013-04-21 00:14:41 +02:00
using NzbDrone.Api.Validation;
using NzbDrone.Core.Datastore;
using NzbDrone.Api.Mapping;
2013-04-21 00:14:41 +02:00
namespace NzbDrone.Api
{
public abstract class NzbDroneRestModule<TResource> : RestModule<TResource> where TResource : RestResource, new()
{
protected NzbDroneRestModule()
: this(new TResource().ResourceName)
{
}
2013-04-21 00:14:41 +02:00
protected NzbDroneRestModule(string resource)
: base("/api/" + resource.Trim('/'))
{
PostValidator.RuleFor(r => r.Id).IsZero();
PutValidator.RuleFor(r => r.Id).ValidId();
}
protected TResource Apply<TModel>(Func<TModel, TModel> function, TResource resource) where TModel : ModelBase, new()
{
var model = resource.InjectTo<TModel>();
function(model);
return model.InjectTo<TResource>();
}
2013-04-25 06:27:49 +02:00
protected List<TResource> ApplyToList<TModel>(Func<IEnumerable<TModel>> function) where TModel : ModelBase, new()
{
var modelList = function();
return modelList.InjectTo<List<TResource>>();
}
2013-04-25 06:27:49 +02:00
protected TResource Apply<TModel>(Func<TModel> function) where TModel : ModelBase, new()
{
var modelList = function();
return modelList.InjectTo<TResource>();
}
protected TResource Apply<TModel>(Func<int, TModel> action, int id) where TModel : ModelBase, new()
{
var model = action(id);
return model.InjectTo<TResource>();
}
2013-05-13 08:12:19 +02:00
protected PagingResource<TResource> ApplyToPage<TModel>(Func<PagingSpec<TModel>, PagingSpec<TModel>> function, PagingSpec<TModel> pagingSpec) where TModel : ModelBase, new()
{
pagingSpec = function(pagingSpec);
return new PagingResource<TResource>
{
Page = pagingSpec.Page,
PageSize = pagingSpec.PageSize,
SortDirection = pagingSpec.SortDirection,
SortKey = pagingSpec.SortKey,
TotalRecords = pagingSpec.TotalRecords,
Records = pagingSpec.Records.InjectTo<List<TResource>>()
};
}
2013-04-21 00:14:41 +02:00
}
}