using System; using System.Linq; 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 string Resource { get; private set; } protected NzbDroneRestModule() : this(new TResource().ResourceName) { } protected NzbDroneRestModule(string resource) : base("/api/" + resource.Trim('/')) { Resource = resource; PostValidator.RuleFor(r => r.Id).IsZero(); PutValidator.RuleFor(r => r.Id).ValidId(); } protected int GetNewId(Func function, TResource resource) where TModel : ModelBase, new() { var model = resource.InjectTo(); function(model); return model.Id; } protected List ToListResource(Func> function) where TModel : class { var modelList = function(); return ToListResource(modelList); } protected virtual List ToListResource(IEnumerable modelList) where TModel : class { return modelList.Select(ToResource).ToList(); } protected virtual TResource ToResource(TModel model) where TModel : class { 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 = ToListResource(pagingSpec.Records) }; } } }