diff --git a/PlexRequests.Api.Interfaces/ISonarrApi.cs b/PlexRequests.Api.Interfaces/ISonarrApi.cs index 5c5594ad8..83282a1ab 100644 --- a/PlexRequests.Api.Interfaces/ISonarrApi.cs +++ b/PlexRequests.Api.Interfaces/ISonarrApi.cs @@ -35,7 +35,10 @@ public interface ISonarrApi { List GetProfiles(string apiKey, Uri baseUrl); + List GetRootFolders(string apiKey, Uri baseUrl); + SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, + int rootFolderId, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false); diff --git a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj index 20363d996..453d2cae9 100644 --- a/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj +++ b/PlexRequests.Api.Models/PlexRequests.Api.Models.csproj @@ -89,6 +89,7 @@ + diff --git a/PlexRequests.Api.Models/Sonarr/SonarrRootFolder.cs b/PlexRequests.Api.Models/Sonarr/SonarrRootFolder.cs new file mode 100644 index 000000000..d93986dd6 --- /dev/null +++ b/PlexRequests.Api.Models/Sonarr/SonarrRootFolder.cs @@ -0,0 +1,38 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SonarrProfile.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; + +namespace PlexRequests.Api.Models.Sonarr +{ + public class SonarrRootFolder + { + public int id { get; set; } + public string path { get; set; } + public long freespace { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Api/SonarrApi.cs b/PlexRequests.Api/SonarrApi.cs index 6cc8019f3..cd9d67437 100644 --- a/PlexRequests.Api/SonarrApi.cs +++ b/PlexRequests.Api/SonarrApi.cs @@ -64,7 +64,24 @@ public List GetProfiles(string apiKey, Uri baseUrl) return obj; } - public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false) + + public List GetRootFolders(string apiKey, Uri baseUrl) + { + var request = new RestRequest { Resource = "/api/rootfolder", Method = Method.GET }; + + request.AddHeader("X-Api-Key", apiKey); + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetRootFolders for Sonarr, Retrying {0}", timespan), new TimeSpan[] { + TimeSpan.FromSeconds (2), + TimeSpan.FromSeconds(5), + TimeSpan.FromSeconds(10) + }); + + var obj = policy.Execute(() => Api.ExecuteJson>(request, baseUrl)); + + return obj; + } + + public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int rootFolderId, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false) { Log.Debug("Adding series {0}", title); Log.Debug("Seasons = {0}, out of {1} seasons", seasons.DumpJson(), seasonCount); diff --git a/PlexRequests.Core/CacheKeys.cs b/PlexRequests.Core/CacheKeys.cs index 0f718f1f1..3409c1b81 100644 --- a/PlexRequests.Core/CacheKeys.cs +++ b/PlexRequests.Core/CacheKeys.cs @@ -37,6 +37,7 @@ public struct TimeFrameMinutes public const string PlexEpisodes = nameof(PlexEpisodes); public const string TvDbToken = nameof(TvDbToken); public const string SonarrQualityProfiles = nameof(SonarrQualityProfiles); + public const string SonarrRootFolders = nameof(SonarrRootFolders); public const string SonarrQueued = nameof(SonarrQueued); public const string SickRageQualityProfiles = nameof(SickRageQualityProfiles); public const string SickRageQueued = nameof(SickRageQueued); diff --git a/PlexRequests.Core/SettingModels/SonarrSettings.cs b/PlexRequests.Core/SettingModels/SonarrSettings.cs index 5b99b8ac2..f347f1dd2 100644 --- a/PlexRequests.Core/SettingModels/SonarrSettings.cs +++ b/PlexRequests.Core/SettingModels/SonarrSettings.cs @@ -31,8 +31,10 @@ public sealed class SonarrSettings : ExternalSettings public bool Enabled { get; set; } public string ApiKey { get; set; } public string QualityProfile { get; set; } - public bool SeasonFolders { get; set; } + public string RootFolder { get; set; } public string RootPath { get; set; } + public bool SeasonFolders { get; set; } + } } \ No newline at end of file diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index dac15a731..7e10af85b 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -135,6 +135,20 @@ public void CacheQualityProfiles() } } + public void CacheRootFolders() + { + var mc = new MemoryCacheProvider(); + + try + { + Task.Run(() => { CacheSonarrRootFolders(mc); }); + } + catch (Exception) + { + Log.Error("Failed to cache quality profiles on startup!"); + } + } + private void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider) { try @@ -157,6 +171,28 @@ private void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider) } } + private void CacheSonarrRootFolders(MemoryCacheProvider cacheProvider) + { + try + { + Log.Info("Executing GetSettings call to Sonarr for root folders"); + var sonarrSettingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var sonarrSettings = sonarrSettingsService.GetSettings(); + if (sonarrSettings.Enabled) + { + Log.Info("Begin executing GetSettings call to Sonarr for root folders"); + SonarrApi sonarrApi = new SonarrApi(); + var rootFolders = sonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri); + cacheProvider.Set(CacheKeys.SonarrRootFolders, rootFolders); + Log.Info("Finished executing GetSettings call to Sonarr for root folders"); + } + } + catch (Exception ex) + { + Log.Error(ex, "Failed to cache Sonarr quality profiles!"); + } + } + private void CacheCouchPotatoQualityProfiles(MemoryCacheProvider cacheProvider) { try diff --git a/PlexRequests.UI.Tests/TvSenderTests.cs b/PlexRequests.UI.Tests/TvSenderTests.cs index eb49f8b45..6232f00c3 100644 --- a/PlexRequests.UI.Tests/TvSenderTests.cs +++ b/PlexRequests.UI.Tests/TvSenderTests.cs @@ -76,6 +76,7 @@ public async Task HappyPathSendSeriesToSonarr() It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), @@ -93,6 +94,7 @@ public async Task HappyPathSendSeriesToSonarr() It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), @@ -113,6 +115,7 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr() It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), @@ -142,19 +145,23 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr() var model = F.Build().With(x => x.ProviderId, 1) .With(x => x.Episodes, episodes).Create(); - var result = await Sender.SendToSonarr(GetSonarrSettings(), model, "2"); + var result = await Sender.SendToSonarr(GetSonarrSettings(), model, "2", "1"); Assert.That(result, Is.EqualTo(seriesResult)); - SonarrMock.Verify(x => x.AddSeries(It.IsAny(), + SonarrMock.Verify(x => x.AddSeries( + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), // rootFolderId It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - true, It.IsAny()), Times.Once); + true, + It.IsAny() + ), Times.Once); } [Test] diff --git a/PlexRequests.UI/Helpers/TvSender.cs b/PlexRequests.UI/Helpers/TvSender.cs index fe1572044..171470d72 100644 --- a/PlexRequests.UI/Helpers/TvSender.cs +++ b/PlexRequests.UI/Helpers/TvSender.cs @@ -55,10 +55,10 @@ public TvSender(ISonarrApi sonarrApi, ISickRageApi srApi) public async Task SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model) { - return await SendToSonarr(sonarrSettings, model, string.Empty); + return await SendToSonarr(sonarrSettings, model, string.Empty, string.Empty); } - public async Task SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId) + public async Task SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId, string rootFolderId) { var qualityProfile = 0; var episodeRequest = model.Episodes.Any(); @@ -72,6 +72,17 @@ public async Task SendToSonarr(SonarrSettings sonarrSettings, R int.TryParse(sonarrSettings.QualityProfile, out qualityProfile); } + var rootFolder = 0; + if (!string.IsNullOrEmpty(rootFolderId)) + { + int.TryParse(qualityId, out rootFolder); + } + + if (rootFolder <= 0) + { + int.TryParse(sonarrSettings.RootFolder, out rootFolder); + } + var series = await GetSonarrSeries(sonarrSettings, model.ProviderId); if (episodeRequest) @@ -88,7 +99,9 @@ public async Task SendToSonarr(SonarrSettings sonarrSettings, R // Series doesn't exist, need to add it as unmonitored. var addResult = await Task.Run(() => SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, - sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, new int[0], sonarrSettings.ApiKey, + sonarrSettings.SeasonFolders, sonarrSettings.RootPath, + 1, // rootFolderId + 0, new int[0], sonarrSettings.ApiKey, sonarrSettings.FullUri, false)); @@ -160,7 +173,9 @@ public async Task SendToSonarr(SonarrSettings sonarrSettings, R var result = SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile, - sonarrSettings.SeasonFolders, sonarrSettings.RootPath, model.SeasonCount, model.SeasonList, sonarrSettings.ApiKey, + sonarrSettings.SeasonFolders, sonarrSettings.RootPath, + rootFolder, + model.SeasonCount, model.SeasonList, sonarrSettings.ApiKey, sonarrSettings.FullUri, true, true); return result; diff --git a/PlexRequests.UI/ModelDataProviders/SonarrSettingsDataProvider.cs b/PlexRequests.UI/ModelDataProviders/SonarrSettingsDataProvider.cs index b1019eb58..6cd58862b 100644 --- a/PlexRequests.UI/ModelDataProviders/SonarrSettingsDataProvider.cs +++ b/PlexRequests.UI/ModelDataProviders/SonarrSettingsDataProvider.cs @@ -50,6 +50,7 @@ public SwaggerModelData GetModelData() with.Property(x => x.SubDir).Description("Subdir/BaseUrl of Sonarr").Required(false); with.Property(x => x.ApiKey).Description("Sonarr's API key").Required(true); with.Property(x => x.QualityProfile).Description("Sonarr's quality profile").Required(true); + with.Property(x => x.RootFolder).Description("Sonarr's root folder").Required(true); with.Property(x => x.SeasonFolders).Description("Sonarr's season folders").Required(false); diff --git a/PlexRequests.UI/Models/RequestViewModel.cs b/PlexRequests.UI/Models/RequestViewModel.cs index 6d1ecb0c5..112c742fa 100644 --- a/PlexRequests.UI/Models/RequestViewModel.cs +++ b/PlexRequests.UI/Models/RequestViewModel.cs @@ -53,6 +53,7 @@ public class RequestViewModel public string TvSeriesRequestType { get; set; } public string MusicBrainzId { get; set; } public QualityModel[] Qualities { get; set; } + public RootFolderModel[] RootFolders { get; set; } public string ArtistName { get; set; } public Store.EpisodesModel[] Episodes { get; set; } public bool Denied { get; set; } diff --git a/PlexRequests.UI/Models/RootFolderModel.cs b/PlexRequests.UI/Models/RootFolderModel.cs new file mode 100644 index 000000000..afdee12f6 --- /dev/null +++ b/PlexRequests.UI/Models/RootFolderModel.cs @@ -0,0 +1,10 @@ +namespace PlexRequests.UI.Models +{ + public class RootFolderModel + { + public string Id { get; set; } + public string Path { get; set; } + public long FreeSpace { get; set; } + } +} + diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index a04edca3c..6f24c5d0e 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -170,6 +170,7 @@ public AdminModule(ISettingsService prService, Post["/sickrage"] = _ => SaveSickrage(); Post["/sonarrprofiles"] = _ => GetSonarrQualityProfiles(); + Post["/sonarrrootfolders"] = _ => GetSonarrRootFolders(); Post["/cpprofiles", true] = async (x, ct) => await GetCpProfiles(); Post["/cpapikey"] = x => GetCpApiKey(); @@ -470,6 +471,19 @@ private Response GetSonarrQualityProfiles() return Response.AsJson(profiles); } + private Response GetSonarrRootFolders() + { + var settings = this.Bind(); + var rootFolders = SonarrApi.GetRootFolders(settings.ApiKey, settings.FullUri); + + // set the cache + if (rootFolders != null) + { + Cache.Set(CacheKeys.SonarrRootFolders, rootFolders); + } + + return Response.AsJson(rootFolders); + } private Negotiator EmailNotifications() { diff --git a/PlexRequests.UI/Modules/ApprovalModule.cs b/PlexRequests.UI/Modules/ApprovalModule.cs index 478f8424c..f87c20b21 100644 --- a/PlexRequests.UI/Modules/ApprovalModule.cs +++ b/PlexRequests.UI/Modules/ApprovalModule.cs @@ -64,7 +64,7 @@ public ApprovalModule(IRequestService service, ISettingsService await Approve((int)Request.Form.requestid, (string)Request.Form.qualityId); + Post["/approve", true] = async (x, ct) => await Approve((int)Request.Form.requestid, (string)Request.Form.qualityId, (string)Request.Form.rootFolderId); Post["/deny", true] = async (x, ct) => await DenyRequest((int)Request.Form.requestid, (string)Request.Form.reason); Post["/approveall", true] = async (x, ct) => await ApproveAll(); Post["/approveallmovies", true] = async (x, ct) => await ApproveAllMovies(); @@ -91,7 +91,7 @@ public ApprovalModule(IRequestService service, ISettingsService /// The request identifier. /// - private async Task Approve(int requestId, string qualityId) + private async Task Approve(int requestId, string qualityId, string rootFolderId) { Log.Info("approving request {0}", requestId); @@ -109,7 +109,7 @@ private async Task Approve(int requestId, string qualityId) case RequestType.Movie: return await RequestMovieAndUpdateStatus(request, qualityId); case RequestType.TvShow: - return await RequestTvAndUpdateStatus(request, qualityId); + return await RequestTvAndUpdateStatus(request, qualityId, rootFolderId); case RequestType.Album: return await RequestAlbumAndUpdateStatus(request); default: @@ -117,7 +117,7 @@ private async Task Approve(int requestId, string qualityId) } } - private async Task RequestTvAndUpdateStatus(RequestedModel request, string qualityId) + private async Task RequestTvAndUpdateStatus(RequestedModel request, string qualityId, string rootFolderId) { var sender = new TvSender(SonarrApi, SickRageApi); @@ -125,7 +125,7 @@ private async Task RequestTvAndUpdateStatus(RequestedModel request, st if (sonarrSettings.Enabled) { Log.Trace("Sending to Sonarr"); - var result = await sender.SendToSonarr(sonarrSettings, request, qualityId); + var result = await sender.SendToSonarr(sonarrSettings, request, qualityId, rootFolderId); Log.Trace("Sonarr Result: "); Log.Trace(result.DumpJson()); if (!string.IsNullOrEmpty(result.title)) diff --git a/PlexRequests.UI/Modules/RequestsBetaModule.cs b/PlexRequests.UI/Modules/RequestsBetaModule.cs index a8a1a6e3f..4b94272d6 100644 --- a/PlexRequests.UI/Modules/RequestsBetaModule.cs +++ b/PlexRequests.UI/Modules/RequestsBetaModule.cs @@ -186,6 +186,37 @@ private async Task GetTvShows() } + IEnumerable rootFolders = new List(); + if (IsAdmin) + { + try + { + var sonarrSettings = await SonarrSettings.GetSettingsAsync(); + if (sonarrSettings.Enabled) + { + var result = Cache.GetOrSetAsync(CacheKeys.SonarrRootFolders, async () => + { + return await Task.Run(() => SonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri)); + }); + rootFolders = result.Result.Select(x => new RootFolderModel { Id = x.id.ToString(), Path = x.path }).ToList(); + } + // @TODO Sick Rage Root Folders + //else + //{ + // var sickRageSettings = await SickRageSettings.GetSettingsAsync(); + // if (sickRageSettings.Enabled) + // { + // qualities = sickRageSettings.Qualities.Select(x => new QualityModel { Id = x.Key, Name = x.Value }).ToList(); + // } + //} + } + catch (Exception e) + { + Log.Info(e); + } + + } + var viewModel = dbTv.Select(tv => new RequestViewModel { ProviderId = tv.ProviderId, @@ -209,6 +240,7 @@ private async Task GetTvShows() IssueId = tv.IssueId, TvSeriesRequestType = tv.SeasonsRequested, Qualities = qualities.ToArray(), + RootFolders = rootFolders.ToArray(), Episodes = tv.Episodes.ToArray(), }).ToList(); diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index dc46b2473..610d16372 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -230,6 +230,38 @@ private async Task GetTvShows() } + + IEnumerable rootFolders = new List(); + if (IsAdmin) + { + try + { + var sonarrSettings = await SonarrSettings.GetSettingsAsync(); + if (sonarrSettings.Enabled) + { + var result = Cache.GetOrSetAsync(CacheKeys.SonarrRootFolders, async () => + { + return await Task.Run(() => SonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri)); + }); + rootFolders = result.Result.Select(x => new RootFolderModel { Id = x.id.ToString(), Path = x.path }).ToList(); + } + // @TODO Sick Rage Root Folders + //else + //{ + // var sickRageSettings = await SickRageSettings.GetSettingsAsync(); + // if (sickRageSettings.Enabled) + // { + // qualities = sickRageSettings.Qualities.Select(x => new QualityModel { Id = x.Key, Name = x.Value }).ToList(); + // } + //} + } + catch (Exception e) + { + Log.Info(e); + } + + } + var viewModel = dbTv.Select(tv => new RequestViewModel { ProviderId = tv.ProviderId, @@ -255,6 +287,7 @@ private async Task GetTvShows() DeniedReason = tv.DeniedReason, TvSeriesRequestType = tv.SeasonsRequested, Qualities = qualities.ToArray(), + RootFolders = rootFolders.ToArray(), Episodes = tv.Episodes.ToArray(), }).ToList(); diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 1779f378b..69cc74b70 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -233,6 +233,7 @@ + diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index d71a3976c..80c3a8409 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -75,6 +75,7 @@ static void Main(string[] args) var s = new Setup(); var cn = s.SetupDb(baseUrl); s.CacheQualityProfiles(); + s.CacheRootFolders(); ConfigureTargets(cn); SetupLogging(); diff --git a/PlexRequests.UI/Validators/SonarrValidator.cs b/PlexRequests.UI/Validators/SonarrValidator.cs index b252bd3b0..faf3e6448 100644 --- a/PlexRequests.UI/Validators/SonarrValidator.cs +++ b/PlexRequests.UI/Validators/SonarrValidator.cs @@ -38,6 +38,7 @@ public SonarrValidator() RuleFor(request => request.Ip).NotEmpty().WithMessage("You must specify a IP/Host name."); RuleFor(request => request.Port).NotEmpty().WithMessage("You must specify a Port."); RuleFor(request => request.QualityProfile).NotEmpty().WithMessage("You must specify a Quality Profile."); + RuleFor(request => request.RootFolder).NotEmpty().WithMessage("You must specify a Root Folder."); } } } \ No newline at end of file diff --git a/PlexRequests.UI/Views/Admin/Sonarr.cshtml b/PlexRequests.UI/Views/Admin/Sonarr.cshtml index 388f359e6..fa139771e 100644 --- a/PlexRequests.UI/Views/Admin/Sonarr.cshtml +++ b/PlexRequests.UI/Views/Admin/Sonarr.cshtml @@ -80,12 +80,24 @@
+
+ +
+
+
+ +
+ +
+
+ + @*
-
+ *@
@@ -126,12 +138,10 @@ @if (!string.IsNullOrEmpty(Model.QualityProfile)) { - - preLoad(); + console.log('Hit profiles..'); - function preLoad() { var qualitySelected = @Model.QualityProfile; if (!qualitySelected) { return; @@ -145,7 +155,6 @@ success: function(response) { response.forEach(function(result) { if (result.id == qualitySelected) { - $("#select").append(""); } else { $("#select").append(""); @@ -157,11 +166,44 @@ generateNotify("Something went wrong!", "danger"); } }); + } - - } + + @if (!string.IsNullOrEmpty(Model.RootFolder)) + { + + + console.log('Hit root folders..'); + + var rootFolderSelected = @Model.RootFolder; + if (!rootFolderSelected) { + return; + } + var $form = $("#mainForm"); + $.ajax({ + type: $form.prop("method"), + data: $form.serialize(), + url: "sonarrrootfolders", + dataType: "json", + success: function(response) { + response.forEach(function(result) { + if (result.id == rootFolderSelected) { + $("#selectRootFolder").append(""); + } else { + $("#selectRootFolder").append(""); + } + }); + }, + error: function(e) { + console.log(e); + generateNotify("Something went wrong!", "danger"); + } + }); + + } + $('#save').click(function(e) { e.preventDefault(); var port = $('#portNumber').val(); @@ -201,17 +243,17 @@ e.preventDefault(); if (!$('#Ip').val()) { generateNotify("Please enter a valid IP/Hostname.", "warning"); - $('#getSpinner').attr("class", "fa fa-times"); + $('#getSpinner').attr("class", "fa fa-times"); return; } if (!$('#portNumber').val()) { generateNotify("Please enter a valid Port Number.", "warning"); - $('#getSpinner').attr("class", "fa fa-times"); + $('#getSpinner').attr("class", "fa fa-times"); return; } if (!$('#ApiKey').val()) { generateNotify("Please enter a valid ApiKey.", "warning"); - $('#getSpinner').attr("class", "fa fa-times"); + $('#getSpinner').attr("class", "fa fa-times"); return; } var $form = $("#mainForm"); @@ -222,13 +264,52 @@ dataType: "json", success: function (response) { response.forEach(function (result) { - $('#getSpinner').attr("class", "fa fa-check"); + $('#getSpinner').attr("class", "fa fa-check"); $("#select").append(""); }); }, error: function (e) { console.log(e); - $('#getSpinner').attr("class", "fa fa-times"); + $('#getSpinner').attr("class", "fa fa-times"); + generateNotify("Something went wrong!", "danger"); + } + }); + }); + + $('#getRootFolders').click(function (e) { + + $('#getRootFolderSpinner').attr("class", "fa fa-spinner fa-spin"); + e.preventDefault(); + if (!$('#Ip').val()) { + generateNotify("Please enter a valid IP/Hostname.", "warning"); + $('#getRootFolderSpinner').attr("class", "fa fa-times"); + return; + } + if (!$('#portNumber').val()) { + generateNotify("Please enter a valid Port Number.", "warning"); + $('#getRootFolderSpinner').attr("class", "fa fa-times"); + return; + } + if (!$('#ApiKey').val()) { + generateNotify("Please enter a valid ApiKey.", "warning"); + $('#getRootFolderSpinner').attr("class", "fa fa-times"); + return; + } + var $form = $("#mainForm"); + $.ajax({ + type: $form.prop("method"), + data: $form.serialize(), + url: "sonarrrootfolders", + dataType: "json", + success: function (response) { + response.forEach(function (result) { + $('#getRootFolderSpinner').attr("class", "fa fa-check"); + $("#selectRootFolder").append(""); + }); + }, + error: function (e) { + console.log(e); + $('#getRootFolderSpinner').attr("class", "fa fa-times"); generateNotify("Something went wrong!", "danger"); } }); @@ -245,7 +326,7 @@ var data = $form.serialize(); data = data + "&qualityProfile=" + qualityProfile; - + var url = createBaseUrl(base, '/test/sonarr'); $.ajax({ type: $form.prop("method"), @@ -256,16 +337,16 @@ console.log(response); if (response.result === true) { generateNotify(response.message, "success"); - $('#spinner').attr("class", "fa fa-check"); + $('#spinner').attr("class", "fa fa-check"); } else { generateNotify(response.message, "warning"); - $('#spinner').attr("class", "fa fa-times"); + $('#spinner').attr("class", "fa fa-times"); } }, error: function (e) { console.log(e); generateNotify("Something went wrong!", "danger"); - $('#spinner').attr("class", "fa fa-times"); + $('#spinner').attr("class", "fa fa-times"); } }); });