1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Api/NzbDroneRestModule.cs

55 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using NzbDrone.Api.REST;
2013-04-21 01:14:41 +03:00
using NzbDrone.Api.Validation;
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-09-11 09:33:47 +03:00
protected string Resource { get; private set; }
protected NzbDroneRestModule()
: this(new TResource().ResourceName)
{
}
2013-04-21 01:14:41 +03:00
protected NzbDroneRestModule(string resource)
: base("/api/" + resource.Trim('/'))
{
2013-09-11 09:33:47 +03:00
Resource = resource;
2013-04-21 01:14:41 +03:00
PostValidator.RuleFor(r => r.Id).IsZero();
PutValidator.RuleFor(r => r.Id).ValidId();
}
protected int GetNewId<TModel>(Func<TModel, TModel> function, TResource resource) where TModel : ModelBase, new()
{
var model = resource.InjectTo<TModel>();
function(model);
return model.Id;
}
2013-09-11 09:33:47 +03:00
protected List<TResource> ToListResource<TModel>(Func<IEnumerable<TModel>> function) where TModel : class
{
var modelList = function();
return modelList.InjectTo<List<TResource>>();
}
2013-05-13 09:12:19 +03: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 01:14:41 +03:00
}
}