2013-04-22 03:18:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NzbDrone.Api.REST;
|
2013-04-20 22:14:41 +00:00
|
|
|
|
using NzbDrone.Api.Validation;
|
2013-04-22 03:18:08 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
|
using NzbDrone.Api.Mapping;
|
2013-04-20 22:14:41 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api
|
|
|
|
|
{
|
|
|
|
|
public abstract class NzbDroneRestModule<TResource> : RestModule<TResource> where TResource : RestResource, new()
|
|
|
|
|
{
|
2013-04-22 03:18:08 +00:00
|
|
|
|
protected NzbDroneRestModule()
|
|
|
|
|
: this(new TResource().ResourceName)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 22:14:41 +00:00
|
|
|
|
protected NzbDroneRestModule(string resource)
|
|
|
|
|
: base("/api/" + resource.Trim('/'))
|
|
|
|
|
{
|
|
|
|
|
PostValidator.RuleFor(r => r.Id).IsZero();
|
|
|
|
|
PutValidator.RuleFor(r => r.Id).ValidId();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 03:18:08 +00:00
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
protected TResource ToResource<TModel>(Func<TModel, TModel> function, TResource resource) where TModel : ModelBase, new()
|
2013-04-22 03:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
var model = resource.InjectTo<TModel>();
|
|
|
|
|
function(model);
|
|
|
|
|
return model.InjectTo<TResource>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
protected List<TResource> ToListResource<TModel>(Func<IEnumerable<TModel>> function) where TModel : ModelBase, new()
|
2013-04-22 03:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
var modelList = function();
|
|
|
|
|
return modelList.InjectTo<List<TResource>>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
protected TResource ToResource<TModel>(Func<TModel> function) where TModel : ModelBase, new()
|
2013-04-25 04:27:49 +00:00
|
|
|
|
{
|
|
|
|
|
var modelList = function();
|
|
|
|
|
return modelList.InjectTo<TResource>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:12:20 +00:00
|
|
|
|
protected TResource ToResource<TModel>(Func<int, TModel> action, int id) where TModel : ModelBase, new()
|
2013-04-22 03:18:08 +00:00
|
|
|
|
{
|
|
|
|
|
var model = action(id);
|
|
|
|
|
return model.InjectTo<TResource>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 06:12:19 +00: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-20 22:14:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|