From ff8f9f4571a2c0b10e5ca3d8c829f7dcc9f4c348 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 16 Aug 2015 15:28:05 -0700 Subject: Moving episode handling to PodcastEpisode. --- lib/PodcastEpisode.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/UpdateHandlers.js | 41 +------------------ 2 files changed, 109 insertions(+), 39 deletions(-) create mode 100644 lib/PodcastEpisode.js diff --git a/lib/PodcastEpisode.js b/lib/PodcastEpisode.js new file mode 100644 index 0000000..ebe7b26 --- /dev/null +++ b/lib/PodcastEpisode.js @@ -0,0 +1,107 @@ +var doc = require('dynamodb-doc'); +var dynamo = new doc.DynamoDB(); + +var DDB_EPISODES_TABLE = 'podcast-episodes'; + +/** + * PodcastEpisode represents a single podcast episode. + * + * @param {Object} episode - properties to set on the episode. + * @constructor + */ +var PodcastEpisode = module.exports = function PodcastEpisode(episode) { + for (var property in episode) { + if (episode.hasOwnProperty(property)) { + this[property] = episode[property]; + } + } +}; + +/** + * Find an episode in DynamoDB by Id. + * + * @param {Object} id - The episode id. + * @param {Function} - Callback to call with an error or episode. + */ +PodcastEpisode.findById = function(id, callback) { + var params = { + TableName: DDB_EPISODES_TABLE, + Key: id + }; + + dynamo.getItem(params, function(error, data) { + if (error) { + callback(error); + return; + } + + var episode = new PodcastEpisode(data.Item); + callback(null, episode); + }); +} + +/** + * Find all episodes for a given feed. + * + * @param {Object} feedId - The feed id. + * @param {Function} - Callback with signature function(error, episode, last) + */ +PodcastEpisode.forEachEpisode = function(feedId, callback) { + forEachEpisode(feedId, callback); +}; + +function forEachEpisode(feedId, callback, startKey) { + var params = { + TableName: DDB_EPISODES_TABLE, + KeyConditionExpression: "feedId = :feedId", + ExpressionAttributeValues: { + ':feedId': feedId + } + }; + + if (startKey) { + params['ExclusiveStartKey'] = startKey; + } + + dynamo.query(params, function(error, data) { + if (error != null) { + callback(error); + return; + } + + var lastResponse = !data.LastEvaluatedKey; + + data.Items.forEach(function(episodeData, index, array) { + var episode = new PodcastEpisode(episodeData); + var last = lastResponse && index == (array.length - 1); + callback(null, episode, last); + }); + + // If this is not the last set of responses, get more. + if (!lastResponse) { + forEachEpisode(feedId, callback, data.LastEvaluatedKey); + } + }); +} + +/** + * Find all episodes for a given feed and return them as a collection. + * + * @param {Object} feedId - The feed id. + * @param {Function} - Callback with signature function(error, episodes) + */ +PodcastEpisode.getEpisodesForFeed = function(feedId, callback) { + var episodes = []; + PodcastEpisode.forEachEpisode(feedId, function(error, episode, last) { + if (error) { + callback(error); + return; + } + + episodes.append(episode); + + if (last) { + callbacK(null, episodes); + } + }); +}; diff --git a/lib/UpdateHandlers.js b/lib/UpdateHandlers.js index 6483020..2b920d2 100644 --- a/lib/UpdateHandlers.js +++ b/lib/UpdateHandlers.js @@ -5,8 +5,7 @@ var doc = require('dynamodb-doc'); var dynamo = new doc.DynamoDB(); var PodcastView = require('./PodcastView'); - -var DDB_EPISODES_TABLE = 'podcast-episodes'; +var PodcastEpisode = require('./PodcastEpisode'); exports.handleViewUpdate = function(event, context) { // Handle dynamo event for the views table. @@ -125,7 +124,7 @@ exports.handleEpisodeUpdate = function(event, context) { }; function renderViewsForFeed(feedId, callback) { - getEpisodesForFeed(feedId, function(error, episodes) { + PodcastEpisode.getEpisodesForFeed(feedId, function(error, episodes) { if (error) { callback(error); return; @@ -162,39 +161,3 @@ function renderViewsForFeed(feedId, callback) { }); }); } - -function getEpisodesForFeed(feedId, callback, startKey, episodes) { - var params = { - TableName: DDB_EPISODES_TABLE, - ConsistentRead: true, - KeyConditionExpression: "feedId = :feedId", - ExpressionAttributeValues: { - ':feedId': feedId - } - }; - - if (startKey) { - params['ExclusiveStartKey'] = startKey; - } - - dynamo.query(params, function(error, data) { - if (error != null) { - callback(error); - return; - } - - if (!episodes) { - episodes = data.Items; - } else { - episodes = episodes.concat(data.Items); - } - - // If this is not the last set of responses, get more. - var lastKey = data.LastEvaluatedKey; - if (lastKey) { - getEpisodesForFeed(feedId, callback, lastKey, episodes); - } else { - callback(null, episodes); - } - }); -} -- cgit v1.2.3