summaryrefslogtreecommitdiff
path: root/lib/PodcastEpisode.js
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2015-08-16 15:28:05 -0700
committerJesse Morgan <jesse@jesterpm.net>2015-08-16 15:28:05 -0700
commitff8f9f4571a2c0b10e5ca3d8c829f7dcc9f4c348 (patch)
tree9a537a36487466f9f60ff6cc957ac8911e98ef03 /lib/PodcastEpisode.js
parent686cdf055174044cbd037327418efbe91beb7131 (diff)
Moving episode handling to PodcastEpisode.HEADmaster
Diffstat (limited to 'lib/PodcastEpisode.js')
-rw-r--r--lib/PodcastEpisode.js107
1 files changed, 107 insertions, 0 deletions
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);
+ }
+ });
+};