Fixed a bug when sending to radarr

pull/1141/head
tidusjar 8 years ago
parent a93c18bc04
commit ac1cc94255

@ -26,14 +26,19 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using Ombi.Api; using Ombi.Api;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Api.Models.Radarr; using Ombi.Api.Models.Radarr;
using Ombi.Api.Models.Sonarr;
using Ombi.Api.Models.Watcher; using Ombi.Api.Models.Watcher;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store; using Ombi.Store;
using Ploeh.AutoFixture; using Ploeh.AutoFixture;
@ -48,6 +53,7 @@ namespace Ombi.Core.Tests
private Mock<ICouchPotatoApi> CpApiMock { get; set; } private Mock<ICouchPotatoApi> CpApiMock { get; set; }
private Mock<IWatcherApi> WatcherApiMock { get; set; } private Mock<IWatcherApi> WatcherApiMock { get; set; }
private Mock<IRadarrApi> RadarrApiMock { get; set; } private Mock<IRadarrApi> RadarrApiMock { get; set; }
private Mock<ICacheProvider> CacheMock { get; set; }
private Fixture F { get; set; } private Fixture F { get; set; }
@ -61,6 +67,8 @@ namespace Ombi.Core.Tests
RadarrMock = new Mock<ISettingsService<RadarrSettings>>(); RadarrMock = new Mock<ISettingsService<RadarrSettings>>();
CpApiMock = new Mock<ICouchPotatoApi>(); CpApiMock = new Mock<ICouchPotatoApi>();
WatcherApiMock = new Mock<IWatcherApi>(); WatcherApiMock = new Mock<IWatcherApi>();
CacheMock = new Mock<ICacheProvider>();
RadarrMock.Setup(x => x.GetSettingsAsync()) RadarrMock.Setup(x => x.GetSettingsAsync())
.ReturnsAsync(F.Build<RadarrSettings>().With(x => x.Enabled, false).Create()); .ReturnsAsync(F.Build<RadarrSettings>().With(x => x.Enabled, false).Create());
@ -69,7 +77,7 @@ namespace Ombi.Core.Tests
CpMock.Setup(x => x.GetSettingsAsync()) CpMock.Setup(x => x.GetSettingsAsync())
.ReturnsAsync(F.Build<CouchPotatoSettings>().With(x => x.Enabled, false).Create()); .ReturnsAsync(F.Build<CouchPotatoSettings>().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] [Test]
@ -80,6 +88,9 @@ namespace Ombi.Core.Tests
RadarrApiMock.Setup(x => x.AddMovie(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), RadarrApiMock.Setup(x => x.AddMovie(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Uri>(), It.IsAny<bool>())).Returns(new RadarrAddMovie { title = "Abc" }); It.IsAny<Uri>(), It.IsAny<bool>())).Returns(new RadarrAddMovie { title = "Abc" });
CacheMock.Setup(x => x.GetOrSet<List<SonarrRootFolder>>(CacheKeys.RadarrRootFolders, It.IsAny<Func<List<SonarrRootFolder>>>(), It.IsAny<int>()))
.Returns(F.CreateMany<SonarrRootFolder>().ToList());
var model = F.Create<RequestedModel>(); var model = F.Create<RequestedModel>();
var result = await Sender.Send(model, 2.ToString()); var result = await Sender.Send(model, 2.ToString());
@ -101,6 +112,9 @@ namespace Ombi.Core.Tests
RadarrApiMock.Setup(x => x.AddMovie(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), RadarrApiMock.Setup(x => x.AddMovie(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Uri>(), It.IsAny<bool>())).Returns(new RadarrAddMovie { Error = new RadarrError{message = "Movie Already Added"}}); It.IsAny<Uri>(), It.IsAny<bool>())).Returns(new RadarrAddMovie { Error = new RadarrError{message = "Movie Already Added"}});
CacheMock.Setup(x => x.GetOrSet<List<SonarrRootFolder>>(CacheKeys.RadarrRootFolders, It.IsAny<Func<List<SonarrRootFolder>>>(), It.IsAny<int>()))
.Returns(F.CreateMany<SonarrRootFolder>().ToList());
var model = F.Create<RequestedModel>(); var model = F.Create<RequestedModel>();
var result = await Sender.Send(model, 2.ToString()); var result = await Sender.Send(model, 2.ToString());

@ -26,10 +26,12 @@
#endregion #endregion
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store; using Ombi.Store;
namespace Ombi.Core namespace Ombi.Core
@ -37,7 +39,8 @@ namespace Ombi.Core
public class MovieSender : IMovieSender public class MovieSender : IMovieSender
{ {
public MovieSender(ISettingsService<CouchPotatoSettings> cp, ISettingsService<WatcherSettings> watcher, public MovieSender(ISettingsService<CouchPotatoSettings> cp, ISettingsService<WatcherSettings> watcher,
ICouchPotatoApi cpApi, IWatcherApi watcherApi, IRadarrApi radarrApi, ISettingsService<RadarrSettings> radarrSettings) ICouchPotatoApi cpApi, IWatcherApi watcherApi, IRadarrApi radarrApi, ISettingsService<RadarrSettings> radarrSettings,
ICacheProvider cache)
{ {
CouchPotatoSettings = cp; CouchPotatoSettings = cp;
WatcherSettings = watcher; WatcherSettings = watcher;
@ -45,6 +48,7 @@ namespace Ombi.Core
WatcherApi = watcherApi; WatcherApi = watcherApi;
RadarrSettings = radarrSettings; RadarrSettings = radarrSettings;
RadarrApi = radarrApi; RadarrApi = radarrApi;
Cache = cache;
} }
private ISettingsService<CouchPotatoSettings> CouchPotatoSettings { get; } private ISettingsService<CouchPotatoSettings> CouchPotatoSettings { get; }
@ -53,6 +57,7 @@ namespace Ombi.Core
private IRadarrApi RadarrApi { get; } private IRadarrApi RadarrApi { get; }
private ICouchPotatoApi CpApi { get; } private ICouchPotatoApi CpApi { get; }
private IWatcherApi WatcherApi { get; } private IWatcherApi WatcherApi { get; }
private ICacheProvider Cache { get; }
private static Logger Log = LogManager.GetCurrentClassLogger(); private static Logger Log = LogManager.GetCurrentClassLogger();
public async Task<MovieSenderResult> Send(RequestedModel model, string qualityId = "") public async Task<MovieSenderResult> Send(RequestedModel model, string qualityId = "")
@ -115,7 +120,8 @@ namespace Ombi.Core
int.TryParse(settings.QualityProfile, out qualityProfile); 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)) if (!string.IsNullOrEmpty(result.Error?.message))
{ {
@ -128,5 +134,16 @@ namespace Ombi.Core
} }
return new MovieSenderResult { Result = false, MovieSendingEnabled = true }; 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;
}
} }
} }
Loading…
Cancel
Save