From 55c62772e40de6e01865e4b70b53628d3094b67f Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 21 Oct 2017 22:12:38 +0100 Subject: [PATCH] Fixed the issue where we did not check if they are already in sonarr when choosing certain options #1540 --- .../Rules/Request/SonarrCacheRequestRule.cs | 23 +++++++++ .../Rules/Search/SonarrCacheSearchRule.cs | 50 +++++++++++++++++++ .../Rules/{Search => }/SonarrCacheRule.cs | 39 +++++++++++++-- 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 src/Ombi.Core/Rule/Rules/Request/SonarrCacheRequestRule.cs create mode 100644 src/Ombi.Core/Rule/Rules/Search/SonarrCacheSearchRule.cs rename src/Ombi.Core/Rule/Rules/{Search => }/SonarrCacheRule.cs (51%) diff --git a/src/Ombi.Core/Rule/Rules/Request/SonarrCacheRequestRule.cs b/src/Ombi.Core/Rule/Rules/Request/SonarrCacheRequestRule.cs new file mode 100644 index 000000000..65750a64b --- /dev/null +++ b/src/Ombi.Core/Rule/Rules/Request/SonarrCacheRequestRule.cs @@ -0,0 +1,23 @@ +using System.Threading.Tasks; +using Ombi.Core.Rule.Interfaces; +using Ombi.Store.Context; +using Ombi.Store.Entities.Requests; + +namespace Ombi.Core.Rule.Rules.Request +{ + public class SonarrCacheRequestRule : BaseRequestRule, IRules + { + public SonarrCacheRequestRule(IOmbiContext ctx) + { + _ctx = ctx; + } + + private readonly IOmbiContext _ctx; + + public Task Execute(BaseRequest obj) + { + var rule = new SonarrCacheRule(_ctx); + return rule.Execute(obj); + } + } +} \ No newline at end of file diff --git a/src/Ombi.Core/Rule/Rules/Search/SonarrCacheSearchRule.cs b/src/Ombi.Core/Rule/Rules/Search/SonarrCacheSearchRule.cs new file mode 100644 index 000000000..f9c5cd09d --- /dev/null +++ b/src/Ombi.Core/Rule/Rules/Search/SonarrCacheSearchRule.cs @@ -0,0 +1,50 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: SonarrCacheSearchRule.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.Threading.Tasks; +using Ombi.Core.Models.Search; +using Ombi.Core.Rule.Interfaces; +using Ombi.Store.Context; + +namespace Ombi.Core.Rule.Rules.Search +{ + public class SonarrCacheSearchRule : BaseSearchRule, IRules + { + public SonarrCacheSearchRule(IOmbiContext ctx) + { + _ctx = ctx; + } + + private readonly IOmbiContext _ctx; + + public Task Execute(SearchViewModel obj) + { + var rule = new SonarrCacheRule(_ctx); + return rule.Execute(obj); + } + } +} \ No newline at end of file diff --git a/src/Ombi.Core/Rule/Rules/Search/SonarrCacheRule.cs b/src/Ombi.Core/Rule/Rules/SonarrCacheRule.cs similarity index 51% rename from src/Ombi.Core/Rule/Rules/Search/SonarrCacheRule.cs rename to src/Ombi.Core/Rule/Rules/SonarrCacheRule.cs index 562b550fe..5dd2e8b54 100644 --- a/src/Ombi.Core/Rule/Rules/Search/SonarrCacheRule.cs +++ b/src/Ombi.Core/Rule/Rules/SonarrCacheRule.cs @@ -2,13 +2,13 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Ombi.Core.Models.Search; -using Ombi.Core.Rule.Interfaces; using Ombi.Store.Context; using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; -namespace Ombi.Core.Rule.Rules.Search +namespace Ombi.Core.Rule.Rules { - public class SonarrCacheRule : BaseSearchRule, IRules + public class SonarrCacheRule { public SonarrCacheRule(IOmbiContext ctx) { @@ -17,6 +17,37 @@ namespace Ombi.Core.Rule.Rules.Search private readonly IOmbiContext _ctx; + public async Task Execute(BaseRequest obj) + { + if (obj.RequestType == RequestType.TvShow) + { + var vm = (ChildRequests) obj; + var result = await _ctx.SonarrCache.FirstOrDefaultAsync(x => x.TvDbId == vm.Id); + if (result != null) + { + if (vm.SeasonRequests.Any()) + { + var sonarrEpisodes = _ctx.SonarrEpisodeCache; + foreach (var season in vm.SeasonRequests) + { + foreach (var ep in season.Episodes) + { + // Check if we have it + var monitoredInSonarr = sonarrEpisodes.Any(x => + x.EpisodeNumber == ep.EpisodeNumber && x.SeasonNumber == season.SeasonNumber + && x.TvDbId == vm.Id); + if (monitoredInSonarr) + { + return new RuleResult{Message = "We already have this request, please choose the \"Select...\" option to refine your request"}; + } + } + } + } + } + } + return new RuleResult { Success = true }; + } + public async Task Execute(SearchViewModel obj) { if (obj.Type == RequestType.TvShow) @@ -48,7 +79,7 @@ namespace Ombi.Core.Rule.Rules.Search } } } - return Success(); + return new RuleResult { Success = true }; } } } \ No newline at end of file