From 2e41763b92808d0bf304c36b8e2c62afe307cbb3 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Sat, 11 Mar 2017 17:03:03 +0100 Subject: [PATCH] Preliminary Fix for downloaded error in Wanted section. Ignores filters when sorting by downloaded. --- src/NzbDrone.Core/Tv/MovieRepository.cs | 48 +++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Tv/MovieRepository.cs b/src/NzbDrone.Core/Tv/MovieRepository.cs index 46e485504..f970f22d9 100644 --- a/src/NzbDrone.Core/Tv/MovieRepository.cs +++ b/src/NzbDrone.Core/Tv/MovieRepository.cs @@ -243,15 +243,42 @@ namespace NzbDrone.Core.Tv public PagingSpec MoviesWhereCutoffUnmet(PagingSpec pagingSpec, List qualitiesBelowCutoff) { + //I know this is bad, but if you have a better Idea please tell me. + if (pagingSpec.SortKey == "downloadedQuality") + { + var mapper = _database.GetDataMapper(); + var offset = pagingSpec.PagingOffset(); + var limit = pagingSpec.PageSize; + var direction = "ASC"; + if (pagingSpec.SortDirection == NzbDrone.Core.Datastore.SortDirection.Descending) + { + direction = "DESC"; + } + + var whereClause = BuildQualityCutoffWhereClauseSpecial(qualitiesBelowCutoff); + + var q = mapper.Query($"SELECT * from \"Movies\" , \"MovieFiles\", \"QualityDefinitions\" WHERE Movies.MovieFileId=MovieFiles.Id AND instr(MovieFiles.Quality, ('quality\": ' || QualityDefinitions.Quality || \",\")) > 0 AND {whereClause} ORDER BY QualityDefinitions.Title {direction} LIMIT {offset},{limit};"); + var q2 = mapper.Query($"SELECT * from \"Movies\" , \"MovieFiles\", \"QualityDefinitions\" WHERE Movies.MovieFileId=MovieFiles.Id AND instr(MovieFiles.Quality, ('quality\": ' || QualityDefinitions.Quality || \",\")) > 0 AND {whereClause} ORDER BY QualityDefinitions.Title ASC;"); + + //var ok = q.BuildQuery(); + + pagingSpec.Records = q.ToList(); + pagingSpec.TotalRecords = q2.Count(); - pagingSpec.TotalRecords = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).GetRowCount(); - pagingSpec.Records = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).ToList(); + } + else + { + + pagingSpec.TotalRecords = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).GetRowCount(); + pagingSpec.Records = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).ToList(); + + } return pagingSpec; } private SortBuilder MoviesWhereCutoffUnmetQuery(PagingSpec pagingSpec, List qualitiesBelowCutoff) - { + { return Query.Join(JoinType.Left, e => e.MovieFile, (e, s) => e.MovieFileId == s.Id) .Where(pagingSpec.FilterExpression) .AndWhere(m => m.MovieFileId != 0) @@ -276,6 +303,21 @@ namespace NzbDrone.Core.Tv return string.Format("({0})", string.Join(" OR ", clauses)); } + private string BuildQualityCutoffWhereClauseSpecial(List qualitiesBelowCutoff) + { + var clauses = new List(); + + foreach (var profile in qualitiesBelowCutoff) + { + foreach (var belowCutoff in profile.QualityIds) + { + clauses.Add(string.Format("(Movies.ProfileId = {0} AND MovieFiles.Quality LIKE '%_quality_: {1},%')", profile.ProfileId, belowCutoff)); + } + } + + return string.Format("({0})", string.Join(" OR ", clauses)); + } + public Movie FindByTmdbId(int tmdbid) { return Query.Where(m => m.TmdbId == tmdbid).FirstOrDefault();