From c9e5a8aec8f24d4fc50d3ec5ee07b1021320271c Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 24 May 2016 17:18:34 +0100 Subject: [PATCH] Fixed api --- PlexRequests.UI/Modules/ApiRequestModule.cs | 21 +- .../Modules/ApiSettingsMetadataModule.cs | 19 ++ PlexRequests.UI/Modules/ApiSettingsModule.cs | 185 ++++++++++++++---- 3 files changed, 177 insertions(+), 48 deletions(-) diff --git a/PlexRequests.UI/Modules/ApiRequestModule.cs b/PlexRequests.UI/Modules/ApiRequestModule.cs index 199cb9814..6e56ce6ff 100644 --- a/PlexRequests.UI/Modules/ApiRequestModule.cs +++ b/PlexRequests.UI/Modules/ApiRequestModule.cs @@ -28,7 +28,11 @@ using System; using System.Collections.Generic; using Nancy; +using Nancy.Extensions; using Nancy.ModelBinding; +using Nancy.Validation; + +using Newtonsoft.Json; using PlexRequests.Core; using PlexRequests.Core.SettingModels; @@ -84,14 +88,13 @@ namespace PlexRequests.UI.Modules public Response CreateRequest() { - var request = this.BindAndValidate(); - - if (!ModelValidationResult.IsValid) + var request = JsonConvert.DeserializeObject(Request.Body.AsString()); + var a = this.Validate(request); + if (!a.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(a); } - var apiModel = new ApiModel(); var result = RequestService.AddRequest(request); @@ -109,11 +112,11 @@ namespace PlexRequests.UI.Modules public Response UpdateRequest() { - var request = this.BindAndValidate(); - - if (!ModelValidationResult.IsValid) + var request = JsonConvert.DeserializeObject(Request.Body.AsString()); + var a = this.Validate(request); + if (!a.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(a); } diff --git a/PlexRequests.UI/Modules/ApiSettingsMetadataModule.cs b/PlexRequests.UI/Modules/ApiSettingsMetadataModule.cs index 7bb88faaa..1c7e41b38 100644 --- a/PlexRequests.UI/Modules/ApiSettingsMetadataModule.cs +++ b/PlexRequests.UI/Modules/ApiSettingsMetadataModule.cs @@ -150,6 +150,25 @@ namespace PlexRequests.UI.Modules with.BodyParam("headphones settings", true); with.Notes("Saves the headphones settings saved in the application"); }); + + Describe["GetPlexRequestSettings"] = description => description.AsSwagger(with => + { + with.ResourcePath("/settings/plexrequest"); + with.Summary("Gets the plexrequest settings saved in the application"); + with.Model>(); + with.Notes("Gets the plexrequest settings saved in the application"); + with.QueryParam("apikey", "The Api Key found in the settings", true); + }); + + Describe["PostPlexRequestSettings"] = description => description.AsSwagger(with => + { + with.ResourcePath("/settings/plexrequest"); + with.Summary("Saves the plexrequest settings saved in the application"); + with.Model>(); + with.QueryParam("apikey", "The Api Key found in the settings", true); + with.BodyParam("plexrequest settings", true); + with.Notes("Saves the plexrequest settings saved in the application"); + }); } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/ApiSettingsModule.cs b/PlexRequests.UI/Modules/ApiSettingsModule.cs index 984986399..4d84a2c4a 100644 --- a/PlexRequests.UI/Modules/ApiSettingsModule.cs +++ b/PlexRequests.UI/Modules/ApiSettingsModule.cs @@ -24,10 +24,15 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion +using System; using System.Web.UI.WebControls; using Nancy; +using Nancy.Extensions; using Nancy.ModelBinding; +using Nancy.Validation; + +using Newtonsoft.Json; using PlexRequests.Core; using PlexRequests.Core.SettingModels; @@ -40,8 +45,11 @@ namespace PlexRequests.UI.Modules ISettingsService plexSettings, ISettingsService cp, ISettingsService sonarr, ISettingsService sr, ISettingsService hp) : base("api", pr) { - Get["GetAuthSettings","/settings/authentication"] = x => GetAuthSettings(); - Post["PostAuthSettings","/settings/authentication"] = x => PostAuthSettings(); + Get["GetAuthSettings", "/settings/authentication"] = x => GetAuthSettings(); + Post["PostAuthSettings", "/settings/authentication"] = x => PostAuthSettings(); + + Get["GetPlexRequestSettings", "/settings/plexrequest"] = x => GetPrSettings(); + Post["PostPlexRequestSettings", "/settings/plexrequest"] = x => PostPrSettings(); Get["GetPlexSettings", "/settings/plex"] = x => GetPlexSettings(); Post["PostPlexSettings", "/settings/plex"] = x => PostPlexSettings(); @@ -66,7 +74,7 @@ namespace PlexRequests.UI.Modules SickRageSettings = sr; HeadphonesSettings = hp; } - + private ISettingsService SettingsService { get; } private ISettingsService AuthSettings { get; } private ISettingsService PlexSettings { get; } @@ -75,20 +83,69 @@ namespace PlexRequests.UI.Modules private ISettingsService SickRageSettings { get; } private ISettingsService HeadphonesSettings { get; } + private Response GetPrSettings() + { + var model = new ApiModel(); + try + { + var settings = SettingsService.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } + } + + private Response PostPrSettings() + { + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) + { + return ReturnValidationReponse(result); + } + + var model = new ApiModel(); + var settings = SettingsService.SaveSettings(newSettings); + if (settings) + { + model.Data = true; + return ReturnReponse(model); + } + + model.Error = true; + model.ErrorMessage = "Could not update the settings"; + return ReturnReponse(model); + } + private Response GetAuthSettings() { var model = new ApiModel(); - var settings = AuthSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = AuthSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostAuthSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel(); @@ -107,17 +164,27 @@ namespace PlexRequests.UI.Modules private Response GetPlexSettings() { var model = new ApiModel(); - var settings = PlexSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = PlexSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostPlexSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel(); @@ -136,17 +203,27 @@ namespace PlexRequests.UI.Modules private Response GetCpSettings() { var model = new ApiModel(); - var settings = CpSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = CpSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostCpSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel(); @@ -165,17 +242,27 @@ namespace PlexRequests.UI.Modules private Response GetSonarrSettings() { var model = new ApiModel(); - var settings = SonarrSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = SonarrSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostSonarrSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel(); @@ -194,17 +281,27 @@ namespace PlexRequests.UI.Modules private Response GetSickRageSettings() { var model = new ApiModel(); - var settings = SickRageSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = SickRageSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostSickRageSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel(); @@ -222,17 +319,27 @@ namespace PlexRequests.UI.Modules private Response GetHeadphonesSettings() { var model = new ApiModel(); - var settings = HeadphonesSettings.GetSettings(); - model.Data = settings; - return ReturnReponse(model); + try + { + var settings = HeadphonesSettings.GetSettings(); + model.Data = settings; + return ReturnReponse(model); + } + catch (Exception e) + { + model.ErrorMessage = e.Message; + model.Error = true; + return ReturnReponse(model); + } } private Response PostHeadphonesSettings() { - var newSettings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) + var newSettings = JsonConvert.DeserializeObject(Request.Body.AsString()); + var result = this.Validate(newSettings); + if (!result.IsValid) { - return ReturnValidationReponse(ModelValidationResult); + return ReturnValidationReponse(result); } var model = new ApiModel();