From 83ce84660a62423ea6bd5af61bde55b7f47c5016 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Mon, 29 Feb 2016 11:40:41 +0000 Subject: [PATCH] added helpers --- RequestPlex.Helpers/ICacheProvider.cs | 66 ++++++++ RequestPlex.Helpers/MemoryCacheProvider.cs | 105 +++++++++++++ RequestPlex.Helpers/ObjectCopier.cs | 57 +++++++ .../Properties/AssemblyInfo.cs | 36 +++++ .../RequestPlex.Helpers.csproj | 65 ++++++++ RequestPlex.Helpers/StringCipher.cs | 145 ++++++++++++++++++ RequestPlex.Helpers/packages.config | 4 + RequestPlex.Store/ISettingsRepository.cs | 70 +++++++++ RequestPlex.Store/Models/GlobalSettings.cs | 39 +++++ .../Repository/JsonRepository.cs | 119 ++++++++++++++ RequestPlex.Store/RequestPlex.Store.csproj | 9 ++ RequestPlex.sln | 6 + 12 files changed, 721 insertions(+) create mode 100644 RequestPlex.Helpers/ICacheProvider.cs create mode 100644 RequestPlex.Helpers/MemoryCacheProvider.cs create mode 100644 RequestPlex.Helpers/ObjectCopier.cs create mode 100644 RequestPlex.Helpers/Properties/AssemblyInfo.cs create mode 100644 RequestPlex.Helpers/RequestPlex.Helpers.csproj create mode 100644 RequestPlex.Helpers/StringCipher.cs create mode 100644 RequestPlex.Helpers/packages.config create mode 100644 RequestPlex.Store/ISettingsRepository.cs create mode 100644 RequestPlex.Store/Models/GlobalSettings.cs create mode 100644 RequestPlex.Store/Repository/JsonRepository.cs diff --git a/RequestPlex.Helpers/ICacheProvider.cs b/RequestPlex.Helpers/ICacheProvider.cs new file mode 100644 index 000000000..0706502c5 --- /dev/null +++ b/RequestPlex.Helpers/ICacheProvider.cs @@ -0,0 +1,66 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ICacheProvider.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; + +namespace RequestPlex.Helpers +{ + public interface ICacheProvider + { + /// + /// Gets the item from the cache, if the item is not present + /// then we will get that item and store it in the cache. + /// + /// Type to store in the cache + /// The key + /// The item callback. This will be called if the item is not present in the cache. + /// The amount of time we want to cache the object + /// + T GetOrSet(string key, Func itemCallback, int cacheTime = 20) where T : class; + + /// + /// Gets the specified item from the cache. + /// + /// Type to get from the cache + /// The key. + /// + T Get(string key) where T : class; + + /// + /// Set/Store the specified object in the cache + /// + /// The key. + /// The object we want to store. + /// The amount of time we want to cache the object. + void Set(string key, object data, int cacheTime); + + /// + /// Removes the specified object from the cache. + /// + /// The key. + void Remove(string key); + } +} \ No newline at end of file diff --git a/RequestPlex.Helpers/MemoryCacheProvider.cs b/RequestPlex.Helpers/MemoryCacheProvider.cs new file mode 100644 index 000000000..cf125dec8 --- /dev/null +++ b/RequestPlex.Helpers/MemoryCacheProvider.cs @@ -0,0 +1,105 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MemoryCacheProvider.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 System.Runtime.Caching; + +namespace RequestPlex.Helpers +{ + public class MemoryCacheProvider : ICacheProvider + { + private ObjectCache Cache => MemoryCache.Default; + + /// + /// Gets the item from the cache, if the item is not present + /// then we will get that item and store it in the cache. + /// + /// Type to store in the cache. + /// The key. + /// The item callback. This will be called if the item is not present in the cache. + /// + /// The amount of time we want to cache the object. + /// A copy of the cached object. + /// If the ]]> itemCallback is null and the item is not in the cache it will throw a . + /// If you do not want to change the object in the cache (since it's a copy returned and not a reference) you will need to + /// the cached item and then it, or just call this method. + public T GetOrSet(string key, Func itemCallback, int cacheTime = 20) where T : class + { + var item = Get(key); + if (item == null) + { + item = itemCallback(); + if (item != null) + { + Set(key, item, cacheTime); + } + } + + // Return a copy, not the stored cache reference + // The cached object will not change + // If we + return item.CloneJson(); + } + + /// + /// Gets the specified item from the cache. + /// + /// Type to get from the cache + /// The key. + /// + public T Get(string key) where T : class + { + var item = Cache.Get(key) as T; + return item; + } + + /// + /// Set/Store the specified object in the cache + /// + /// The key. + /// The object we want to store. + /// The amount of time we want to cache the object. + public void Set(string key, object data, int cacheTime) + { + var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) }; + Cache.Add(new CacheItem(key, data), policy); + } + + /// + /// Removes the specified object from the cache. + /// + /// The key. + public void Remove(string key) + { + var keys = Cache.Where(x => x.Key.Contains(key)); + foreach (var k in keys) + { + Cache.Remove(k.Key); + } + } + } +} \ No newline at end of file diff --git a/RequestPlex.Helpers/ObjectCopier.cs b/RequestPlex.Helpers/ObjectCopier.cs new file mode 100644 index 000000000..dca1fa3aa --- /dev/null +++ b/RequestPlex.Helpers/ObjectCopier.cs @@ -0,0 +1,57 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ObjectCopier.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; + +namespace RequestPlex.Helpers +{ + /// + /// Provides a method for performing a deep copy of an object. + /// Binary Serialization is used to perform the copy. + /// + public static class ObjectCopier + { + /// + /// Initialize inner objects individually + /// For example in default constructor some list property initialized with some values, + /// but in 'source' these items are cleaned - + /// without ObjectCreationHandling.Replace default constructor values will be added to result + /// + private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace }; + + /// + /// Perform a deep Copy of the object, using Json as a serialisation method. + /// + /// The type of object being copied. + /// The object instance to copy. + /// The copied object. + public static T CloneJson(this T source) + { + // Don't serialize a null object, simply return the default for that object + return ReferenceEquals(source, null) ? default(T) : JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source), Settings); + } + } +} \ No newline at end of file diff --git a/RequestPlex.Helpers/Properties/AssemblyInfo.cs b/RequestPlex.Helpers/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..e8c938896 --- /dev/null +++ b/RequestPlex.Helpers/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RequestPlex.Helpers")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RequestPlex.Helpers")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1252336d-42a3-482a-804c-836e60173dfa")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RequestPlex.Helpers/RequestPlex.Helpers.csproj b/RequestPlex.Helpers/RequestPlex.Helpers.csproj new file mode 100644 index 000000000..113b5c9f9 --- /dev/null +++ b/RequestPlex.Helpers/RequestPlex.Helpers.csproj @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {1252336D-42A3-482A-804C-836E60173DFA} + Library + Properties + RequestPlex.Helpers + RequestPlex.Helpers + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RequestPlex.Helpers/StringCipher.cs b/RequestPlex.Helpers/StringCipher.cs new file mode 100644 index 000000000..47847fffe --- /dev/null +++ b/RequestPlex.Helpers/StringCipher.cs @@ -0,0 +1,145 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: StringCipher.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.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace RequestPlex.Helpers +{ + public class StringCipher + { + // This constant determines the number of iterations for the password bytes generation function. + private const int DerivationIterations = 1000; + // This constant is used to determine the keysize of the encryption algorithm in bits. + // We divide this by 8 within the code below to get the equivalent number of bytes. + private const int Keysize = 256; + + /// + /// Decrypts the specified cipher text. + /// + /// The cipher text. + /// The pass phrase. + /// + public static string Decrypt(string cipherText, string passPhrase) + { + // Get the complete stream of bytes that represent: + // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText] + var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText); + // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes. + var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray(); + // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes. + var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray(); + // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string. + var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray(); + + using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) + { + var keyBytes = password.GetBytes(Keysize / 8); + using (var symmetricKey = new RijndaelManaged()) + { + symmetricKey.BlockSize = 256; + symmetricKey.Mode = CipherMode.CBC; + symmetricKey.Padding = PaddingMode.PKCS7; + using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)) + { + using (var memoryStream = new MemoryStream(cipherTextBytes)) + { + using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) + { + var plainTextBytes = new byte[cipherTextBytes.Length]; + var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); + memoryStream.Close(); + cryptoStream.Close(); + return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); + } + } + } + } + } + } + + /// + /// Encrypts the specified plain text. + /// + /// The plain text. + /// The pass phrase. + /// + public static string Encrypt(string plainText, string passPhrase) + { + // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text + // so that the same Salt and IV values can be used when decrypting. + var saltStringBytes = Generate256BitsOfRandomEntropy(); + var ivStringBytes = Generate256BitsOfRandomEntropy(); + var plainTextBytes = Encoding.UTF8.GetBytes(plainText); + using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) + { + var keyBytes = password.GetBytes(Keysize / 8); + using (var symmetricKey = new RijndaelManaged()) + { + symmetricKey.BlockSize = 256; + symmetricKey.Mode = CipherMode.CBC; + symmetricKey.Padding = PaddingMode.PKCS7; + using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)) + { + using (var memoryStream = new MemoryStream()) + { + using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) + { + cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); + cryptoStream.FlushFinalBlock(); + // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes. + var cipherTextBytes = saltStringBytes; + cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray(); + cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray(); + memoryStream.Close(); + cryptoStream.Close(); + return Convert.ToBase64String(cipherTextBytes); + } + } + } + } + } + } + + /// + /// Generate256s the bits of random entropy. + /// + /// + private static byte[] Generate256BitsOfRandomEntropy() + { + var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits. + using (var rngCsp = new RNGCryptoServiceProvider()) + { + // Fill the array with cryptographically secure random bytes. + rngCsp.GetBytes(randomBytes); + } + return randomBytes; + } + } +} \ No newline at end of file diff --git a/RequestPlex.Helpers/packages.config b/RequestPlex.Helpers/packages.config new file mode 100644 index 000000000..47cebb403 --- /dev/null +++ b/RequestPlex.Helpers/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RequestPlex.Store/ISettingsRepository.cs b/RequestPlex.Store/ISettingsRepository.cs new file mode 100644 index 000000000..e85bb49f9 --- /dev/null +++ b/RequestPlex.Store/ISettingsRepository.cs @@ -0,0 +1,70 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ISettingsRepository.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.Collections.Generic; + +using RequestPlex.Store.Models; + +namespace RequestPlex.Store +{ + public interface ISettingsRepository + { + /// + /// Inserts the specified entity. + /// + /// The entity. + long Insert(GlobalSettings entity); + + /// + /// Gets all. + /// + /// + IEnumerable GetAll(); + + /// + /// Gets the specified identifier. + /// + /// Name of the settings. + /// + GlobalSettings Get(string settingsName); + + /// + /// Deletes the specified entity. + /// + /// The entity. + /// + bool Delete(GlobalSettings entity); + + /// + /// Updates the specified entity. + /// + /// The entity. + /// + bool Update(GlobalSettings entity); + + + } +} \ No newline at end of file diff --git a/RequestPlex.Store/Models/GlobalSettings.cs b/RequestPlex.Store/Models/GlobalSettings.cs new file mode 100644 index 000000000..26fce6a19 --- /dev/null +++ b/RequestPlex.Store/Models/GlobalSettings.cs @@ -0,0 +1,39 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: GlobalSettings.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 Dapper.Contrib.Extensions; + +namespace RequestPlex.Store.Models +{ + + [Table("GlobalSettings")] + public class GlobalSettings : Entity + { + public string Content { get; set; } + public string SettingsName { get; set; } + } + +} \ No newline at end of file diff --git a/RequestPlex.Store/Repository/JsonRepository.cs b/RequestPlex.Store/Repository/JsonRepository.cs new file mode 100644 index 000000000..4123aadc0 --- /dev/null +++ b/RequestPlex.Store/Repository/JsonRepository.cs @@ -0,0 +1,119 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: JsonRepository.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.Collections.Generic; +using System.Linq; + +using Dapper.Contrib.Extensions; + +using RequestPlex.Helpers; +using RequestPlex.Store.Models; + +namespace RequestPlex.Store.Repository +{ + + + namespace NZBDash.DataAccessLayer.Repository + { + public class JsonRepository : ISettingsRepository + { + private ICacheProvider Cache { get; set; } + + private string TypeName { get; set; } + public JsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider) + { + Db = config; + Cache = cacheProvider; + TypeName = typeof(JsonRepository).Name; + } + + private ISqliteConfiguration Db { get; set; } + + public long Insert(GlobalSettings entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return con.Insert(entity); + } + } + + public IEnumerable GetAll() + { + var key = TypeName + "GetAll"; + var item = Cache.GetOrSet(key, () => + { + using (var con = Db.DbConnection()) + { + var page = con.GetAll(); + return page; + } + }, 5); + return item; + } + + public GlobalSettings Get(string pageName) + { + var key = pageName + "Get"; + var item = Cache.GetOrSet(key, () => + { + using (var con = Db.DbConnection()) + { + var page = con.GetAll().SingleOrDefault(x => x.SettingsName == pageName); + return page; + } + }, 5); + return item; + } + + public bool Delete(GlobalSettings entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return con.Delete(entity); + } + } + + public bool Update(GlobalSettings entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return con.Update(entity); + } + } + + private void ResetCache() + { + Cache.Remove("Get"); + Cache.Remove(TypeName + "GetAll"); + } + } + } +} \ No newline at end of file diff --git a/RequestPlex.Store/RequestPlex.Store.csproj b/RequestPlex.Store/RequestPlex.Store.csproj index a51323bed..e7a782019 100644 --- a/RequestPlex.Store/RequestPlex.Store.csproj +++ b/RequestPlex.Store/RequestPlex.Store.csproj @@ -53,9 +53,12 @@ + + + @@ -83,6 +86,12 @@ + + + {1252336D-42A3-482A-804C-836E60173DFA} + RequestPlex.Helpers + +