diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs index d8b1b1739..fb68fab37 100644 --- a/PlexRequests.Core/IRequestService.cs +++ b/PlexRequests.Core/IRequestService.cs @@ -32,7 +32,7 @@ namespace PlexRequests.Core { public interface IRequestService { - long AddRequest(int providerId, RequestedModel model); + long AddRequest(RequestedModel model); bool CheckRequest(int providerId); void DeleteRequest(RequestedModel request); bool UpdateRequest(RequestedModel model); diff --git a/PlexRequests.Core/JsonRequestService.cs b/PlexRequests.Core/JsonRequestService.cs index 67163e6e7..1a99569d3 100644 --- a/PlexRequests.Core/JsonRequestService.cs +++ b/PlexRequests.Core/JsonRequestService.cs @@ -42,7 +42,7 @@ namespace PlexRequests.Core Repo = repo; } private IRequestRepository Repo { get; } - public long AddRequest(int providerId, RequestedModel model) + public long AddRequest(RequestedModel model) { var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId }; var id = Repo.Insert(entity); diff --git a/PlexRequests.Core/RequestService.cs b/PlexRequests.Core/RequestService.cs index 99ab20bf4..7788b6436 100644 --- a/PlexRequests.Core/RequestService.cs +++ b/PlexRequests.Core/RequestService.cs @@ -40,7 +40,7 @@ namespace PlexRequests.Core private IRepository Repo { get; set; } - public long AddRequest(int providerId, RequestedModel model) + public long AddRequest(RequestedModel model) { return Repo.Insert(model); } diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 3866bad6a..1d50794aa 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -24,6 +24,9 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion +using System.Collections.Generic; +using System.Linq; + using Mono.Data.Sqlite; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; @@ -46,6 +49,7 @@ namespace PlexRequests.Core CreateDefaultSettingsPage(); } + MigrateDb(); return Db.DbConnection().ConnectionString; } @@ -63,5 +67,28 @@ namespace PlexRequests.Core var s = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); s.SaveSettings(defaultSettings); } + + private void MigrateDb() + { + var repo = new GenericRepository(Db); + var records = repo.GetAll(); + var requestedModels = records as RequestedModel[] ?? records.ToArray(); + if(!requestedModels.Any()) + { return; } + + var jsonRepo = new JsonRequestService(new RequestJsonRepository(Db, new MemoryCacheProvider())); + var result = new List(); + foreach (var r in requestedModels) + { + var id = jsonRepo.AddRequest(r); + result.Add(id); + } + + if (result.Any(x => x == -1)) + { + throw new SqliteException("Could not migrate the DB!"); + } + + } } } diff --git a/PlexRequests.Store/PlexRequests.Store.csproj b/PlexRequests.Store/PlexRequests.Store.csproj index 45ae52a0b..3772c77a4 100644 --- a/PlexRequests.Store/PlexRequests.Store.csproj +++ b/PlexRequests.Store/PlexRequests.Store.csproj @@ -68,7 +68,6 @@ - diff --git a/PlexRequests.Store/SettingsModel.cs b/PlexRequests.Store/SettingsModel.cs deleted file mode 100644 index 3c8913cd3..000000000 --- a/PlexRequests.Store/SettingsModel.cs +++ /dev/null @@ -1,38 +0,0 @@ -#region Copyright -// /************************************************************************ -// Copyright (c) 2016 Jamie Rees -// File: SettingsModel.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 -{ - [Table("Settings")] - public class SettingsModel : Entity - { - public int Port { get; set; } - public bool UserAuthentication { get; set; } - public string PlexAuthToken { get; set; } - } -} diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index dee07581d..d696da6b4 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -8,36 +8,6 @@ CREATE TABLE IF NOT EXISTS User Password varchar(100) NOT NULL ); -CREATE TABLE IF NOT EXISTS Settings -( - Id INTEGER PRIMARY KEY AUTOINCREMENT, - Port INTEGER NOT NULL, - UserAuthentication INTEGER NOT NULL, - PlexAuthToken varchar(50) -); - - -CREATE TABLE IF NOT EXISTS Requested -( - Id INTEGER PRIMARY KEY AUTOINCREMENT, - Type INTEGER NOT NULL, - ProviderId INTEGER NOT NULL, - ImdbId varchar(50), - Overview varchar(50), - Title varchar(50) NOT NULL, - PosterPath varchar(50) NOT NULL, - ReleaseDate varchar(50) NOT NULL, - Status varchar(50) NOT NULL, - AdminNote varchar(50), - Approved INTEGER NOT NULL, - LatestTv INTEGER NOT NULL, - RequestedBy varchar(50), - RequestedDate varchar(50) NOT NULL, - Available INTEGER(50), - Issues INTEGER, - OtherMessage varchar(50) - -); CREATE TABLE IF NOT EXISTS GlobalSettings ( diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 44c7a83a3..6556f2e3c 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -225,7 +225,7 @@ namespace PlexRequests.UI.Modules { model.Approved = true; Log.Debug("Adding movie to database requests (No approval required)"); - RequestService.AddRequest(movieId, model); + RequestService.AddRequest(model); return Response.AsJson(new JsonResponseModel { Result = true }); } @@ -235,7 +235,7 @@ namespace PlexRequests.UI.Modules try { Log.Debug("Adding movie to database requests"); - var id = RequestService.AddRequest(movieId, model); + var id = RequestService.AddRequest(model); NotificationService.Publish(model.Title, model.RequestedBy); @@ -307,14 +307,14 @@ namespace PlexRequests.UI.Modules { model.Approved = true; Log.Debug("Adding tv to database requests (No approval required)"); - RequestService.AddRequest(showId, model); + RequestService.AddRequest(model); return Response.AsJson(new JsonResponseModel { Result = true }); } return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." }); } - RequestService.AddRequest(showId, model); + RequestService.AddRequest(model); NotificationService.Publish(model.Title, model.RequestedBy); return Response.AsJson(new { Result = true });