using System; using System.Collections.Generic; using NzbDrone.Api.REST; using NzbDrone.Api.Validation; using NzbDrone.Core.Datastore; using NzbDrone.Api.Mapping; namespace NzbDrone.Api { public abstract class NzbDroneRestModule : RestModule where TResource : RestResource, new() { protected NzbDroneRestModule() : this(new TResource().ResourceName) { } protected NzbDroneRestModule(string resource) : base("/api/" + resource.Trim('/')) { PostValidator.RuleFor(r => r.Id).IsZero(); PutValidator.RuleFor(r => r.Id).ValidId(); } protected TResource ToResource(Func function, TResource resource) where TModel : ModelBase, new() { var model = resource.InjectTo(); function(model); return model.InjectTo(); } protected List ToListResource(Func> function) where TModel : ModelBase, new() { var modelList = function(); return modelList.InjectTo>(); } protected TResource ToResource(Func function) where TModel : ModelBase, new() { var modelList = function(); return modelList.InjectTo(); } protected TResource ToResource(Func action, int id) where TModel : ModelBase, new() { var model = action(id); return model.InjectTo(); } protected PagingResource ApplyToPage(Func, PagingSpec> function, PagingSpec pagingSpec) where TModel : ModelBase, new() { pagingSpec = function(pagingSpec); return new PagingResource { Page = pagingSpec.Page, PageSize = pagingSpec.PageSize, SortDirection = pagingSpec.SortDirection, SortKey = pagingSpec.SortKey, TotalRecords = pagingSpec.TotalRecords, Records = pagingSpec.Records.InjectTo>() }; } } }