diff --git a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs index 668e1321c..dc0e96464 100644 --- a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs +++ b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs @@ -32,7 +32,7 @@ namespace PlexRequests.Core.SettingModels public bool SearchForMovies { get; set; } public bool SearchForTvShows { get; set; } - public bool RequireApprovial { get; set; } + public bool RequireApproval { get; set; } public int WeeklyRequestLimit { get; set; } } } diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index bcd6f40c5..7d71d332d 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -25,8 +25,10 @@ // ************************************************************************/ #endregion using Mono.Data.Sqlite; - +using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; using PlexRequests.Store; +using PlexRequests.Store.Repository; namespace PlexRequests.Core { @@ -36,9 +38,29 @@ namespace PlexRequests.Core public string SetupDb() { var db = new DbConfiguration(new SqliteFactory()); - db.CheckDb(); + var created = db.CheckDb(); TableCreation.CreateTables(db.DbConnection()); + + if (created) + { + CreateDefaultSettingsPage(); + } + return db.DbConnection().ConnectionString; } + + + private void CreateDefaultSettingsPage() + { + var defaultSettings = new PlexRequestSettings + { + RequireApproval = true, + SearchForMovies = true, + SearchForTvShows = true, + WeeklyRequestLimit = 0 + }; + var s = new SettingsServiceV2(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + s.SaveSettings(defaultSettings); + } } } diff --git a/PlexRequests.Store/DbConfiguration.cs b/PlexRequests.Store/DbConfiguration.cs index bccd679c0..d0d5cd16e 100644 --- a/PlexRequests.Store/DbConfiguration.cs +++ b/PlexRequests.Store/DbConfiguration.cs @@ -31,6 +31,8 @@ using System.IO; using Mono.Data.Sqlite; using NLog; +using PlexRequests.Helpers; +using PlexRequests.Store.Repository; namespace PlexRequests.Store { @@ -44,14 +46,16 @@ namespace PlexRequests.Store private SqliteFactory Factory { get; set; } - public virtual void CheckDb() + public virtual bool CheckDb() { Log.Trace("Checking DB"); if (!File.Exists(DbFile)) { Log.Trace("DB doesn't exist, creating a new one"); CreateDatabase(); + return true; } + return false; } public string DbFile = "RequestPlex.sqlite"; diff --git a/PlexRequests.Store/ISqliteConfiguration.cs b/PlexRequests.Store/ISqliteConfiguration.cs index d0ac8427a..19c5b6756 100644 --- a/PlexRequests.Store/ISqliteConfiguration.cs +++ b/PlexRequests.Store/ISqliteConfiguration.cs @@ -33,7 +33,7 @@ namespace PlexRequests.Store /// /// Checks the database. /// - void CheckDb(); + bool CheckDb(); /// /// Returns the database connection. diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 6c5a5cb3f..b11da1746 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -30,7 +30,8 @@ using Humanizer; using Nancy; using Nancy.Responses.Negotiation; - +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; using PlexRequests.Store; using PlexRequests.UI.Models; @@ -38,10 +39,11 @@ namespace PlexRequests.UI.Modules { public class RequestsModule : BaseModule { - private IRepository Service { get; set; } - public RequestsModule(IRepository service) : base("requests") + + public RequestsModule(IRepository service, ISettingsService prSettings) : base("requests") { Service = service; + PrSettings = prSettings; Get["/"] = _ => LoadRequests(); Get["/movies"] = _ => GetMovies(); @@ -52,11 +54,13 @@ namespace PlexRequests.UI.Modules return DeleteRequest((int)Request.Form.id, convertedType); }; } - + private IRepository Service { get; } + private ISettingsService PrSettings { get; } private Negotiator LoadRequests() { - return View["Requests/Index"]; + var settings = PrSettings.GetSettings(); + return View["Requests/Index", settings]; } private Response GetMovies() diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 86e15ee76..3d8c30d67 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -43,13 +43,14 @@ namespace PlexRequests.UI.Modules { public class SearchModule : BaseModule { - public SearchModule(ICacheProvider cache, ISettingsService cpSettings) : base("search") + public SearchModule(ICacheProvider cache, ISettingsService cpSettings, ISettingsService prSettings) : base("search") { CpService = cpSettings; + PrService = prSettings; MovieApi = new TheMovieDbApi(); TvApi = new TheTvDbApi(); Cache = cache; - + Get["/"] = parameters => RequestLoad(); Get["movie/{searchTerm}"] = parameters => SearchMovie((string)parameters.searchTerm); @@ -64,14 +65,17 @@ namespace PlexRequests.UI.Modules private TheMovieDbApi MovieApi { get; } private TheTvDbApi TvApi { get; } private ICacheProvider Cache { get; } - private ISettingsService CpService { get; set; } + private ISettingsService CpService { get; } + private ISettingsService PrService { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50); private Negotiator RequestLoad() { + var settings = PrService.GetSettings(); + Log.Trace("Loading Index"); - return View["Search/Index"]; + return View["Search/Index", settings]; } private Response SearchMovie(string searchTerm) @@ -103,7 +107,7 @@ namespace PlexRequests.UI.Modules Aliases = t.aliases, // We are constructing the banner with the id: // http://thetvdb.com/banners/_cache/posters/ID-1.jpg - Banner = t.id.ToString(), + Banner = t.id.ToString(), FirstAired = t.firstAired, Genre = t.genre, Id = t.id, @@ -148,14 +152,14 @@ namespace PlexRequests.UI.Modules return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" }); } Log.Trace("movie with id {0} doesnt exists", movieId); - var settings = CpService.GetSettings(); - if (settings.ApiKey == null) + var cpSettings = CpService.GetSettings(); + if (cpSettings.ApiKey == null) { Log.Warn("CP apiKey is null"); return Response.AsJson(new { Result = false, Message = "CouchPotato is not yet configured, If you are the Admin, please log in." }); } Log.Trace("Settings: "); - Log.Trace(settings.DumpJson); + Log.Trace(cpSettings.DumpJson); var movieApi = new TheMovieDbApi(); var movieInfo = movieApi.GetMovieInformation(movieId).Result; @@ -173,21 +177,41 @@ namespace PlexRequests.UI.Modules ReleaseDate = movieInfo.ReleaseDate ?? DateTime.MinValue, Status = movieInfo.Status, RequestedDate = DateTime.Now, - Approved = false + Approved = false, + RequestedBy = Session[SessionKeys.UsernameKey].ToString() }; - var cp = new CouchPotatoApi(); - Log.Trace("Adding movie to CP"); - var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.FullUri); - Log.Trace("Adding movie to CP result {0}", result); - if (result) + + var settings = PrService.GetSettings(); + if (!settings.RequireApproval) + { + var cp = new CouchPotatoApi(); + Log.Trace("Adding movie to CP (No approval required)"); + var result = cp.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri); + Log.Trace("Adding movie to CP result {0}", result); + if (result) + { + model.Approved = true; + Log.Trace("Adding movie to database requests (No approval required)"); + s.AddRequest(movieId, model); + + return Response.AsJson(new { Result = true }); + } + return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." }); + } + + try { Log.Trace("Adding movie to database requests"); s.AddRequest(movieId, model); - return Response.AsJson(new { Result = true }); } - return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." }); + catch (Exception e) + { + Log.Fatal(e); + + return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." }); + } } /// @@ -213,7 +237,7 @@ namespace PlexRequests.UI.Modules DateTime firstAir; DateTime.TryParse(showInfo.firstAired, out firstAir); - var model = new RequestedModel + var model = new RequestedModel { ProviderId = showInfo.id, Type = RequestType.TvShow, @@ -223,11 +247,12 @@ namespace PlexRequests.UI.Modules ReleaseDate = firstAir, Status = showInfo.status, RequestedDate = DateTime.Now, - Approved = false + Approved = false, + RequestedBy = Session[SessionKeys.UsernameKey].ToString() }; s.AddRequest(showId, model); - return Response.AsJson(new {Result = true }); + return Response.AsJson(new { Result = true }); } private string GetAuthToken(TheTvDbApi api) { diff --git a/PlexRequests.UI/Views/Admin/Settings.cshtml b/PlexRequests.UI/Views/Admin/Settings.cshtml index 3bb514d5e..780e9f46f 100644 --- a/PlexRequests.UI/Views/Admin/Settings.cshtml +++ b/PlexRequests.UI/Views/Admin/Settings.cshtml @@ -54,7 +54,24 @@ +
+ + +
+ +
+
+
diff --git a/PlexRequests.UI/Views/Admin/_Sidebar.cshtml b/PlexRequests.UI/Views/Admin/_Sidebar.cshtml index ac6648ad8..140166eb3 100644 --- a/PlexRequests.UI/Views/Admin/_Sidebar.cshtml +++ b/PlexRequests.UI/Views/Admin/_Sidebar.cshtml @@ -1,6 +1,6 @@ 
- Request Plex Settings + Plex Request Settings Authentication CouchPotato Settings Sonarr Settings diff --git a/PlexRequests.UI/Views/Requests/Index.cshtml b/PlexRequests.UI/Views/Requests/Index.cshtml index 64c111034..b6a5ae8ea 100644 --- a/PlexRequests.UI/Views/Requests/Index.cshtml +++ b/PlexRequests.UI/Views/Requests/Index.cshtml @@ -3,31 +3,42 @@

Below you can see yours and all other requests, as well as their download and approval status.

- - -
-
-
- -
+ @if (Model.SearchForMovies) + { + +
+
+
+ +
+
-
+ } - -
-
-
- -
+ @if (Model.SearchForTvShows) + { + +
+
+
+ +
+
-
+ }
diff --git a/PlexRequests.UI/Views/Search/Index.cshtml b/PlexRequests.UI/Views/Search/Index.cshtml index 03c8ea86d..375379d28 100644 --- a/PlexRequests.UI/Views/Search/Index.cshtml +++ b/PlexRequests.UI/Views/Search/Index.cshtml @@ -1,47 +1,60 @@ 

Search

-

Want to wacth something that is not currently on Plex?! No problem! Just search for it below and request it!

+

Want to watch something that is not currently on Plex?! No problem! Just search for it below and request it!

- - -
-
- -
- + @if (Model.SearchForMovies) + { + +
+
+ +
+ +
+
+
+
+ +
-
-
- -
-
-
+ } + - -
-
- -
- + @if (Model.SearchForTvShows) + { + +
+
+ +
+ +
+
+
+
+ +
-
-
- -
-
-
+ }
+