Skip to content

Commit

Permalink
Merge pull request #14 from tidusjar/dev
Browse files Browse the repository at this point in the history
Update to 1.2.1
  • Loading branch information
Jamie committed Mar 16, 2016
2 parents 5d4c1fd + d9a3d59 commit 7b0e5e3
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 29 deletions.
3 changes: 1 addition & 2 deletions PlexRequests.UI/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ protected override void ConfigureRequestContainer(TinyIoCContainer container, Na
//container.Register<ISonarrApi, MockSonarrApi>();




container.Register<IPlexApi, PlexApi>();
base.ConfigureRequestContainer(container, context);
}
Expand All @@ -100,6 +98,7 @@ protected override void ApplicationStartup(TinyIoCContainer container, IPipeline

StaticConfiguration.DisableErrorTraces = false;


base.ApplicationStartup(container, pipelines);

// Enable forms auth
Expand Down
59 changes: 59 additions & 0 deletions PlexRequests.UI/Helpers/ValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ValidationHelper.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 System.Linq;

using Nancy.Validation;


using PlexRequests.UI.Models;

namespace PlexRequests.UI.Helpers
{
public static class ValidationHelper
{

/// <summary>
/// This will send the first error as a JsonResponseModel
/// </summary>
/// <param name="result">The result.</param>
/// <returns></returns>
public static JsonResponseModel SendJsonError(this ModelValidationResult result)
{
var errors = result.Errors;
return errors
.Select(e => e.Value.FirstOrDefault())
.Where(modelValidationError => modelValidationError != null)
.Select(modelValidationError =>
new JsonResponseModel
{
Result = false,
Message = modelValidationError.ErrorMessage
})
.FirstOrDefault();
}
}
}
41 changes: 34 additions & 7 deletions PlexRequests.UI/Modules/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
using Nancy.ModelBinding;
using Nancy.Responses.Negotiation;
using Nancy.Security;
using Nancy.Validation;

using NLog;

using PlexRequests.Api.Interfaces;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models;

namespace PlexRequests.UI.Modules
Expand Down Expand Up @@ -214,9 +216,16 @@ private Negotiator CouchPotato()
private Response SaveCouchPotato()
{
var couchPotatoSettings = this.Bind<CouchPotatoSettings>();
CpService.SaveSettings(couchPotatoSettings);
var valid = this.Validate(couchPotatoSettings);
if (!valid.IsValid)
{
return Response.AsJson(valid.SendJsonError());
}

return Context.GetRedirect("~/admin/couchpotato");
var result = CpService.SaveSettings(couchPotatoSettings);
return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for CouchPotato!" }
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
}

private Negotiator Plex()
Expand All @@ -229,9 +238,18 @@ private Negotiator Plex()
private Response SavePlex()
{
var plexSettings = this.Bind<PlexSettings>();
PlexService.SaveSettings(plexSettings);
var valid = this.Validate(plexSettings);
if (!valid.IsValid)
{
return Response.AsJson(valid.SendJsonError());
}


return Context.GetRedirect("~/admin/plex");
var result = PlexService.SaveSettings(plexSettings);

return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Plex!" }
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
}

private Negotiator Sonarr()
Expand All @@ -243,10 +261,19 @@ private Negotiator Sonarr()

private Response SaveSonarr()
{
var plexSettings = this.Bind<SonarrSettings>();
SonarrService.SaveSettings(plexSettings);
var sonarrSettings = this.Bind<SonarrSettings>();

var valid = this.Validate(sonarrSettings);
if (!valid.IsValid)
{
return Response.AsJson(valid.SendJsonError());
}

var result = SonarrService.SaveSettings(sonarrSettings);

return Response.AsJson(new JsonResponseModel { Result = true });
return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Sonarr!" }
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
}

private Response GetSonarrQualityProfiles()
Expand Down
29 changes: 23 additions & 6 deletions PlexRequests.UI/PlexRequests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<HintPath>..\packages\FluentScheduler.3.1.46\lib\net40\FluentScheduler.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentValidation, Version=6.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\FluentValidation.6.2.1.0\lib\Net45\FluentValidation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Humanizer, Version=2.0.1.0, Culture=neutral, PublicKeyToken=979442b78dfc278e, processorArchitecture=MSIL">
<HintPath>..\packages\Humanizer.Core.2.0.1\lib\dotnet\Humanizer.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -109,6 +113,10 @@
<HintPath>..\packages\Nancy.Owin.1.4.1\lib\net40\Nancy.Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Validation.FluentValidation, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Validation.FluentValidation.1.4.1\lib\net40\Nancy.Validation.FluentValidation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.ViewEngines.Razor, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Viewengines.Razor.1.4.1\lib\net40\Nancy.ViewEngines.Razor.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -152,6 +160,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Bootstrapper.cs" />
<Compile Include="Helpers\ValidationHelper.cs" />
<Compile Include="Validators\CouchPotatoValidator.cs" />
<Compile Include="Validators\PlexValidator.cs" />
<Compile Include="Validators\SonarrValidator.cs" />
<Content Include="Content\bootstrap-notify.min.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -373,11 +385,16 @@
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.99.0\build\net46\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.99.0\build\net46\System.Data.SQLite.Core.targets'))" />
</Target>
<Import Project="..\packages\System.Data.SQLite.Core.1.0.99.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.99.0\build\net46\System.Data.SQLite.Core.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
<Target Name="RemoveSatelliteAssemblies" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="@(ReferenceSatellitePaths)" />
</ItemGroup>
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
31 changes: 23 additions & 8 deletions PlexRequests.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,39 @@ static void Main(string[] args)
Console.ReadLine();
Environment.Exit(1);
}
uri = $"http://localhost:{portResult}";
uri = $"http://*:{portResult}";
}

Log.Trace("Getting product version");
WriteOutVersion();

var s = new Setup();
s.SetupDb();
if(string.IsNullOrEmpty(uri))

if (string.IsNullOrEmpty(uri))
uri = GetStartupUri();

using (WebApp.Start<Startup>(uri))
;
var options = new StartOptions(uri)
{
ServerFactory = "Microsoft.Owin.Host.HttpListener"
};
try
{

using (WebApp.Start<Startup>(options))
{
Console.WriteLine($"Request Plex is running on {uri}");
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}

}
catch (Exception e)
{
var a = e.Message;
throw;
}
}

private static void WriteOutVersion()
Expand All @@ -89,13 +104,13 @@ private static void WriteOutVersion()
private static string GetStartupUri()
{
Log.Trace("Getting startup URI");
var uri = "http://localhost:3579/";
var uri = "http://*:3579/";
var service = new SettingsServiceV2<PlexRequestSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var settings = service.GetSettings();
Log.Trace("Port: {0}", settings.Port);
if (settings.Port != 0)
{
uri = $"http://localhost:{settings.Port}";
uri = $"http://*:{settings.Port}";
}

return uri;
Expand Down Expand Up @@ -146,7 +161,7 @@ private static void ConfigureTargets(string connectionString)
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
catch (Exception )
catch (Exception)
{

throw;
Expand Down
42 changes: 42 additions & 0 deletions PlexRequests.UI/Validators/CouchPotatoValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SonarrValidator.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.Core.SettingModels;

namespace PlexRequests.UI.Validators
{
public class CouchPotatoValidator : AbstractValidator<CouchPotatoSettings>
{
public CouchPotatoValidator()
{
RuleFor(request => request.Ip).NotEmpty().WithMessage("You must specify a IP/Host name.");
RuleFor(request => request.Port).NotEmpty().WithMessage("You must specify a Port.");
RuleFor(request => request.ApiKey).NotEmpty().WithMessage("You must specify a Api Key.");
}
}
}
41 changes: 41 additions & 0 deletions PlexRequests.UI/Validators/PlexValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SonarrValidator.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.Core.SettingModels;

namespace PlexRequests.UI.Validators
{
public class PlexValidator : AbstractValidator<PlexSettings>
{
public PlexValidator()
{
RuleFor(request => request.Ip).NotEmpty().WithMessage("You must specify a IP/Host name.");
RuleFor(request => request.Port).NotEmpty().WithMessage("You must specify a Port.");
}
}
}
Loading

0 comments on commit 7b0e5e3

Please sign in to comment.