From 866cec096d397e6dc2fac862f90bf69d57caad29 Mon Sep 17 00:00:00 2001 From: geogolem Date: Thu, 9 Mar 2017 11:08:38 -0500 Subject: [PATCH] refactor so that filteringExpressions are constructed in one place less code duplication, easier to manage moving forward --- src/NzbDrone.Api/Series/MovieModule.cs | 51 +-------------- src/NzbDrone.Core/Tv/MovieService.cs | 63 +++++++++++++------ src/UI/Movies/MoviesCollection.js | 14 ++--- src/UI/Wanted/Cutoff/CutoffUnmetCollection.js | 8 +-- src/UI/Wanted/Missing/MissingCollection.js | 6 +- 5 files changed, 59 insertions(+), 83 deletions(-) diff --git a/src/NzbDrone.Api/Series/MovieModule.cs b/src/NzbDrone.Api/Series/MovieModule.cs index 8c3fd3077..c0bf8811e 100644 --- a/src/NzbDrone.Api/Series/MovieModule.cs +++ b/src/NzbDrone.Api/Series/MovieModule.cs @@ -116,56 +116,9 @@ namespace NzbDrone.Api.Movie { var pagingSpec = pagingResource.MapToPagingSpec(); - if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false") - { - pagingSpec.FilterExpression = v => v.Monitored == false; - } - else if (pagingResource.FilterKey == "monitored") - { - pagingSpec.FilterExpression = v => v.Monitored == true; - } - - if (pagingResource.FilterKey == "status") - { - switch (pagingResource.FilterValue) - { - case "released": - pagingSpec.FilterExpression = v => v.Status == MovieStatusType.Released; - break; - case "inCinemas": - pagingSpec.FilterExpression = v => v.Status == MovieStatusType.InCinemas; - break; - case "announced": - pagingSpec.FilterExpression = v => v.Status == MovieStatusType.Announced; - break; - } - } - - if (pagingResource.FilterKey == "downloaded") - { - pagingSpec.FilterExpression = v => v.MovieFileId == 0; - } - - if (pagingResource.FilterKey == "title") - { - if (pagingResource.FilterValue == string.Empty || pagingResource.FilterValue == null) - { - pagingSpec.FilterExpression = v => true; - } - else - { - if (pagingResource.FilterType == "contains") - { - pagingSpec.FilterExpression = v => v.CleanTitle.Contains(pagingResource.FilterValue); - } - else - { - pagingSpec.FilterExpression = v => v.CleanTitle == pagingResource.FilterValue; - } - } - } + pagingSpec.FilterExpression = _moviesService.ConstructFilterExpression(pagingResource.FilterKey, pagingResource.FilterValue, pagingResource.FilterType); - return ApplyToPage(_moviesService.Paged, pagingSpec, MovieResourceMapper.ToResource); + return ApplyToPage(_moviesService.Paged, pagingSpec, MovieResourceMapper.ToResource); } protected MovieResource MapToResource(Core.Tv.Movie movies) diff --git a/src/NzbDrone.Core/Tv/MovieService.cs b/src/NzbDrone.Core/Tv/MovieService.cs index d535a1dc4..cae86a968 100644 --- a/src/NzbDrone.Core/Tv/MovieService.cs +++ b/src/NzbDrone.Core/Tv/MovieService.cs @@ -41,7 +41,7 @@ namespace NzbDrone.Core.Tv bool MoviePathExists(string folder); void RemoveAddOptions(Movie movie); List MoviesWithFiles(int movieId); - System.Linq.Expressions.Expression> ConstructFilterExpression(string FilterKey, string FilterValue); + System.Linq.Expressions.Expression> ConstructFilterExpression(string FilterKey, string FilterValue, string filterType = null); } public class MovieService : IMovieService, IHandle, @@ -69,42 +69,65 @@ namespace NzbDrone.Core.Tv } - public System.Linq.Expressions.Expression> ConstructFilterExpression(string FilterKey, string FilterValue) + public System.Linq.Expressions.Expression> ConstructFilterExpression(string FilterKey, string FilterValue, string FilterType = null) { - if (FilterKey == "all" && FilterValue == "all") - { - return v => v.Monitored == true || v.Monitored == false; - } - else if (FilterKey == "monitored" && FilterValue == "false") + //if (FilterKey == "all" && FilterValue == "all") + //{ + // return v => v.Monitored == true || v.Monitored == false; + //} + if (FilterKey == "moviemonitored" && FilterValue == "false") { return v => v.Monitored == false; } - else if (FilterKey == "monitored" && FilterValue == "true") + else if (FilterKey == "moviemonitored" && FilterValue == "true") { return v => v.Monitored == true; } - else if (FilterKey == "moviestatus" && FilterValue == "available") + else if (FilterKey == "moviestatus") { - //TODO: might need to handle PreDB here - return v => v.Monitored == true && + switch (FilterValue) + { + case "released": + return v => v.Status == MovieStatusType.Released; + break; + case "inCinemas": + return v => v.Status == MovieStatusType.InCinemas; + break; + case "announced": + return v => v.Status == MovieStatusType.Announced; + break; + case "available": + return v => v.Monitored == true && ((v.MinimumAvailability == MovieStatusType.Released && v.Status >= MovieStatusType.Released) || (v.MinimumAvailability == MovieStatusType.InCinemas && v.Status >= MovieStatusType.InCinemas) || (v.MinimumAvailability == MovieStatusType.Announced && v.Status >= MovieStatusType.Announced) || (v.MinimumAvailability == MovieStatusType.PreDB && v.Status >= MovieStatusType.Released)); + break; + } } - else if (FilterKey == "moviestatus" && FilterValue == "announced") - { - return v => v.Status == MovieStatusType.Announced; - } - else if (FilterKey == "moviestatus" && FilterValue == "incinemas") + else if (FilterKey == "moviedownloaded") { - return v => v.Status == MovieStatusType.InCinemas; + return v => v.MovieFileId == 0; } - else if (FilterKey == "moviestatus" && FilterValue == "released") + else if (FilterKey == "title") { - return v => v.Status == MovieStatusType.Released; + if (FilterValue == string.Empty || FilterValue == null) + { + return v => true; + } + else + { + if (FilterType == "contains") + { + return v => v.CleanTitle.Contains(FilterValue); + } + else + { + return v => v.CleanTitle == FilterValue; + } + } } - return v => v.Monitored == true; + return v => true; } public Movie GetMovie(int movieId) diff --git a/src/UI/Movies/MoviesCollection.js b/src/UI/Movies/MoviesCollection.js index a4ec0fa87..36a99e3e0 100644 --- a/src/UI/Movies/MoviesCollection.js +++ b/src/UI/Movies/MoviesCollection.js @@ -19,33 +19,33 @@ var filterModes = { null ], 'continuing' : [ - 'status', + 'moviestatus', 'continuing' ], 'ended' : [ - 'status', + 'moviestatus', 'ended' ], 'monitored' : [ - 'monitored', + 'moviemonitored', true ], 'missing' : [ - 'downloaded', + 'moviedownloaded', false ], 'released' : [ - "status", + "moviestatus", "released", //function(model) { return model.getStatus() == "released"; } ], 'announced' : [ - "status", + "moviestatus", "announced", //function(model) { return model.getStatus() == "announced"; } ], 'cinemas' : [ - "status", + "moviestatus", "inCinemas", //function(model) { return model.getStatus() == "inCinemas"; } ] diff --git a/src/UI/Wanted/Cutoff/CutoffUnmetCollection.js b/src/UI/Wanted/Cutoff/CutoffUnmetCollection.js index d44254d17..107573d81 100644 --- a/src/UI/Wanted/Cutoff/CutoffUnmetCollection.js +++ b/src/UI/Wanted/Cutoff/CutoffUnmetCollection.js @@ -31,11 +31,11 @@ var Collection = PagableCollection.extend({ filterModes : { 'monitored' : [ - 'monitored', + 'moviemonitored', 'true' ], 'unmonitored' : [ - 'monitored', + 'moviemonitored', 'false' ], 'announced' : [ @@ -44,7 +44,7 @@ var Collection = PagableCollection.extend({ ], 'incinemas' : [ 'moviestatus', - 'incinemas' + 'inCinemas' ], 'released' : [ 'moviestatus', @@ -77,4 +77,4 @@ var Collection = PagableCollection.extend({ Collection = AsFilteredCollection.call(Collection); Collection = AsSortedCollection.call(Collection); -module.exports = AsPersistedStateCollection.call(Collection); \ No newline at end of file +module.exports = AsPersistedStateCollection.call(Collection); diff --git a/src/UI/Wanted/Missing/MissingCollection.js b/src/UI/Wanted/Missing/MissingCollection.js index 43ce5741c..2dc2449e0 100644 --- a/src/UI/Wanted/Missing/MissingCollection.js +++ b/src/UI/Wanted/Missing/MissingCollection.js @@ -30,11 +30,11 @@ var Collection = PagableCollection.extend({ filterModes : { 'monitored' : [ - 'monitored', + 'moviemonitored', 'true' ], 'unmonitored' : [ - 'monitored', + 'moviemonitored', 'false' ], 'announced' : [ @@ -43,7 +43,7 @@ var Collection = PagableCollection.extend({ ], 'incinemas' : [ 'moviestatus', - 'incinemas' + 'inCinemas' ], 'released' : [ 'moviestatus',