Using the IoC container now

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

View file

@ -0,0 +1,35 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ISettingsService.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
namespace RequestPlex.Core
{
public interface ISettingsService<T>
{
T GetSettings();
bool SaveSettings(T model);
bool Delete(T model);
}
}

View file

@ -41,6 +41,14 @@
<HintPath>..\packages\Nancy.Authentication.Forms.1.4.1\lib\net40\Nancy.Authentication.Forms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Omu.ValueInjecter, Version=3.1.1.0, Culture=neutral, PublicKeyToken=c7694541b0ac80e4, processorArchitecture=MSIL">
<HintPath>..\packages\valueinjecter.3.1.1.2\lib\net40\Omu.ValueInjecter.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -51,7 +59,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ISettingsService.cs" />
<Compile Include="RequestService.cs" />
<Compile Include="SettingModels\RequestPlexSettings.cs" />
<Compile Include="SettingModels\Settings.cs" />
<Compile Include="SettingsService.cs" />
<Compile Include="SettingsServiceV2.cs" />
<Compile Include="Setup.cs" />
<Compile Include="UserIdentity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -61,6 +74,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<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

@ -0,0 +1,65 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: RequestService.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 RequestPlex.Store;
namespace RequestPlex.Core
{
public class RequestService
{
public RequestService(ISqliteConfiguration db, IRepository<RequestedModel> repo)
{
Db = db;
Repo = repo;
}
private ISqliteConfiguration Db { get; set; }
private IRepository<RequestedModel> Repo { get; set; }
public void AddRequest(int tmdbid, RequestType type)
{
var model = new RequestedModel
{
Tmdbid = tmdbid,
Type = type
};
Repo.Insert(model);
}
public bool CheckRequest(int tmdbid)
{
return Repo.GetAll().Any(x => x.Tmdbid == tmdbid);
}
public void DeleteRequest(int tmdbId)
{
var entity = Repo.GetAll().FirstOrDefault(x => x.Tmdbid == tmdbId);
Repo.Delete(entity);
}
}
}

View file

@ -0,0 +1,33 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: Settings.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
namespace RequestPlex.Core.SettingModels
{
public class Settings
{
public int Id { get; set; }
}
}

View file

@ -1,8 +1,30 @@
using System;
using System.Collections.Generic;
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SettingsService.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 System.Text;
using System.Threading.Tasks;
using Mono.Data.Sqlite;
@ -12,21 +34,6 @@ namespace RequestPlex.Core
{
public class SettingsService
{
public void SaveSettings(SettingsModel model)
{
var db = new DbConfiguration(new SqliteFactory());
var repo = new GenericRepository<SettingsModel>(db);
var existingSettings = repo.GetAll().FirstOrDefault();
if (existingSettings != null)
{
existingSettings = model;
repo.Update(existingSettings);
return;
}
repo.Insert(model);
}
public SettingsModel GetSettings()
{

View file

@ -0,0 +1,114 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SettingsServiceV2.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 Newtonsoft.Json;
using Omu.ValueInjecter;
using RequestPlex.Core.SettingModels;
using RequestPlex.Helpers;
using RequestPlex.Store;
using RequestPlex.Store.Models;
namespace RequestPlex.Core
{
public class SettingsServiceV2<T> : ISettingsService<T>
where T : Settings, new()
{
public SettingsServiceV2(ISettingsRepository repo)
{
Repo = repo;
EntityName = typeof(T).Name;
}
private ISettingsRepository Repo { get; set; }
private string EntityName { get; set; }
public T GetSettings()
{
var result = Repo.Get(EntityName);
if (result == null)
{
return new T();
}
result.Content = DecryptSettings(result);
var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings);
var model = obj;
return model;
}
public bool SaveSettings(T model)
{
var entity = Repo.Get(EntityName);
if (entity == null)
{
var newEntity = model;
var settings = new GlobalSettings { SettingsName = EntityName, Content = JsonConvert.SerializeObject(newEntity, SerializerSettings.Settings) };
settings.Content = EncryptSettings(settings);
var insertResult = Repo.Insert(settings);
return insertResult != long.MinValue;
}
var modified = model;
modified.Id = entity.Id;
var globalSettings = new GlobalSettings { SettingsName = EntityName, Content = JsonConvert.SerializeObject(modified, SerializerSettings.Settings), Id = entity.Id };
globalSettings.Content = EncryptSettings(globalSettings);
var result = Repo.Update(globalSettings);
return result;
}
public bool Delete(T model)
{
var entity = Repo.Get(EntityName);
if (entity != null)
{
return Repo.Delete(entity);
}
// Entity does not exist so nothing to delete
return true;
}
private string EncryptSettings(GlobalSettings settings)
{
return StringCipher.Encrypt(settings.Content, settings.SettingsName);
}
private string DecryptSettings(GlobalSettings settings)
{
return StringCipher.Decrypt(settings.Content, settings.SettingsName);
}
}
}

View file

@ -1,4 +1,30 @@
using System;
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UserMapper.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;
using System.Linq;
using Mono.Data.Sqlite;
@ -13,11 +39,14 @@ namespace RequestPlex.Core
{
public class UserMapper : IUserMapper
{
public UserMapper(ISqliteConfiguration db)
{
Db = db;
}
private static ISqliteConfiguration Db { get; set; }
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
{
var db = new DbConfiguration(new SqliteFactory());
var repo = new UserRepository<UserModel>(db);
var repo = new UserRepository<UserModel>(Db);
var user = repo.Get(identifier.ToString());
@ -35,7 +64,7 @@ namespace RequestPlex.Core
public static Guid? ValidateUser(string username, string password)
{
var db = new DbConfiguration(new SqliteFactory());
var repo = new UserRepository<UserModel>(db);
var repo = new UserRepository<UserModel>(Db);
var users = repo.GetAll();
var userRecord = users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase) && u.Password.Equals(password)); // TODO hashing
@ -50,7 +79,7 @@ namespace RequestPlex.Core
public static bool DoUsersExist()
{
var db = new DbConfiguration(new SqliteFactory());
var repo = new UserRepository<UserModel>(db);
var repo = new UserRepository<UserModel>(Db);
var users = repo.GetAll();
return users.Any();
}
@ -58,7 +87,7 @@ namespace RequestPlex.Core
public static Guid? CreateUser(string username, string password)
{
var db = new DbConfiguration(new SqliteFactory());
var repo = new UserRepository<UserModel>(db);
var repo = new UserRepository<UserModel>(Db);
var userModel = new UserModel { UserName = username, User = Guid.NewGuid().ToString(), Password = password };
repo.Insert(userModel);

View file

@ -2,4 +2,6 @@
<packages>
<package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Nancy.Authentication.Forms" version="1.4.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="valueinjecter" version="3.1.1.2" targetFramework="net452" />
</packages>