More work on the api and documentation #222 #205

pull/226/head
tidusjar 8 years ago
parent 9b4ae2c486
commit 187a59261a

@ -0,0 +1,84 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ApiModuleTests.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Moq;
using Nancy;
using Nancy.Testing;
using NUnit.Framework;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Modules;
namespace PlexRequests.UI.Tests
{
[TestFixture]
[Ignore("Locator :(")]
public class ApiModuleTests
{
private ConfigurableBootstrapper Bootstrapper { get; set; }
[SetUp]
public void Setup()
{
var requestMock = new Mock<IRequestService>();
var settingsMock = new Mock<ISettingsService<PlexRequestSettings>>();
Bootstrapper = new ConfigurableBootstrapper(with =>
{
with.Module<ApiModule>();
with.Dependency(requestMock.Object);
with.Dependency(settingsMock.Object);
with.ApplicationStartup(
(c, a) =>
{
var loc = ServiceLocator.Instance;
loc.SetContainer(c);
});
});
}
[Test]
public void GetAllRequests()
{
var browser = new Browser(Bootstrapper);
var result = browser.Post("/api/requests", with =>
{
with.HttpRequest();
with.Header("Accept", "application/json");
with.Query("apikey","a");
});
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
}
}
}

@ -90,6 +90,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="ApiModuleTests.cs" />
<Compile Include="BootstrapperExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestRootPathProvider.cs" />

@ -34,6 +34,10 @@ namespace PlexRequests.UI.ModelDataProviders
{
public class RequestedModelDataProvider : ISwaggerModelDataProvider
{
/// <summary>
/// Gets the model data for the api documentation.
/// </summary>
/// <returns></returns>
public SwaggerModelData GetModelData()
{
return SwaggerModelData.ForType<RequestedModel>(with =>

@ -42,11 +42,23 @@ namespace PlexRequests.UI.Modules
with.ResourcePath("/requests");
with.Summary("The list of requests");
with.Notes("This returns a list of users from our awesome app");
with.Notes("This returns a list of requests");
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
with.Model<ApiModel<List<RequestedModel>>>();
});
Describe["GetRequest"] = description => description.AsSwagger(with =>
{
with.ResourcePath("/requests/{id}");
with.Summary("Get's a single request");
with.Notes("This returns a single request");
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
//with.QueryParam<int>("id", "The request id to return", true);
with.PathParam<int>("id");
with.Model<ApiModel<List<RequestedModel>>>();
});
Describe["PostRequests"] = description => description.AsSwagger(with =>
{
with.ResourcePath("/requests");
@ -72,7 +84,7 @@ namespace PlexRequests.UI.Modules
with.ResourcePath("/requests");
with.Summary("Deletes an existing request");
with.Model<ApiModel<bool>>();
with.BodyParam<RequestedModel>("The request", true);
with.BodyParam<int>("The request ID to delete", true);
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
with.Notes("Deletes an existing request. If the request doesn't exist we will return an error.");
});

@ -29,8 +29,6 @@ using System.Collections.Generic;
using Nancy;
using Nancy.ModelBinding;
using Nancy.Responses.Negotiation;
using Nancy.Validation;
using PlexRequests.Core;
using PlexRequests.Store;
@ -42,6 +40,7 @@ namespace PlexRequests.UI.Modules
public ApiModule(IRequestService service) : base("api")
{
Get["GetRequests","/requests"] = x => GetRequests();
Get["GetRequest","/requests/{id}"] = x => GetSingleRequests(x);
Post["PostRequests", "/requests"] = x => CreateRequest();
Put["PutRequests", "/requests"] = x => UpdateRequest();
Delete["DeleteRequests", "/requests"] = x => DeleteRequest();
@ -61,15 +60,33 @@ namespace PlexRequests.UI.Modules
return ReturnReponse(apiModel);
}
public Response GetSingleRequests(dynamic x)
{
var id = (int)x.id;
var apiModel = new ApiModel<List<RequestedModel>> { Data = new List<RequestedModel>() };
var requests = RequestService.Get(id);
if (string.IsNullOrEmpty(requests.Title))
{
apiModel.Error = true;
apiModel.ErrorMessage = "Request does not exist";
return ReturnReponse(apiModel);
}
apiModel.Data.Add(requests);
return ReturnReponse(apiModel);
}
public Response CreateRequest()
{
var request = this.Bind<RequestedModel>();
var valid = this.Validate(request);
if (!valid.IsValid)
var request = this.BindAndValidate<RequestedModel>();
if (!ModelValidationResult.IsValid)
{
return ReturnValidationReponse(valid);
return ReturnValidationReponse(ModelValidationResult);
}
var apiModel = new ApiModel<bool>();
var result = RequestService.AddRequest(request);
@ -87,11 +104,11 @@ namespace PlexRequests.UI.Modules
public Response UpdateRequest()
{
var request = this.Bind<RequestedModel>();
var valid = this.Validate(request);
if (!valid.IsValid)
var request = this.BindAndValidate<RequestedModel>();
if (!ModelValidationResult.IsValid)
{
return ReturnValidationReponse(valid);
return ReturnValidationReponse(ModelValidationResult);
}
@ -112,17 +129,20 @@ namespace PlexRequests.UI.Modules
public Response DeleteRequest()
{
var request = this.Bind<RequestedModel>();
var valid = this.Validate(request);
if (!valid.IsValid)
{
return ReturnValidationReponse(valid);
}
var id = this.Bind<int>();
var apiModel = new ApiModel<bool>();
try
{
RequestService.DeleteRequest(request);
var exisitingRequest = RequestService.Get(id);
if (exisitingRequest == null)
{
apiModel.Error = true;
apiModel.ErrorMessage = $"The request id {id} does not exist";
return ReturnReponse(apiModel);
}
RequestService.DeleteRequest(exisitingRequest);
apiModel.Data = true;
return ReturnReponse(apiModel);

@ -185,6 +185,7 @@
<Compile Include="Validators\EmailNotificationSettingsValidator.cs" />
<Compile Include="Validators\CouchPotatoValidator.cs" />
<Compile Include="Validators\PlexValidator.cs" />
<Compile Include="Validators\RequestedModelValidator.cs" />
<Compile Include="Validators\SickRageValidator.cs" />
<Compile Include="Validators\SonarrValidator.cs" />
<Content Include="Content\bootstrap-notify.min.js">

@ -0,0 +1,45 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: RequestedModelValidator.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using FluentValidation;
using PlexRequests.Store;
namespace PlexRequests.UI.Validators
{
public class RequestedModelValidator : AbstractValidator<RequestedModel>
{
public RequestedModelValidator()
{
RuleFor(x => x.Title).NotNull();
RuleFor(x => x.ProviderId).NotNull().WithMessage("'ProviderId' must not be empty. Please use either TVMaze Id or TheMovieDb Id");
RuleFor(x => x.PosterPath).NotNull();
RuleFor(x => x.ReleaseDate).NotNull();
RuleFor(x => x.Type).NotNull();
RuleFor(x => x.RequestedUsers).NotNull();
}
}
}
Loading…
Cancel
Save