diff --git a/RequestPlex.Api/MovieBase.cs b/RequestPlex.Api/MovieBase.cs index 27c518964..6a4b719dd 100644 --- a/RequestPlex.Api/MovieBase.cs +++ b/RequestPlex.Api/MovieBase.cs @@ -1,14 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MovieBase.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 RequestPlex.Helpers; namespace RequestPlex.Api { public abstract class MovieBase { - protected string ApiKey = "b8eabaf5608b88d0298aa189dd90bf00"; + private static readonly string Encrypted = "0T3QNSseexLO7n7UPiJvl70Y+KKnvbeTlsusl7Kwq0hPH0BHOuFNGwksNCjkwqWedyDdI/MJeUR4wtL4bIl0Z+//uHXEaYM/4H2pjeLbH5EWdUe5TTj1AhaIR5PQweamvcienRyFD/3YPCC/+qL5mHkKXBkPumMod3Zb/4yN0Ik="; + protected string ApiKey = StringCipher.Decrypt(Encrypted, "ApiKey"); protected string Url = "http://api.themoviedb.org/3"; } } diff --git a/RequestPlex.Api/PlexApi.cs b/RequestPlex.Api/PlexApi.cs index efa7c2727..20f4b5421 100644 --- a/RequestPlex.Api/PlexApi.cs +++ b/RequestPlex.Api/PlexApi.cs @@ -25,7 +25,7 @@ namespace RequestPlex.Api Method = Method.POST, }; - request.AddHeader("X-Plex-Client-Identifier", "Test213"); + request.AddHeader("X-Plex-Client-Identifier", "Test213"); // TODO need something unique to the users version/installation request.AddHeader("X-Plex-Product", "Request Plex"); request.AddHeader("X-Plex-Version", "0.0.1"); request.AddHeader("Content-Type", "application/json"); diff --git a/RequestPlex.Api/RequestPlex.Api.csproj b/RequestPlex.Api/RequestPlex.Api.csproj index 4afe2f00a..1b81b40ee 100644 --- a/RequestPlex.Api/RequestPlex.Api.csproj +++ b/RequestPlex.Api/RequestPlex.Api.csproj @@ -77,6 +77,10 @@ {95834072-A675-415D-AA8F-877C91623810} RequestPlex.Api.Interfaces + + {1252336D-42A3-482A-804C-836E60173DFA} + RequestPlex.Helpers + diff --git a/RequestPlex.Api/TheMovieDbApi.cs b/RequestPlex.Api/TheMovieDbApi.cs index 9fd1e544c..6ee80ab27 100644 --- a/RequestPlex.Api/TheMovieDbApi.cs +++ b/RequestPlex.Api/TheMovieDbApi.cs @@ -5,6 +5,7 @@ using TMDbLib.Client; using TMDbLib.Objects.General; using TMDbLib.Objects.Movies; using TMDbLib.Objects.Search; +using TMDbLib.Objects.TvShows; namespace RequestPlex.Api { @@ -39,5 +40,17 @@ namespace RequestPlex.Api var movies = await Client.GetMovieList(MovieListType.Upcoming); return movies.Results; } + + public async Task GetMovieInformation(int tmdbId) + { + var movies = await Client.GetMovie(tmdbId); + return movies; + } + + public async Task GetTvShowInformation(int tmdbId) + { + var show = await Client.GetTvShow(tmdbId); + return show; + } } } diff --git a/RequestPlex.Core/ISettingsService.cs b/RequestPlex.Core/ISettingsService.cs new file mode 100644 index 000000000..78e4a07ef --- /dev/null +++ b/RequestPlex.Core/ISettingsService.cs @@ -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 GetSettings(); + bool SaveSettings(T model); + bool Delete(T model); + } +} \ No newline at end of file diff --git a/RequestPlex.Core/RequestPlex.Core.csproj b/RequestPlex.Core/RequestPlex.Core.csproj index 67157151c..62122a746 100644 --- a/RequestPlex.Core/RequestPlex.Core.csproj +++ b/RequestPlex.Core/RequestPlex.Core.csproj @@ -41,6 +41,14 @@ ..\packages\Nancy.Authentication.Forms.1.4.1\lib\net40\Nancy.Authentication.Forms.dll True + + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + True + + + ..\packages\valueinjecter.3.1.1.2\lib\net40\Omu.ValueInjecter.dll + True + @@ -49,9 +57,17 @@ + + ..\packages\TMDbLib.0.9.0.0-alpha\lib\net45\TMDbLib.dll + + + + + + @@ -61,6 +77,14 @@ + + {8CB8D235-2674-442D-9C6A-35FCAEEB160D} + RequestPlex.Api + + + {1252336D-42A3-482A-804C-836E60173DFA} + RequestPlex.Helpers + {92433867-2B7B-477B-A566-96C382427525} RequestPlex.Store diff --git a/RequestPlex.Core/RequestService.cs b/RequestPlex.Core/RequestService.cs new file mode 100644 index 000000000..51845784d --- /dev/null +++ b/RequestPlex.Core/RequestService.cs @@ -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 repo) + { + Db = db; + Repo = repo; + } + private ISqliteConfiguration Db { get; set; } + private IRepository 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); + } + + } +} \ No newline at end of file diff --git a/RequestPlex.Core/SettingModels/RequestPlexSettings.cs b/RequestPlex.Core/SettingModels/RequestPlexSettings.cs new file mode 100644 index 000000000..c28c933f0 --- /dev/null +++ b/RequestPlex.Core/SettingModels/RequestPlexSettings.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RequestPlexSettings.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 RequestPlexSettings : Settings + { + public int Port { get; set; } + public bool UserAuthentication { get; set; } + public string PlexAuthToken { get; set; } + } +} diff --git a/RequestPlex.Core/SettingModels/Settings.cs b/RequestPlex.Core/SettingModels/Settings.cs new file mode 100644 index 000000000..af3194b42 --- /dev/null +++ b/RequestPlex.Core/SettingModels/Settings.cs @@ -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; } + } +} \ No newline at end of file diff --git a/RequestPlex.Core/SettingsService.cs b/RequestPlex.Core/SettingsService.cs index c9d86a44e..c0d5c1fe7 100644 --- a/RequestPlex.Core/SettingsService.cs +++ b/RequestPlex.Core/SettingsService.cs @@ -1,32 +1,41 @@ -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; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Mono.Data.Sqlite; +using RequestPlex.Api; using RequestPlex.Store; namespace RequestPlex.Core { public class SettingsService { - public void SaveSettings(SettingsModel model) - { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new GenericRepository(db); - - var existingSettings = repo.GetAll().FirstOrDefault(); - if (existingSettings != null) - { - existingSettings = model; - repo.Update(existingSettings); - return; - } - - repo.Insert(model); - } public SettingsModel GetSettings() { @@ -40,12 +49,39 @@ namespace RequestPlex.Core public void AddRequest(int tmdbid, RequestType type) { - var model = new RequestedModel + var api = new TheMovieDbApi(); + var model = new RequestedModel(); + if (type == RequestType.Movie) { - Tmdbid = tmdbid, - Type = type - }; + var movieInfo = api.GetMovieInformation(tmdbid).Result; + model = new RequestedModel + { + Tmdbid = movieInfo.Id, + Type = type, + Overview = movieInfo.Overview, + ImdbId = movieInfo.ImdbId, + PosterPath = "http://image.tmdb.org/t/p/w150/" + movieInfo.PosterPath, + Title = movieInfo.Title, + ReleaseDate = movieInfo.ReleaseDate ?? DateTime.MinValue, + Status = movieInfo.Status + }; + } + else + { + var showInfo = api.GetTvShowInformation(tmdbid).Result; + + model = new RequestedModel + { + Tmdbid = showInfo.Id, + Type = type, + Overview = showInfo.Overview, + PosterPath = "http://image.tmdb.org/t/p/w150/" + showInfo.PosterPath, + Title = showInfo.Name, + ReleaseDate = showInfo.FirstAirDate ?? DateTime.MinValue, + Status = showInfo.Status + }; + } var db = new DbConfiguration(new SqliteFactory()); var repo = new GenericRepository(db); diff --git a/RequestPlex.Core/SettingsServiceV2.cs b/RequestPlex.Core/SettingsServiceV2.cs new file mode 100644 index 000000000..e0245e8a8 --- /dev/null +++ b/RequestPlex.Core/SettingsServiceV2.cs @@ -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 : ISettingsService + 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(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); + } + } +} diff --git a/RequestPlex.Core/UserMapper.cs b/RequestPlex.Core/UserMapper.cs index 89f724143..ae70167a1 100644 --- a/RequestPlex.Core/UserMapper.cs +++ b/RequestPlex.Core/UserMapper.cs @@ -1,8 +1,32 @@ -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; - using Nancy; using Nancy.Authentication.Forms; using Nancy.Security; @@ -13,11 +37,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(db); + var repo = new UserRepository(Db); var user = repo.Get(identifier.ToString()); @@ -34,8 +61,7 @@ namespace RequestPlex.Core public static Guid? ValidateUser(string username, string password) { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new UserRepository(db); + var repo = new UserRepository(Db); var users = repo.GetAll(); var userRecord = users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase) && u.Password.Equals(password)); // TODO hashing @@ -49,16 +75,14 @@ namespace RequestPlex.Core public static bool DoUsersExist() { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new UserRepository(db); + var repo = new UserRepository(Db); var users = repo.GetAll(); return users.Any(); } public static Guid? CreateUser(string username, string password) { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new UserRepository(db); + var repo = new UserRepository(Db); var userModel = new UserModel { UserName = username, User = Guid.NewGuid().ToString(), Password = password }; repo.Insert(userModel); diff --git a/RequestPlex.Core/packages.config b/RequestPlex.Core/packages.config index 22cd929ec..2d5fcc6ef 100644 --- a/RequestPlex.Core/packages.config +++ b/RequestPlex.Core/packages.config @@ -2,4 +2,6 @@ + + \ No newline at end of file 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..4fd02c8e9 --- /dev/null +++ b/RequestPlex.Helpers/RequestPlex.Helpers.csproj @@ -0,0 +1,66 @@ + + + + + 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/SerializerSettings.cs b/RequestPlex.Helpers/SerializerSettings.cs new file mode 100644 index 000000000..f89668047 --- /dev/null +++ b/RequestPlex.Helpers/SerializerSettings.cs @@ -0,0 +1,45 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SerializerSettings.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.Runtime.Serialization.Formatters; + +using Newtonsoft.Json; + +namespace RequestPlex.Helpers +{ + public static class SerializerSettings + { + + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings + { + Formatting = Formatting.None, + TypeNameHandling = TypeNameHandling.Objects, + TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple, + NullValueHandling = NullValueHandling.Ignore + }; + + } +} \ 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/Entity.cs b/RequestPlex.Store/Entity.cs index cca38fcfd..9f849518c 100644 --- a/RequestPlex.Store/Entity.cs +++ b/RequestPlex.Store/Entity.cs @@ -1,9 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: Entity.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 diff --git a/RequestPlex.Store/GenericRepository.cs b/RequestPlex.Store/GenericRepository.cs index 0707fe6ce..526c2bc96 100644 --- a/RequestPlex.Store/GenericRepository.cs +++ b/RequestPlex.Store/GenericRepository.cs @@ -1,8 +1,31 @@ -using System; +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: GenericRepository.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.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Dapper.Contrib.Extensions; diff --git a/RequestPlex.Store/IRepository.cs b/RequestPlex.Store/IRepository.cs index 22158e975..09ff3715d 100644 --- a/RequestPlex.Store/IRepository.cs +++ b/RequestPlex.Store/IRepository.cs @@ -1,8 +1,30 @@ -using System; +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IRepository.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 System.Text; -using System.Threading.Tasks; namespace RequestPlex.Store { 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 + + + + + +
+ + +
+
+
+ +
+
+
+ + +
+
+
+ +
+
+
+
+ + + + + + + + diff --git a/RequestPlex.UI/Views/Search/Index.cshtml b/RequestPlex.UI/Views/Search/Index.cshtml index 8dc337963..a054dde29 100644 --- a/RequestPlex.UI/Views/Search/Index.cshtml +++ b/RequestPlex.UI/Views/Search/Index.cshtml @@ -51,20 +51,37 @@ poster {{/if}} -
+
-

{{overview}}.

+

{{overview}}

-
- Vote Average: {{voteAverage}} - Vote Count: {{voteCount}} +
- - + + {{#if_eq type "movie"}} + + {{/if_eq}} + {{#if_eq type "tv"}} + + {{/if_eq}} +
+
+
+ Vote Average: {{voteAverage}} + Vote Count: {{voteCount}}
diff --git a/RequestPlex.UI/app.config b/RequestPlex.UI/app.config index d33a25db3..825a85f28 100644 --- a/RequestPlex.UI/app.config +++ b/RequestPlex.UI/app.config @@ -1,19 +1,31 @@ - - - - -
- - - - - - - - - - - - - + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RequestPlex.UI/packages.config b/RequestPlex.UI/packages.config index b9d30a043..7b054d963 100644 --- a/RequestPlex.UI/packages.config +++ b/RequestPlex.UI/packages.config @@ -10,7 +10,6 @@ - diff --git a/RequestPlex.sln b/RequestPlex.sln index ee695ce68..7ae8c0377 100644 --- a/RequestPlex.sln +++ b/RequestPlex.sln @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RequestPlex.Helpers", "RequestPlex.Helpers\RequestPlex.Helpers.csproj", "{1252336D-42A3-482A-804C-836E60173DFA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {92433867-2B7B-477B-A566-96C382427525}.Debug|Any CPU.Build.0 = Debug|Any CPU {92433867-2B7B-477B-A566-96C382427525}.Release|Any CPU.ActiveCfg = Release|Any CPU {92433867-2B7B-477B-A566-96C382427525}.Release|Any CPU.Build.0 = Release|Any CPU + {1252336D-42A3-482A-804C-836E60173DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1252336D-42A3-482A-804C-836E60173DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1252336D-42A3-482A-804C-836E60173DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1252336D-42A3-482A-804C-836E60173DFA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RequestPlex.sln.DotSettings b/RequestPlex.sln.DotSettings new file mode 100644 index 000000000..6c902f71f --- /dev/null +++ b/RequestPlex.sln.DotSettings @@ -0,0 +1,27 @@ + + /************************************************************************ + Copyright (c) $CURRENT_YEAR$ Jamie Rees + File: $FILENAME$ + Created By: $USER_NAME$ + + 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. + ************************************************************************/ + + Copyright \ No newline at end of file