diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 207745298..0ef67ce52 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -48,11 +48,16 @@ namespace PlexRequests.Core Db = new DbConfiguration(new SqliteFactory()); var created = Db.CheckDb(); TableCreation.CreateTables(Db.DbConnection()); - + if (created) { CreateDefaultSettingsPage(urlBase); } + else + { + // Shrink DB + TableCreation.Vacuum(Db.DbConnection()); + } var version = CheckSchema(); if (version > 0) diff --git a/PlexRequests.Services/Jobs/SonarrCacher.cs b/PlexRequests.Services/Jobs/SonarrCacher.cs index 97089da27..26e2b36cb 100644 --- a/PlexRequests.Services/Jobs/SonarrCacher.cs +++ b/PlexRequests.Services/Jobs/SonarrCacher.cs @@ -61,12 +61,9 @@ namespace PlexRequests.Services.Jobs public void Queued() { - Log.Trace("Getting the settings"); - var settings = SonarrSettings.GetSettings(); if (settings.Enabled) { - Log.Trace("Getting all tv series from Sonarr"); try { var series = SonarrApi.GetSeries(settings.ApiKey, settings.FullUri); diff --git a/PlexRequests.Store/TableCreation.cs b/PlexRequests.Store/TableCreation.cs index 1e3a9d5a7..3e6336deb 100644 --- a/PlexRequests.Store/TableCreation.cs +++ b/PlexRequests.Store/TableCreation.cs @@ -1,130 +1,141 @@ -#region Copyright -// *********************************************************************** -// Copyright (c) 2016 Jamie Rees -// File: TableCreation.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.Data; -using System.Linq; -using Dapper; -using Dapper.Contrib.Extensions; - -namespace PlexRequests.Store -{ - public static class TableCreation - { - /// - /// Creates the tables located in the SqlTables.sql file. - /// - /// The connection. - public static void CreateTables(IDbConnection connection) - { - connection.Open(); - connection.Execute(Sql.SqlTables); - connection.Close(); - } - - public static void DropTable(IDbConnection con, string tableName) - { - using (con) - { - con.Open(); - var query = $"DROP TABLE IF EXISTS {tableName}"; - con.Execute(query); - con.Close(); - } - } - - public static void AddColumn(IDbConnection connection, string tableName, string alterType, string newColumn, bool isNullable, string dataType) - { - connection.Open(); - var result = connection.Query($"PRAGMA table_info({tableName});"); - if (result.Any(x => x.name == newColumn)) - { - return; - } - - var query = $"ALTER TABLE {tableName} {alterType} {newColumn} {dataType}"; - if (isNullable) - { - query = query + " NOT NULL"; - } - - connection.Execute(query); - - connection.Close(); - } - - public static DbInfo GetSchemaVersion(this IDbConnection con) - { - con.Open(); - var result = con.Query("SELECT * FROM DBInfo"); - con.Close(); - - return result.FirstOrDefault(); - } - - public static void UpdateSchemaVersion(this IDbConnection con, int version) - { - con.Open(); - con.Query($"UPDATE DBInfo SET SchemaVersion = {version}"); - con.Close(); - } - - public static void CreateSchema(this IDbConnection con, int version) - { - con.Open(); - con.Query($"INSERT INTO DBInfo (SchemaVersion) values ({version})"); - con.Close(); - } - - - - [Table("DBInfo")] - public class DbInfo - { - public int SchemaVersion { get; set; } - } - - [Table("sqlite_master")] - public class SqliteMasterTable - { - public string type { get; set; } - public string name { get; set; } - public string tbl_name { get; set; } - [Key] - public long rootpage { get; set; } - public string sql { get; set; } - } - - [Table("table_info")] - public class TableInfo - { - public int cid { get; set; } - public string name { get; set; } - public int notnull { get; set; } - public string dflt_value { get; set; } - public int pk { get; set; } - } - } -} +#region Copyright +// *********************************************************************** +// Copyright (c) 2016 Jamie Rees +// File: TableCreation.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.Data; +using System.Linq; +using Dapper; +using Dapper.Contrib.Extensions; + +namespace PlexRequests.Store +{ + public static class TableCreation + { + /// + /// Creates the tables located in the SqlTables.sql file. + /// + /// The connection. + public static void CreateTables(IDbConnection connection) + { + connection.Open(); + connection.Execute(Sql.SqlTables); + connection.Close(); + } + + public static void DropTable(IDbConnection con, string tableName) + { + using (con) + { + con.Open(); + var query = $"DROP TABLE IF EXISTS {tableName}"; + con.Execute(query); + con.Close(); + } + } + + public static void AddColumn(IDbConnection connection, string tableName, string alterType, string newColumn, bool isNullable, string dataType) + { + connection.Open(); + var result = connection.Query($"PRAGMA table_info({tableName});"); + if (result.Any(x => x.name == newColumn)) + { + return; + } + + var query = $"ALTER TABLE {tableName} {alterType} {newColumn} {dataType}"; + if (isNullable) + { + query = query + " NOT NULL"; + } + + connection.Execute(query); + + connection.Close(); + } + + public static void Vacuum(IDbConnection con) + { + using (con) + { + con.Open(); + + con.Query("VACUUM;"); + + } + } + + public static DbInfo GetSchemaVersion(this IDbConnection con) + { + con.Open(); + var result = con.Query("SELECT * FROM DBInfo"); + con.Close(); + + return result.FirstOrDefault(); + } + + public static void UpdateSchemaVersion(this IDbConnection con, int version) + { + con.Open(); + con.Query($"UPDATE DBInfo SET SchemaVersion = {version}"); + con.Close(); + } + + public static void CreateSchema(this IDbConnection con, int version) + { + con.Open(); + con.Query($"INSERT INTO DBInfo (SchemaVersion) values ({version})"); + con.Close(); + } + + + + [Table("DBInfo")] + public class DbInfo + { + public int SchemaVersion { get; set; } + } + + [Table("sqlite_master")] + public class SqliteMasterTable + { + public string type { get; set; } + public string name { get; set; } + public string tbl_name { get; set; } + [Key] + public long rootpage { get; set; } + public string sql { get; set; } + } + + [Table("table_info")] + public class TableInfo + { + public int cid { get; set; } + public string name { get; set; } + public int notnull { get; set; } + public string dflt_value { get; set; } + public int pk { get; set; } + } + } +} diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index a33c5165e..ad15e8e43 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -314,8 +314,6 @@ namespace PlexRequests.UI.Modules var viewTv = new List(); foreach (var t in apiTv) { - var tvInfoTask = Task.Run(() => TvApi.EpisodeLookup(t.show.id)); - var banner = t.show.image?.medium; if (!string.IsNullOrEmpty(banner)) { @@ -351,7 +349,6 @@ namespace PlexRequests.UI.Modules else if (t.show?.externals?.thetvdb != null) { var tvdbid = (int)t.show.externals.thetvdb; - if (dbTv.ContainsKey(tvdbid)) { var dbt = dbTv[tvdbid]; @@ -361,20 +358,12 @@ namespace PlexRequests.UI.Modules viewT.Approved = dbt.Approved; viewT.Available = dbt.Available; } - else if (sonarrCached.Contains(tvdbid) || sickRageCache.Contains(tvdbid)) // compare to the sonarr/sickrage db + if (sonarrCached.Contains(tvdbid) || sickRageCache.Contains(tvdbid)) // compare to the sonarr/sickrage db { viewT.Requested = true; } } - var tvInfo = await tvInfoTask; - - // Check if we have every episode in all seasons - var epModel = tvInfo.Select(tvIn => new Store.EpisodesModel { SeasonNumber = tvIn.season, EpisodeNumber = tvIn.number }).ToList(); - var diff = viewT.Episodes.Except(epModel); - if (diff.Any()) - { - viewT.TvFullyAvailable = true; - } + viewTv.Add(viewT); } @@ -978,11 +967,11 @@ namespace PlexRequests.UI.Modules var model = new List(); - var enumerable = allResults as RequestedModel[] ?? allResults.ToArray(); + var requests = allResults as RequestedModel[] ?? allResults.ToArray(); - var dbDbShow = enumerable.FirstOrDefault(x => x.Type == RequestType.TvShow && x.TvDbId == seriesId.ToString()); + var existingRequest = requests.FirstOrDefault(x => x.Type == RequestType.TvShow && x.TvDbId == seriesId.ToString()); var show = await Task.Run(() => TvApi.ShowLookupByTheTvDbId(seriesId)); - var seasons = await Task.Run(() => TvApi.EpisodeLookup(show.id)); + var tvMaxeEpisodes = await Task.Run(() => TvApi.EpisodeLookup(show.id)); var sonarrEpisodes = new List(); if (sonarrEnabled) @@ -994,9 +983,9 @@ namespace PlexRequests.UI.Modules var plexCacheTask = await Checker.GetEpisodes(seriesId); var plexCache = plexCacheTask.ToList(); - foreach (var ep in seasons) + foreach (var ep in tvMaxeEpisodes) { - var requested = dbDbShow?.Episodes + var requested = existingRequest?.Episodes .Any(episodesModel => ep.number == episodesModel.EpisodeNumber && ep.season == episodesModel.SeasonNumber) ?? false; diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index f9878acbb..d71a3976c 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -149,10 +149,7 @@ namespace PlexRequests.UI var settingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); var logSettings = settingsService.GetSettings(); - if (logSettings != null) - { - LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level)); - } + LoggingHelper.ReconfigureLogLevel(logSettings != null ? LogLevel.FromOrdinal(logSettings.Level) : LogLevel.FromOrdinal(4)); } private static void PrintToConsole(string message, ConsoleColor colour = ConsoleColor.Gray) diff --git a/PlexRequests.UI/Views/Search/Index.cshtml b/PlexRequests.UI/Views/Search/Index.cshtml index a35ffd031..b27c6705b 100644 --- a/PlexRequests.UI/Views/Search/Index.cshtml +++ b/PlexRequests.UI/Views/Search/Index.cshtml @@ -185,6 +185,7 @@ {{/if_eq}} {{#if_eq type "tv"}} {{#if_eq tvFullyAvailable true}} + @*//TODO Not used yet*@ {{else}}