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