Fixed a bug when sending to radarr

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

@ -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<ICouchPotatoApi> CpApiMock { get; set; }
private Mock<IWatcherApi> WatcherApiMock { get; set; }
private Mock<IRadarrApi> RadarrApiMock { get; set; }
private Mock<ICacheProvider> CacheMock { get; set; }
private Fixture F { get; set; }
@ -61,6 +67,8 @@ namespace Ombi.Core.Tests
RadarrMock = new Mock<ISettingsService<RadarrSettings>>();
CpApiMock = new Mock<ICouchPotatoApi>();
WatcherApiMock = new Mock<IWatcherApi>();
CacheMock = new Mock<ICacheProvider>();
RadarrMock.Setup(x => x.GetSettingsAsync())
.ReturnsAsync(F.Build<RadarrSettings>().With(x => x.Enabled, false).Create());
@ -69,7 +77,7 @@ namespace Ombi.Core.Tests
CpMock.Setup(x => x.GetSettingsAsync())
.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]
@ -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>(),
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 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>(),
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 result = await Sender.Send(model, 2.ToString());

@ -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<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;
WatcherSettings = watcher;
@ -45,6 +48,7 @@ namespace Ombi.Core
WatcherApi = watcherApi;
RadarrSettings = radarrSettings;
RadarrApi = radarrApi;
Cache = cache;
}
private ISettingsService<CouchPotatoSettings> 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<MovieSenderResult> 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;
}
}
}
Loading…
Cancel
Save