Using the IoC container now

This commit is contained in:
tidusjar 2016-02-29 14:20:30 +00:00
commit 6fd1e3ba32
27 changed files with 636 additions and 88 deletions

View file

@ -1,9 +1,16 @@
using Nancy;
using Mono.Data.Sqlite;
using Nancy;
using Nancy.Authentication.Forms;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;
using RequestPlex.Core;
using RequestPlex.Core.SettingModels;
using RequestPlex.Helpers;
using RequestPlex.Store;
using RequestPlex.Store.Models;
using RequestPlex.Store.Repository.NZBDash.DataAccessLayer.Repository;
using FormsAuthentication = Nancy.Authentication.Forms.FormsAuthentication;
@ -17,8 +24,21 @@ namespace RequestPlex.UI
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
base.ConfigureRequestContainer(container, context);
container.Register<IUserMapper, UserMapper>();
base.ConfigureRequestContainer(container, context);
container.Register<ISqliteConfiguration, DbConfiguration>(new DbConfiguration(new SqliteFactory()));
container.Register<ISettingsRepository, JsonRepository>();
container.Register<ICacheProvider, MemoryCacheProvider>();
container.Register<ISettingsService<RequestPlexSettings>, SettingsServiceV2<RequestPlexSettings>>();
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)

View file

@ -1,16 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: AdminModule.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.Dynamic;
using System.Linq;
using Nancy;
using Nancy.Extensions;
using Nancy.ModelBinding;
using Nancy.Security;
using Newtonsoft.Json;
using RequestPlex.Api;
using RequestPlex.Core;
using RequestPlex.Core.SettingModels;
using RequestPlex.Store;
using RequestPlex.UI.Models;
@ -18,7 +40,7 @@ namespace RequestPlex.UI.Modules
{
public class AdminModule : NancyModule
{
public AdminModule()
public AdminModule(ISettingsService<RequestPlexSettings> service)
{
#if !DEBUG
this.RequiresAuthentication();
@ -28,8 +50,8 @@ namespace RequestPlex.UI.Modules
dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue;
model.Port = null;
var s = new SettingsService();
var settings = s.GetSettings();
var settings = service.GetSettings();
if (settings != null)
{
model.Port = settings.Port;
@ -41,10 +63,9 @@ namespace RequestPlex.UI.Modules
Post["admin/"] = _ =>
{
var model = this.Bind<SettingsModel>();
var model = this.Bind<RequestPlexSettings>();
var s = new SettingsService();
s.SaveSettings(model);
service.SaveSettings(model);
return Context.GetRedirect("~/admin");
@ -61,20 +82,19 @@ namespace RequestPlex.UI.Modules
var plex = new PlexApi();
var model = plex.GetToken(user.username, user.password);
var s = new SettingsService();
var oldSettings = s.GetSettings();
var oldSettings = service.GetSettings();
if (oldSettings != null)
{
oldSettings.PlexAuthToken = model.user.authentication_token;
s.SaveSettings(oldSettings);
service.SaveSettings(oldSettings);
}
else
{
var newModel = new SettingsModel
var newModel = new RequestPlexSettings
{
PlexAuthToken = model.user.authentication_token
};
s.SaveSettings(newModel);
service.SaveSettings(newModel);
}

View file

@ -1,12 +1,28 @@
using Nancy;
using RequestPlex.Core;
using RequestPlex.Core.SettingModels;
namespace RequestPlex.UI.Modules
{
public class RequestsModule : NancyModule
{
public RequestsModule()
public RequestsModule(ISettingsService<RequestPlexSettings> s)
{
Get["requests/"] = _ => "Hello!";
Get["requests/test"] = _ =>
{
var se = new RequestPlexSettings
{
PlexAuthToken = "abc",
Port = 2344,
UserAuthentication = false
};
s.SaveSettings(se);
var a = s.GetSettings();
return "Hi!";
};
}
}
}

View file

@ -49,7 +49,6 @@ namespace RequestPlex.UI.Modules
private Response SearchMovie(string searchTerm)
{
var s = new SettingsService();
var api = new TheMovieDbApi();
var movies = api.SearchMovie(searchTerm);
var result = movies.Result;

View file

@ -2,9 +2,15 @@
using Microsoft.Owin.Hosting;
using Mono.Data.Sqlite;
using Nancy.Hosting.Self;
using RequestPlex.Core;
using RequestPlex.Core.SettingModels;
using RequestPlex.Helpers;
using RequestPlex.Store;
using RequestPlex.Store.Repository.NZBDash.DataAccessLayer.Repository;
namespace RequestPlex.UI
{
@ -16,7 +22,7 @@ namespace RequestPlex.UI
var s = new Setup();
s.SetupDb();
var service = new SettingsService();
var service = new SettingsServiceV2<RequestPlexSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var settings = service.GetSettings();
if (settings != null)

View file

@ -50,6 +50,10 @@
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Assemblies\Mono.Data.Sqlite.dll</HintPath>
</Reference>
<Reference Include="Nancy">
<HintPath>..\packages\Nancy.1.4.1\lib\net40\Nancy.dll</HintPath>
</Reference>
@ -71,10 +75,6 @@
<Reference Include="Nancy.ViewEngines.Razor">
<HintPath>..\packages\Nancy.Viewengines.Razor.1.4.1\lib\net40\Nancy.ViewEngines.Razor.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
<Private>True</Private>
@ -85,6 +85,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
@ -208,6 +209,10 @@
<Project>{DD7DC444-D3BF-4027-8AB9-EFC71F5EC581}</Project>
<Name>RequestPlex.Core</Name>
</ProjectReference>
<ProjectReference Include="..\RequestPlex.Helpers\RequestPlex.Helpers.csproj">
<Project>{1252336D-42A3-482A-804C-836E60173DFA}</Project>
<Name>RequestPlex.Helpers</Name>
</ProjectReference>
<ProjectReference Include="..\RequestPlex.Store\RequestPlex.Store.csproj">
<Project>{92433867-2B7B-477B-A566-96C382427525}</Project>
<Name>RequestPlex.Store</Name>

View file

@ -1,4 +1,6 @@
using Owin;
using System;
using Owin;
namespace RequestPlex.UI
{
@ -6,7 +8,16 @@ namespace RequestPlex.UI
{
public void Configuration(IAppBuilder app)
{
try
{
app.UseNancy();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
throw;
}
}
}
}

View file

@ -44,7 +44,7 @@
<input type="text" class="form-control" id="username" name="Username" placeholder="Username">
</div>
<div class="col-lg-4 col-lg-push-1">
<input type="password" class="form-control" id="password" name="Username" placeholder="Password">
<input type="password" class="form-control" id="password" name="Password" placeholder="Password">
</div>
</div>
<div class="form-group">
@ -60,8 +60,7 @@
</div>
</div>
<small>Current users that are allowed to authenticate: </small>
<select id="users" multiple="" class="form-control">
</select>
<select id="users" multiple="" class="form-control"></select>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
@ -69,10 +68,10 @@
</div>
</div>
<br/>
<br/>
<br/>
<br/>
<br />
<br />
<br />
<br />
<div>
<small class="col-lg-10 col-lg-offset-2">Please note, you will have to restart after changing these settings.</small>
</div>
@ -98,9 +97,11 @@
<script>
$(function () {
loadUserList();
if ($('#PlexAuthToken')) {
loadUserList();
}
$('#refreshUsers').click(function() {
$('#refreshUsers').click(function () {
loadUserList();
});

View file

@ -10,7 +10,6 @@
<package id="Nancy.Hosting.Self" version="1.4.1" requireReinstallation="true" />
<package id="Nancy.Owin" version="1.4.1" targetFramework="net452" />
<package id="Nancy.Viewengines.Razor" version="1.4.1" requireReinstallation="true" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" />
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" />