From ac1cc942550de9fabec4867f766419e10111def0 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 18 Feb 2017 00:00:59 +0000 Subject: [PATCH] Fixed a bug when sending to radarr --- Ombi.Core.Tests/MovieSenderTests.cs | 16 +++++++++++++++- Ombi.Core/MovieSender.cs | 23 ++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Ombi.Core.Tests/MovieSenderTests.cs b/Ombi.Core.Tests/MovieSenderTests.cs index 9fa468ac3..8c4e85e39 100644 --- a/Ombi.Core.Tests/MovieSenderTests.cs +++ b/Ombi.Core.Tests/MovieSenderTests.cs @@ -26,14 +26,19 @@ #endregion using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using Moq; using NUnit.Framework; using Ombi.Api; using Ombi.Api.Interfaces; using Ombi.Api.Models.Radarr; +using Ombi.Api.Models.Sonarr; using Ombi.Api.Models.Watcher; using Ombi.Core.SettingModels; +using Ombi.Helpers; using Ombi.Store; using Ploeh.AutoFixture; @@ -48,6 +53,7 @@ namespace Ombi.Core.Tests private Mock CpApiMock { get; set; } private Mock WatcherApiMock { get; set; } private Mock RadarrApiMock { get; set; } + private Mock CacheMock { get; set; } private Fixture F { get; set; } @@ -61,6 +67,8 @@ namespace Ombi.Core.Tests RadarrMock = new Mock>(); CpApiMock = new Mock(); WatcherApiMock = new Mock(); + CacheMock = new Mock(); + RadarrMock.Setup(x => x.GetSettingsAsync()) .ReturnsAsync(F.Build().With(x => x.Enabled, false).Create()); @@ -69,7 +77,7 @@ namespace Ombi.Core.Tests CpMock.Setup(x => x.GetSettingsAsync()) .ReturnsAsync(F.Build().With(x => x.Enabled, false).Create()); - Sender = new MovieSender(CpMock.Object, WatcherMock.Object, CpApiMock.Object, WatcherApiMock.Object, RadarrApiMock.Object, RadarrMock.Object); + Sender = new MovieSender(CpMock.Object, WatcherMock.Object, CpApiMock.Object, WatcherApiMock.Object, RadarrApiMock.Object, RadarrMock.Object, CacheMock.Object); } [Test] @@ -80,6 +88,9 @@ namespace Ombi.Core.Tests RadarrApiMock.Setup(x => x.AddMovie(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(new RadarrAddMovie { title = "Abc" }); + CacheMock.Setup(x => x.GetOrSet>(CacheKeys.RadarrRootFolders, It.IsAny>>(), It.IsAny())) + .Returns(F.CreateMany().ToList()); + var model = F.Create(); var result = await Sender.Send(model, 2.ToString()); @@ -101,6 +112,9 @@ namespace Ombi.Core.Tests RadarrApiMock.Setup(x => x.AddMovie(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(new RadarrAddMovie { Error = new RadarrError{message = "Movie Already Added"}}); + CacheMock.Setup(x => x.GetOrSet>(CacheKeys.RadarrRootFolders, It.IsAny>>(), It.IsAny())) + .Returns(F.CreateMany().ToList()); + var model = F.Create(); var result = await Sender.Send(model, 2.ToString()); diff --git a/Ombi.Core/MovieSender.cs b/Ombi.Core/MovieSender.cs index 0240a19ce..c7636ba83 100644 --- a/Ombi.Core/MovieSender.cs +++ b/Ombi.Core/MovieSender.cs @@ -26,10 +26,12 @@ #endregion using System; +using System.Linq; using System.Threading.Tasks; using NLog; using Ombi.Api.Interfaces; using Ombi.Core.SettingModels; +using Ombi.Helpers; using Ombi.Store; namespace Ombi.Core @@ -37,7 +39,8 @@ namespace Ombi.Core public class MovieSender : IMovieSender { public MovieSender(ISettingsService cp, ISettingsService watcher, - ICouchPotatoApi cpApi, IWatcherApi watcherApi, IRadarrApi radarrApi, ISettingsService radarrSettings) + ICouchPotatoApi cpApi, IWatcherApi watcherApi, IRadarrApi radarrApi, ISettingsService radarrSettings, + ICacheProvider cache) { CouchPotatoSettings = cp; WatcherSettings = watcher; @@ -45,6 +48,7 @@ namespace Ombi.Core WatcherApi = watcherApi; RadarrSettings = radarrSettings; RadarrApi = radarrApi; + Cache = cache; } private ISettingsService CouchPotatoSettings { get; } @@ -53,6 +57,7 @@ namespace Ombi.Core private IRadarrApi RadarrApi { get; } private ICouchPotatoApi CpApi { get; } private IWatcherApi WatcherApi { get; } + private ICacheProvider Cache { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); public async Task Send(RequestedModel model, string qualityId = "") @@ -114,8 +119,9 @@ namespace Ombi.Core { int.TryParse(settings.QualityProfile, out qualityProfile); } - - var result = RadarrApi.AddMovie(model.ProviderId, model.Title, model.ReleaseDate.Year, qualityProfile, settings.RootPath, settings.ApiKey, settings.FullUri, true); + + var rootFolderPath = model.RootFolderSelected <= 0 ? settings.FullRootPath : GetRootPath(model.RootFolderSelected, settings); + var result = RadarrApi.AddMovie(model.ProviderId, model.Title, model.ReleaseDate.Year, qualityProfile, rootFolderPath, settings.ApiKey, settings.FullUri, true); if (!string.IsNullOrEmpty(result.Error?.message)) { @@ -128,5 +134,16 @@ namespace Ombi.Core } return new MovieSenderResult { Result = false, MovieSendingEnabled = true }; } + + private string GetRootPath(int pathId, RadarrSettings sonarrSettings) + { + var rootFoldersResult = Cache.GetOrSet(CacheKeys.RadarrRootFolders, () => RadarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri)); + + foreach (var r in rootFoldersResult.Where(r => r.id == pathId)) + { + return r.path; + } + return string.Empty; + } } } \ No newline at end of file