From b1899b5f6fbae0e1d88683fcf2be748281ca82be Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 21 Aug 2013 17:56:25 -0700 Subject: [PATCH] Using reqres to map episode to episode file --- .../EpisodeFiles/EpisodeFileModule.cs | 14 ++++++++- NzbDrone.Api/Episodes/EpisodeModule.cs | 9 ++---- UI/Cells/EpisodeStatusCell.js | 8 +++-- UI/Episode/Summary/Layout.js | 10 ++++-- UI/Series/Details/SeriesDetailsLayout.js | 11 +++++-- UI/Series/EpisodeFileCollection.js | 31 +++++++++++++++++++ UI/Series/EpisodeFileModel.js | 2 +- UI/app.js | 4 +++ 8 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 UI/Series/EpisodeFileCollection.js diff --git a/NzbDrone.Api/EpisodeFiles/EpisodeFileModule.cs b/NzbDrone.Api/EpisodeFiles/EpisodeFileModule.cs index 40be1ab5e..136bf9e6f 100644 --- a/NzbDrone.Api/EpisodeFiles/EpisodeFileModule.cs +++ b/NzbDrone.Api/EpisodeFiles/EpisodeFileModule.cs @@ -16,10 +16,22 @@ namespace NzbDrone.Api.EpisodeFiles : base("/episodefile") { _mediaFileService = mediaFileService; - + GetResourceAll = GetEpisodeFiles; UpdateResource = SetQuality; } + private List GetEpisodeFiles() + { + var seriesId = (int?)Request.Query.SeriesId; + + if (seriesId == null) + { + throw new BadRequestException("seriesId is missing"); + } + + return ToListResource(() => _mediaFileService.GetFilesBySeries(seriesId.Value)); + } + private EpisodeFileResource SetQuality(EpisodeFileResource episodeFileResource) { var episodeFile = _mediaFileService.Get(episodeFileResource.Id); diff --git a/NzbDrone.Api/Episodes/EpisodeModule.cs b/NzbDrone.Api/Episodes/EpisodeModule.cs index bb17aa218..7ead10be9 100644 --- a/NzbDrone.Api/Episodes/EpisodeModule.cs +++ b/NzbDrone.Api/Episodes/EpisodeModule.cs @@ -10,13 +10,11 @@ namespace NzbDrone.Api.Episodes public class EpisodeModule : NzbDroneRestModule { private readonly IEpisodeService _episodeService; - private readonly MediaFileRepository _mediaFileRepository; - public EpisodeModule(IEpisodeService episodeService, MediaFileRepository mediaFileRepository) + public EpisodeModule(IEpisodeService episodeService) : base("/episodes") { _episodeService = episodeService; - _mediaFileRepository = mediaFileRepository; GetResourceAll = GetEpisodes; UpdateResource = SetMonitored; @@ -31,10 +29,7 @@ namespace NzbDrone.Api.Episodes throw new BadRequestException("seriesId is missing"); } - var resource = ToListResource(() => _episodeService.GetEpisodeBySeries(seriesId.Value)) - .LoadSubtype(e => e.EpisodeFileId, _mediaFileRepository); - - return resource.ToList(); + return ToListResource(() => _episodeService.GetEpisodeBySeries(seriesId.Value)); } private EpisodeResource SetMonitored(EpisodeResource episodeResource) diff --git a/UI/Cells/EpisodeStatusCell.js b/UI/Cells/EpisodeStatusCell.js index 36ab0b72d..091064380 100644 --- a/UI/Cells/EpisodeStatusCell.js +++ b/UI/Cells/EpisodeStatusCell.js @@ -2,10 +2,11 @@ define( [ + 'app', 'Cells/NzbDroneCell', 'moment', 'Shared/FormatHelpers' - ], function (NzbDroneCell, Moment, FormatHelpers) { + ], function (App, NzbDroneCell, Moment, FormatHelpers) { return NzbDroneCell.extend({ className: 'episode-status-cell', @@ -22,7 +23,10 @@ define( var hasFile = this.model.get('hasFile'); if (hasFile) { - var episodeFile = this.model.get('episodeFile'); + var episodeFile = App.request(App.Reqres.GetEpisodeFileById, this.model.get('episodeFileId')); + + this.listenTo(episodeFile, 'change', this._refresh); + var quality = episodeFile.get('quality'); var size = FormatHelpers.bytes(episodeFile.get('size')); var title = 'Episode downloaded'; diff --git a/UI/Episode/Summary/Layout.js b/UI/Episode/Summary/Layout.js index b2ecd2c60..1e552da7b 100644 --- a/UI/Episode/Summary/Layout.js +++ b/UI/Episode/Summary/Layout.js @@ -1,12 +1,14 @@ 'use strict'; define( [ + 'app', 'marionette', 'backgrid', + 'Series/EpisodeFileCollection', 'Cells/FileSizeCell', 'Cells/QualityCell', 'Episode/Summary/NoFileView' - ], function (Marionette, Backgrid, FileSizeCell, QualityCell, NoFileView) { + ], function (App, Marionette, Backgrid, EpisodeFileCollection, FileSizeCell, QualityCell, NoFileView) { return Marionette.Layout.extend({ template: 'Episode/Summary/LayoutTemplate', @@ -48,9 +50,11 @@ define( }, onShow: function () { - if (this.model.get('episodeFile')) { + if (this.model.get('hasFile')) { + var episodeFile = App.request(App.Reqres.GetEpisodeFileById, this.model.get('episodeFileId')); + this.activity.show(new Backgrid.Grid({ - collection: new Backbone.Collection(this.model.get('episodeFile')), + collection: new EpisodeFileCollection(episodeFile, { seriesId: this.model.get('seriesId') }), columns : this.columns, className : 'table table-bordered' })); diff --git a/UI/Series/Details/SeriesDetailsLayout.js b/UI/Series/Details/SeriesDetailsLayout.js index 810d2c502..dc47e0810 100644 --- a/UI/Series/Details/SeriesDetailsLayout.js +++ b/UI/Series/Details/SeriesDetailsLayout.js @@ -4,6 +4,7 @@ define( 'app', 'marionette', 'Series/EpisodeCollection', + 'Series/EpisodeFileCollection', 'Series/SeasonCollection', 'Series/Details/SeasonCollectionView', 'Series/Details/SeasonMenu/CollectionView', @@ -12,7 +13,7 @@ define( 'Shared/Actioneer', 'backstrech', 'Mixins/backbone.signalr.mixin' - ], function (App, Marionette, EpisodeCollection, SeasonCollection, SeasonCollectionView, SeasonMenuCollectionView, InfoView, LoadingView, Actioneer) { + ], function (App, Marionette, EpisodeCollection, EpisodeFileCollection, SeasonCollection, SeasonCollectionView, SeasonMenuCollectionView, InfoView, LoadingView, Actioneer) { return Marionette.Layout.extend({ itemViewContainer: '.x-series-seasons', @@ -85,6 +86,7 @@ define( } $('body').removeClass('backdrop'); + App.reqres.removeHandler(App.Reqres.GetEpisodeFileById); }, _toggleMonitored: function () { @@ -167,14 +169,19 @@ define( this.seasonCollection = new SeasonCollection(); this.episodeCollection = new EpisodeCollection({ seriesId: this.model.id }); + this.episodeFileCollection = new EpisodeFileCollection({ seriesId: this.model.id }); - $.when(this.episodeCollection.fetch(), this.seasonCollection.fetch({data: { seriesId: this.model.id }})).done(function () { + $.when(this.episodeCollection.fetch(), this.episodeFileCollection.fetch(), this.seasonCollection.fetch({data: { seriesId: this.model.id }})).done(function () { var seasonCollectionView = new SeasonCollectionView({ collection : self.seasonCollection, episodeCollection: self.episodeCollection, series : self.model }); + App.reqres.setHandler(App.Reqres.GetEpisodeFileById, function(episodeFileId){ + return self.episodeFileCollection.get(episodeFileId); + }); + /* self.episodeCollection.bindSignalR({ onReceived: seasonCollectionView.onEpisodeGrabbed, context : seasonCollectionView diff --git a/UI/Series/EpisodeFileCollection.js b/UI/Series/EpisodeFileCollection.js new file mode 100644 index 000000000..2cac898ea --- /dev/null +++ b/UI/Series/EpisodeFileCollection.js @@ -0,0 +1,31 @@ +'use strict'; +define( + [ + 'backbone', + 'Series/EpisodeFileModel' + ], function (Backbone, EpisodeFileModel) { + return Backbone.Collection.extend({ + url : window.ApiRoot + '/episodefile', + model: EpisodeFileModel, + + originalFetch: Backbone.Collection.prototype.fetch, + + initialize: function (options) { + this.seriesId = options.seriesId; + }, + + fetch: function (options) { + if (!this.seriesId) { + throw 'seriesId is required'; + } + + if (!options) { + options = {}; + } + + options['data'] = { seriesId: this.seriesId }; + + return this.originalFetch.call(this, options); + } + }); + }); diff --git a/UI/Series/EpisodeFileModel.js b/UI/Series/EpisodeFileModel.js index 25ece8c7c..808c9c77c 100644 --- a/UI/Series/EpisodeFileModel.js +++ b/UI/Series/EpisodeFileModel.js @@ -4,6 +4,6 @@ define( 'backbone' ], function (Backbone) { return Backbone.Model.extend({ - url: window.ApiRoot + '/episodefile' + }); }); diff --git a/UI/app.js b/UI/app.js index 949e6fe0b..4f0406162 100644 --- a/UI/app.js +++ b/UI/app.js @@ -200,6 +200,10 @@ define( ShowLogFile : 'showLogFile' }; + app.Reqres = { + GetEpisodeFileById: 'GetEpisodeFileById' + }; + app.addInitializer(function () { console.log('starting application'); });