From d320c2b6a9ca6f5f17d76c8716df4cfaf5f9c721 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 10 May 2013 15:33:04 -0700 Subject: [PATCH] Look ma, history! --- Marr.Data/QGen/QueryBuilder.cs | 12 +++++++ NzbDrone.Api/AutomapperBootstraper.cs | 4 +-- NzbDrone.Api/History/HistoryModule.cs | 41 ++-------------------- NzbDrone.Core/Datastore/TableMapping.cs | 8 +++-- NzbDrone.Core/History/History.cs | 7 ++-- NzbDrone.Core/History/HistoryRepository.cs | 18 +++++++++- NzbDrone.Core/History/HistoryService.cs | 7 ++++ NzbDrone.Core/Providers/XemProvider.cs | 4 --- UI/History/EpisodeTitleTemplate.html | 1 + UI/History/HistoryLayout.js | 6 ++-- UI/History/Model.js | 8 +++++ UI/Series/EpisodeModel.js | 2 +- 12 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 UI/History/EpisodeTitleTemplate.html diff --git a/Marr.Data/QGen/QueryBuilder.cs b/Marr.Data/QGen/QueryBuilder.cs index 45560ab17..6a99c4c19 100644 --- a/Marr.Data/QGen/QueryBuilder.cs +++ b/Marr.Data/QGen/QueryBuilder.cs @@ -389,12 +389,24 @@ namespace Marr.Data.QGen return SortBuilder; } + public virtual SortBuilder OrderBy(Expression> sortExpression, SortDirection sortDirection) + { + SortBuilder.OrderBy(sortExpression, sortDirection); + return SortBuilder; + } + public virtual SortBuilder ThenBy(Expression> sortExpression) { SortBuilder.OrderBy(sortExpression); return SortBuilder; } + public virtual SortBuilder ThenBy(Expression> sortExpression, SortDirection sortDirection) + { + SortBuilder.OrderBy(sortExpression, sortDirection); + return SortBuilder; + } + public virtual SortBuilder OrderByDescending(Expression> sortExpression) { SortBuilder.OrderByDescending(sortExpression); diff --git a/NzbDrone.Api/AutomapperBootstraper.cs b/NzbDrone.Api/AutomapperBootstraper.cs index e8d8ad1d7..7af24e420 100644 --- a/NzbDrone.Api/AutomapperBootstraper.cs +++ b/NzbDrone.Api/AutomapperBootstraper.cs @@ -45,9 +45,7 @@ namespace NzbDrone.Api Mapper.CreateMap, PagingResource>(); //History - Mapper.CreateMap() - .ForMember(dest => dest.Episode, opt => opt.Ignore()) - .ForMember(dest => dest.Series, opt => opt.Ignore()); + Mapper.CreateMap(); Mapper.CreateMap, PagingResource>(); } } diff --git a/NzbDrone.Api/History/HistoryModule.cs b/NzbDrone.Api/History/HistoryModule.cs index 1800f6b3a..b4fefdfcf 100644 --- a/NzbDrone.Api/History/HistoryModule.cs +++ b/NzbDrone.Api/History/HistoryModule.cs @@ -44,7 +44,7 @@ namespace NzbDrone.Api.History ? SortDirection.Ascending : SortDirection.Descending; - var pagingSpec = new PagingSpec + var pagingSpec = new PagingSpec { Page = page, PageSize = pageSize, @@ -52,44 +52,7 @@ namespace NzbDrone.Api.History SortDirection = sortDirection }; - var series = new Core.Tv.Series { Title = "30 Rock", TitleSlug = "30-rock" }; - var episode = new Episode { Title = "Test", SeasonNumber = 1, EpisodeNumber = 5 }; - - var result = new PagingSpec - { - Records = new List - { - new Core.History.History - { - Id = 1, - Date = DateTime.UtcNow.AddHours(-5), -// Episode = episode, -// Series = series, - Indexer = "nzbs.org", - Quality = new QualityModel(Quality.HDTV720p) - }, - new Core.History.History - { - Id = 2, - Date = DateTime.UtcNow.AddDays(-1), - // Episode = episode, - // Series = series, - Indexer = "nzbs.org", - Quality = new QualityModel(Quality.SDTV, true) - }, - new Core.History.History - { - Id = 3, - Date = DateTime.UtcNow.AddDays(-5), -// Episode = episode, -// Series = series, - Indexer = "nzbs.org", - Quality = new QualityModel(Quality.WEBDL1080p) - } - } - }; - - result.TotalRecords = result.Records.Count; + var result = _historyService.Paged(pagingSpec); return Mapper.Map, PagingResource>(result).AsResponse(); } diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index b29464675..5b19de082 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -39,9 +39,11 @@ namespace NzbDrone.Core.Datastore Mapper.Entity().RegisterModel("SceneMappings"); Mapper.Entity().RegisterModel("History") - .Relationship() - .HasOne(h => h.Episode, h => h.EpisodeId) - .HasOne(h => h.Series, h => h.SeriesId); + .Relationships + .AutoMapICollectionOrComplexProperties(); +// .Relationship(); +// .HasOne(h => h.Episode, h => h.EpisodeId) +// .HasOne(h => h.Series, h => h.SeriesId); Mapper.Entity().RegisterModel("Series") .Ignore(s => s.Path) diff --git a/NzbDrone.Core/History/History.cs b/NzbDrone.Core/History/History.cs index b11fa4f83..d60aeebc6 100644 --- a/NzbDrone.Core/History/History.cs +++ b/NzbDrone.Core/History/History.cs @@ -17,7 +17,10 @@ namespace NzbDrone.Core.History public string NzbInfoUrl { get; set; } public string ReleaseGroup { get; set; } - public LazyLoaded Episode { get; set; } - public LazyLoaded Series { get; set; } +// public LazyLoaded Episode { get; set; } +// public LazyLoaded Series { get; set; } + + public Episode Episode { get; set; } + public Series Series { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Core/History/HistoryRepository.cs b/NzbDrone.Core/History/HistoryRepository.cs index 29d121438..0068c1bb7 100644 --- a/NzbDrone.Core/History/HistoryRepository.cs +++ b/NzbDrone.Core/History/HistoryRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Linq; +using Marr.Data.QGen; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; using NzbDrone.Core.Tv; @@ -12,6 +13,7 @@ namespace NzbDrone.Core.History { void Trim(); QualityModel GetBestQualityInHistory(int episodeId); + PagingSpec Paged(PagingSpec pagingSpec); } public class HistoryRepository : BasicRepository, IHistoryRepository @@ -41,6 +43,20 @@ namespace NzbDrone.Core.History return null; } - //public List GetPagedHistory() + public PagingSpec Paged(PagingSpec pagingSpec) + { + var pagingQuery = Query.Join(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id) + .Join(JoinType.Inner, h => h.Episode, (h, e) => h.EpisodeId == e.Id) + .OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection()) + .Skip(pagingSpec.PagingOffset()) + .Take(pagingSpec.PageSize); + + pagingSpec.Records = pagingQuery.ToList(); + + //TODO: Use the same query for count and records + pagingSpec.TotalRecords = Count(); + + return pagingSpec; + } } } \ No newline at end of file diff --git a/NzbDrone.Core/History/HistoryService.cs b/NzbDrone.Core/History/HistoryService.cs index 6bfa50b63..b65a0704c 100644 --- a/NzbDrone.Core/History/HistoryService.cs +++ b/NzbDrone.Core/History/HistoryService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Messaging; +using NzbDrone.Core.Datastore; using NzbDrone.Core.Download; using NzbDrone.Core.Tv; @@ -14,6 +15,7 @@ namespace NzbDrone.Core.History void Purge(); void Trim(); QualityModel GetBestQualityInHistory(int episodeId); + PagingSpec Paged(PagingSpec pagingSpec); } public class HistoryService : IHistoryService, IHandle @@ -33,6 +35,11 @@ namespace NzbDrone.Core.History return _historyRepository.All().ToList(); } + public PagingSpec Paged(PagingSpec pagingSpec) + { + return _historyRepository.Paged(pagingSpec); + } + public void Purge() { _historyRepository.Purge(); diff --git a/NzbDrone.Core/Providers/XemProvider.cs b/NzbDrone.Core/Providers/XemProvider.cs index 6707ccd9b..5699d3cdb 100644 --- a/NzbDrone.Core/Providers/XemProvider.cs +++ b/NzbDrone.Core/Providers/XemProvider.cs @@ -22,10 +22,6 @@ namespace NzbDrone.Core.Providers _seriesRepository = seriesRepository; } - public XemProvider() - { - } - public virtual void UpdateMappings() { _logger.Trace("Starting scene numbering update"); diff --git a/UI/History/EpisodeTitleTemplate.html b/UI/History/EpisodeTitleTemplate.html new file mode 100644 index 000000000..616580cbe --- /dev/null +++ b/UI/History/EpisodeTitleTemplate.html @@ -0,0 +1 @@ +{{episode.title}} \ No newline at end of file diff --git a/UI/History/HistoryLayout.js b/UI/History/HistoryLayout.js index eed56ac45..c8861327b 100644 --- a/UI/History/HistoryLayout.js +++ b/UI/History/HistoryLayout.js @@ -26,7 +26,7 @@ define([ headerCell: 'nzbDrone' }, { - name : 'seriesTitle', + name : 'Series.Title', label : 'Series Title', editable : false, cell : Backgrid.TemplateBackedCell.extend({ template: 'Missing/SeriesTitleTemplate' }), @@ -41,11 +41,11 @@ define([ headerCell: 'nzbDrone' }, { - name : 'episode.title', + name : 'Episode.Title', label : 'Episode Title', editable : false, sortable : false, - cell : 'string', + cell : Backgrid.TemplateBackedCell.extend({ template: 'History/EpisodeTitleTemplate' }), headerCell: 'nzbDrone' }, { diff --git a/UI/History/Model.js b/UI/History/Model.js index 55352f398..8931767d8 100644 --- a/UI/History/Model.js +++ b/UI/History/Model.js @@ -1,6 +1,14 @@ "use strict"; define(['app'], function (app) { NzbDrone.History.Model = Backbone.Model.extend({ + mutators: { + seasonNumber: function () { + return this.get('episode').seasonNumber; + }, + paddedEpisodeNumber: function () { + return this.get('episode').episodeNumber.pad(2); + } + } }); }); diff --git a/UI/Series/EpisodeModel.js b/UI/Series/EpisodeModel.js index 2ce17d7f9..c354f08f3 100644 --- a/UI/Series/EpisodeModel.js +++ b/UI/Series/EpisodeModel.js @@ -7,7 +7,7 @@ define(['app'], function () { return bestDateString(this.get('airDate')); }, paddedEpisodeNumber: function () { - return this.get('episodeNumber'); + return this.get('episodeNumber').pad(2); }, day : function () { return Date.create(this.get('airDate')).format('{dd}');