From 07b42ffd50b241ebbbcce423cf526460523a4861 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Thu, 17 Mar 2016 17:17:34 +0000 Subject: [PATCH] Started on #16, nothing is hooked up yet. --- PlexRequests.Core/IRequestService.cs | 2 +- PlexRequests.Core/JsonRequestService.cs | 100 ++++++++++++++ PlexRequests.Core/PlexRequests.Core.csproj | 1 + PlexRequests.Core/RequestService.cs | 4 +- PlexRequests.Core/Setup.cs | 2 +- .../AvailabilityUpdateService.cs | 2 +- PlexRequests.Store/IRequestRepository.cs | 65 +++++++++ PlexRequests.Store/Models/RequestBlobs.cs | 38 ++++++ PlexRequests.Store/PlexRequests.Store.csproj | 5 +- .../Repository/RequestJsonRepository.cs | 127 ++++++++++++++++++ ...epository.cs => SettingsJsonRepository.cs} | 8 +- PlexRequests.Store/SqlTables.sql | 9 ++ PlexRequests.UI/Bootstrapper.cs | 2 +- PlexRequests.UI/Program.cs | 2 +- 14 files changed, 355 insertions(+), 12 deletions(-) create mode 100644 PlexRequests.Core/JsonRequestService.cs create mode 100644 PlexRequests.Store/IRequestRepository.cs create mode 100644 PlexRequests.Store/Models/RequestBlobs.cs create mode 100644 PlexRequests.Store/Repository/RequestJsonRepository.cs rename PlexRequests.Store/Repository/{JsonRepository.cs => SettingsJsonRepository.cs} (90%) diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs index ed000aa59..18c8485e9 100644 --- a/PlexRequests.Core/IRequestService.cs +++ b/PlexRequests.Core/IRequestService.cs @@ -35,7 +35,7 @@ namespace PlexRequests.Core long AddRequest(int providerId, RequestedModel model); bool CheckRequest(int providerId); void DeleteRequest(int tmdbId); - void UpdateRequest(int originalId, RequestedModel model); + void UpdateRequest(RequestedModel model); RequestedModel Get(int id); IEnumerable GetAll(); bool BatchUpdate(List model); diff --git a/PlexRequests.Core/JsonRequestService.cs b/PlexRequests.Core/JsonRequestService.cs new file mode 100644 index 000000000..f7b5097a4 --- /dev/null +++ b/PlexRequests.Core/JsonRequestService.cs @@ -0,0 +1,100 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: JsonRequestService.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 Newtonsoft.Json; + +using PlexRequests.Store; +using PlexRequests.Store.Models; + +namespace PlexRequests.Core +{ + public class JsonRequestService : IRequestService + { + public JsonRequestService(IRequestRepository repo) + { + Repo = repo; + } + private IRequestRepository Repo { get; } + public long AddRequest(int providerId, RequestedModel model) + { + var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId}; + + return Repo.Insert(entity); + } + + public bool CheckRequest(int providerId) + { + var blobs = Repo.GetAll(); + return blobs.Any(x => x.ProviderId == providerId); + } + + public void DeleteRequest(int tmdbId) + { + var blob = Repo.GetAll().FirstOrDefault(x => x.ProviderId == tmdbId); + Repo.Delete(blob); + } + + public void UpdateRequest(RequestedModel model) + { + var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id}; + Repo.Update(entity); + } + + public RequestedModel Get(int id) + { + var blob = Repo.Get(id); + var json = Encoding.UTF8.GetString(blob.Content); + var model = JsonConvert.DeserializeObject(json); + return model; + } + + public IEnumerable GetAll() + { + var blobs = Repo.GetAll(); + return blobs.Select(b => Encoding.UTF8.GetString(b.Content)) + .Select(JsonConvert.DeserializeObject) + .ToList(); + } + + public bool BatchUpdate(List model) + { + var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ReturnBytes(m), ProviderId = m.ProviderId }).ToList(); + return Repo.UpdateAll(entities); + } + + public byte[] ReturnBytes(object obj) + { + var json = JsonConvert.SerializeObject(obj); + var bytes = Encoding.UTF8.GetBytes(json); + + return bytes; + } + } +} \ No newline at end of file diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index bab564a79..b7534d1b0 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -70,6 +70,7 @@ + diff --git a/PlexRequests.Core/RequestService.cs b/PlexRequests.Core/RequestService.cs index dfa524d67..22164fabd 100644 --- a/PlexRequests.Core/RequestService.cs +++ b/PlexRequests.Core/RequestService.cs @@ -56,9 +56,9 @@ namespace PlexRequests.Core Repo.Delete(entity); } - public void UpdateRequest(int originalId, RequestedModel model) + public void UpdateRequest(RequestedModel model) { - model.Id = originalId; + Repo.Update(model); } diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 2f720f80f..3866bad6a 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -60,7 +60,7 @@ namespace PlexRequests.Core SearchForTvShows = true, WeeklyRequestLimit = 0 }; - var s = new SettingsServiceV2(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var s = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); s.SaveSettings(defaultSettings); } } diff --git a/PlexRequests.Services/AvailabilityUpdateService.cs b/PlexRequests.Services/AvailabilityUpdateService.cs index c32573d7a..a8bd6f336 100644 --- a/PlexRequests.Services/AvailabilityUpdateService.cs +++ b/PlexRequests.Services/AvailabilityUpdateService.cs @@ -49,7 +49,7 @@ namespace PlexRequests.Services public AvailabilityUpdateService() { ConfigurationReader = new ConfigurationReader(); - var repo = new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()); + var repo = new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()); Checker = new PlexAvailabilityChecker(new SettingsServiceV2(repo), new SettingsServiceV2(repo), new RequestService(new GenericRepository(new DbConfiguration(new SqliteFactory()))), new PlexApi()); HostingEnvironment.RegisterObject(this); } diff --git a/PlexRequests.Store/IRequestRepository.cs b/PlexRequests.Store/IRequestRepository.cs new file mode 100644 index 000000000..e2a07b6b5 --- /dev/null +++ b/PlexRequests.Store/IRequestRepository.cs @@ -0,0 +1,65 @@ +#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 PlexRequests.Store.Models; + +namespace PlexRequests.Store +{ + public interface IRequestRepository + { + /// + /// Inserts the specified entity. + /// + /// The entity. + long Insert(RequestBlobs entity); + + /// + /// Gets all. + /// + /// + IEnumerable GetAll(); + + RequestBlobs Get(int id); + + /// + /// Deletes the specified entity. + /// + /// The entity. + /// + bool Delete(RequestBlobs entity); + + /// + /// Updates the specified entity. + /// + /// The entity. + /// + bool Update(RequestBlobs entity); + + bool UpdateAll(IEnumerable entity); + } +} \ No newline at end of file diff --git a/PlexRequests.Store/Models/RequestBlobs.cs b/PlexRequests.Store/Models/RequestBlobs.cs new file mode 100644 index 000000000..3b1127b6a --- /dev/null +++ b/PlexRequests.Store/Models/RequestBlobs.cs @@ -0,0 +1,38 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RequestBlobs.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 PlexRequests.Store.Models +{ + [Table("RequestBlobs")] + public class RequestBlobs : Entity + { + public int ProviderId { get; set; } + public byte[] Content { get; set; } + public RequestType Type { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Store/PlexRequests.Store.csproj b/PlexRequests.Store/PlexRequests.Store.csproj index 95adcfc79..1344e2d4e 100644 --- a/PlexRequests.Store/PlexRequests.Store.csproj +++ b/PlexRequests.Store/PlexRequests.Store.csproj @@ -58,13 +58,16 @@ + + - + + diff --git a/PlexRequests.Store/Repository/RequestJsonRepository.cs b/PlexRequests.Store/Repository/RequestJsonRepository.cs new file mode 100644 index 000000000..85b1781bb --- /dev/null +++ b/PlexRequests.Store/Repository/RequestJsonRepository.cs @@ -0,0 +1,127 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SettingsJsonRepository.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 PlexRequests.Helpers; +using PlexRequests.Store.Models; + +namespace PlexRequests.Store.Repository +{ + public class RequestJsonRepository : IRequestRepository + { + private ICacheProvider Cache { get; } + + private string TypeName { get; } + public RequestJsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider) + { + Db = config; + Cache = cacheProvider; + TypeName = typeof(RequestJsonRepository).Name; + } + + private ISqliteConfiguration Db { get; } + + public long Insert(RequestBlobs 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 RequestBlobs Get(int id) + { + var key = TypeName + "Get"; + var item = Cache.GetOrSet(key, () => + { + using (var con = Db.DbConnection()) + { + var page = con.Get(id); + return page; + } + }, 5); + return item; + } + + public bool Delete(RequestBlobs entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return con.Delete(entity); + } + } + + public bool Update(RequestBlobs entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return con.Update(entity); + } + } + + private void ResetCache() + { + Cache.Remove("Get"); + Cache.Remove(TypeName + "GetAll"); + } + + public bool UpdateAll(IEnumerable entity) + { + var result = new HashSet(); + + using (var db = Db.DbConnection()) + { + db.Open(); + foreach (var e in entity) + { + result.Add(db.Update(e)); + } + } + return result.All(x => true); + } + } +} diff --git a/PlexRequests.Store/Repository/JsonRepository.cs b/PlexRequests.Store/Repository/SettingsJsonRepository.cs similarity index 90% rename from PlexRequests.Store/Repository/JsonRepository.cs rename to PlexRequests.Store/Repository/SettingsJsonRepository.cs index 5b7e2e992..64d98f78a 100644 --- a/PlexRequests.Store/Repository/JsonRepository.cs +++ b/PlexRequests.Store/Repository/SettingsJsonRepository.cs @@ -1,7 +1,7 @@ #region Copyright // /************************************************************************ // Copyright (c) 2016 Jamie Rees -// File: JsonRepository.cs +// File: SettingsJsonRepository.cs // Created By: Jamie Rees // // Permission is hereby granted, free of charge, to any person obtaining @@ -34,16 +34,16 @@ using PlexRequests.Store.Models; namespace PlexRequests.Store.Repository { - public class JsonRepository : ISettingsRepository + public class SettingsJsonRepository : ISettingsRepository { private ICacheProvider Cache { get; set; } private string TypeName { get; set; } - public JsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider) + public SettingsJsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider) { Db = config; Cache = cacheProvider; - TypeName = typeof(JsonRepository).Name; + TypeName = typeof(SettingsJsonRepository).Name; } private ISqliteConfiguration Db { get; set; } diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index 08401d5ef..dee07581d 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS Settings PlexAuthToken varchar(50) ); + CREATE TABLE IF NOT EXISTS Requested ( Id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -45,6 +46,14 @@ CREATE TABLE IF NOT EXISTS GlobalSettings Content varchar(100) NOT NULL ); +CREATE TABLE IF NOT EXISTS RequestBlobs +( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + ProviderId INTEGER NOT NULL, + Type INTEGER NOT NULL, + Content BLOB NOT NULL +); + CREATE TABLE IF NOT EXISTS Log ( diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index 1d7086e2a..f5353f4f8 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -64,7 +64,7 @@ namespace PlexRequests.UI container.Register(new DbConfiguration(new SqliteFactory())); - container.Register(); + container.Register(); container.Register(); container.Register, SettingsServiceV2>(); diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index 4ae59f59e..9db2c673b 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -105,7 +105,7 @@ namespace PlexRequests.UI { Log.Trace("Getting startup URI"); var uri = "http://*:3579/"; - var service = new SettingsServiceV2(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var service = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); var settings = service.GetSettings(); Log.Trace("Port: {0}", settings.Port); if (settings.Port != 0)