2013-04-20 03:05:28 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-04-21 01:14:41 +03:00
|
|
|
using FluentValidation;
|
2013-04-20 03:05:28 +03:00
|
|
|
using Nancy;
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-04-21 01:14:41 +03:00
|
|
|
using System.Linq;
|
2013-05-13 09:12:19 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-04-20 03:05:28 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.REST
|
|
|
|
{
|
|
|
|
public abstract class RestModule<TResource> : NancyModule
|
2013-04-20 23:16:33 +03:00
|
|
|
where TResource : RestResource, new()
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
|
|
|
private const string ROOT_ROUTE = "/";
|
2013-04-26 09:12:11 +03:00
|
|
|
private const string ID_ROUTE = @"/(?<id>[\d]{1,7})";
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
private Action<int> _deleteResource;
|
|
|
|
private Func<int, TResource> _getResourceById;
|
|
|
|
private Func<List<TResource>> _getResourceAll;
|
2013-05-13 09:12:19 +03:00
|
|
|
private Func<PagingResource<TResource>, PagingResource<TResource>> _getResourcePaged;
|
2013-04-25 07:27:49 +03:00
|
|
|
private Func<TResource> _getResourceSingle;
|
2013-04-22 06:18:08 +03:00
|
|
|
private Func<TResource, TResource> _createResource;
|
|
|
|
private Func<TResource, TResource> _updateResource;
|
|
|
|
|
|
|
|
protected ResourceValidator<TResource> PostValidator { get; private set; }
|
|
|
|
protected ResourceValidator<TResource> PutValidator { get; private set; }
|
|
|
|
protected ResourceValidator<TResource> SharedValidator { get; private set; }
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-04-28 03:25:28 +03:00
|
|
|
protected void ValidateId(int id)
|
|
|
|
{
|
|
|
|
if (id <= 0)
|
|
|
|
{
|
|
|
|
throw new BadRequestException(id + " is not a valid ID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 03:05:28 +03:00
|
|
|
protected RestModule(string modulePath)
|
|
|
|
: base(modulePath)
|
|
|
|
{
|
2013-04-21 01:14:41 +03:00
|
|
|
|
|
|
|
PostValidator = new ResourceValidator<TResource>();
|
|
|
|
PutValidator = new ResourceValidator<TResource>();
|
|
|
|
SharedValidator = new ResourceValidator<TResource>();
|
2013-04-22 06:18:08 +03:00
|
|
|
}
|
2013-04-21 01:14:41 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
protected Action<int> DeleteResource
|
|
|
|
{
|
|
|
|
private get { return _deleteResource; }
|
|
|
|
set
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
_deleteResource = value;
|
|
|
|
Delete[ID_ROUTE] = options =>
|
|
|
|
{
|
2013-04-28 03:25:28 +03:00
|
|
|
ValidateId(options.Id);
|
2013-04-22 06:18:08 +03:00
|
|
|
DeleteResource((int)options.Id);
|
2013-05-29 09:24:45 +03:00
|
|
|
|
|
|
|
return new object().AsResponse();
|
2013-04-22 06:18:08 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
protected Func<int, TResource> GetResourceById
|
|
|
|
{
|
|
|
|
private get { return _getResourceById; }
|
|
|
|
set
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
_getResourceById = value;
|
|
|
|
Get[ID_ROUTE] = options =>
|
2013-04-26 09:12:11 +03:00
|
|
|
{
|
2013-04-28 03:25:28 +03:00
|
|
|
ValidateId(options.Id);
|
2013-07-10 01:06:30 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var resource = GetResourceById((int)options.Id);
|
2013-07-12 22:13:16 +03:00
|
|
|
|
|
|
|
if (resource == null)
|
|
|
|
{
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
2013-07-10 01:06:30 +03:00
|
|
|
return resource.AsResponse();
|
|
|
|
}
|
|
|
|
catch (ModelNotFoundException)
|
|
|
|
{
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
2013-04-26 09:12:11 +03:00
|
|
|
};
|
2013-04-22 06:18:08 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
protected Func<List<TResource>> GetResourceAll
|
|
|
|
{
|
|
|
|
private get { return _getResourceAll; }
|
|
|
|
set
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
_getResourceAll = value;
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
Get[ROOT_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var resource = GetResourceAll();
|
|
|
|
return resource.AsResponse();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 03:05:28 +03:00
|
|
|
|
2013-05-13 09:12:19 +03:00
|
|
|
protected Func<PagingResource<TResource>, PagingResource<TResource>> GetResourcePaged
|
|
|
|
{
|
|
|
|
private get { return _getResourcePaged; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_getResourcePaged = value;
|
|
|
|
|
|
|
|
Get[ROOT_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var resource = GetResourcePaged(ReadPagingResourceFromRequest());
|
|
|
|
return resource.AsResponse();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 07:27:49 +03:00
|
|
|
protected Func<TResource> GetResourceSingle
|
|
|
|
{
|
|
|
|
private get { return _getResourceSingle; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_getResourceSingle = value;
|
|
|
|
|
|
|
|
Get[ROOT_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var resource = GetResourceSingle();
|
|
|
|
return resource.AsResponse();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
protected Func<TResource, TResource> CreateResource
|
|
|
|
{
|
|
|
|
private get { return _createResource; }
|
|
|
|
set
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
_createResource = value;
|
|
|
|
Post[ROOT_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var resource = CreateResource(ReadFromRequest());
|
|
|
|
return resource.AsResponse(HttpStatusCode.Created);
|
|
|
|
};
|
2013-04-20 23:16:33 +03:00
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
}
|
2013-04-20 03:05:28 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 06:18:08 +03:00
|
|
|
protected Func<TResource, TResource> UpdateResource
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
private get { return _updateResource; }
|
|
|
|
set
|
2013-04-20 03:05:28 +03:00
|
|
|
{
|
2013-04-22 06:18:08 +03:00
|
|
|
_updateResource = value;
|
|
|
|
Put[ROOT_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var resource = UpdateResource(ReadFromRequest());
|
|
|
|
return resource.AsResponse(HttpStatusCode.Accepted);
|
|
|
|
};
|
2013-05-25 00:28:13 +03:00
|
|
|
|
|
|
|
Put[ID_ROUTE] = options =>
|
|
|
|
{
|
|
|
|
var model = ReadFromRequest();
|
|
|
|
model.Id = options.Id;
|
|
|
|
var resource = UpdateResource(model);
|
|
|
|
return resource.AsResponse(HttpStatusCode.Accepted);
|
|
|
|
};
|
2013-04-20 03:05:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private TResource ReadFromRequest()
|
|
|
|
{
|
2013-05-11 08:59:42 +03:00
|
|
|
//TODO: handle when request is null
|
2013-04-20 03:05:28 +03:00
|
|
|
var resource = Request.Body.FromJson<TResource>();
|
|
|
|
|
2013-05-25 00:28:13 +03:00
|
|
|
if (resource == null)
|
|
|
|
{
|
|
|
|
throw new BadRequestException("Request body can't be empty");
|
|
|
|
}
|
|
|
|
|
2013-04-21 01:14:41 +03:00
|
|
|
var errors = SharedValidator.Validate(resource).Errors.ToList();
|
|
|
|
|
2013-04-20 03:05:28 +03:00
|
|
|
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2013-04-21 01:14:41 +03:00
|
|
|
errors.AddRange(PostValidator.Validate(resource).Errors);
|
2013-04-20 03:05:28 +03:00
|
|
|
}
|
|
|
|
else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2013-04-21 01:14:41 +03:00
|
|
|
errors.AddRange(PutValidator.Validate(resource).Errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.Any())
|
|
|
|
{
|
|
|
|
throw new ValidationException(errors);
|
2013-04-20 03:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
2013-05-13 09:12:19 +03:00
|
|
|
|
|
|
|
private PagingResource<TResource> ReadPagingResourceFromRequest()
|
|
|
|
{
|
|
|
|
int pageSize;
|
2013-05-31 03:12:20 +03:00
|
|
|
Int32.TryParse(Request.Query.PageSize.ToString(), out pageSize);
|
2013-05-13 09:12:19 +03:00
|
|
|
if (pageSize == 0) pageSize = 10;
|
2013-05-31 03:12:20 +03:00
|
|
|
|
2013-05-13 09:12:19 +03:00
|
|
|
int page;
|
2013-05-31 03:12:20 +03:00
|
|
|
Int32.TryParse(Request.Query.Page.ToString(), out page);
|
2013-05-13 09:12:19 +03:00
|
|
|
if (page == 0) page = 1;
|
|
|
|
|
|
|
|
|
|
|
|
var pagingResource = new PagingResource<TResource>
|
2013-06-05 03:49:53 +03:00
|
|
|
{
|
|
|
|
PageSize = pageSize,
|
|
|
|
Page = page,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Request.Query.SortKey != null)
|
|
|
|
{
|
|
|
|
pagingResource.SortKey = Request.Query.SortKey.ToString();
|
|
|
|
|
|
|
|
if (Request.Query.SortDir != null)
|
|
|
|
{
|
|
|
|
pagingResource.SortDirection = Request.Query.SortDir.ToString()
|
|
|
|
.Equals("Asc", StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
? SortDirection.Ascending
|
|
|
|
: SortDirection.Descending;
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 09:12:19 +03:00
|
|
|
|
|
|
|
return pagingResource;
|
|
|
|
}
|
2013-04-20 03:05:28 +03:00
|
|
|
}
|
|
|
|
}
|