Skip to content

Commit

Permalink
Why use constructors in service files?
Browse files Browse the repository at this point in the history
This PR is opened in the form of a question. Why are constructors used
at all in the singleton module services? It's awkward that something is
exported initialized. If the constructor were exported, I could see it
being useful for testing and reusability, but right now it's just an
object serving as a layer of indirection.
  • Loading branch information
tbranyen committed Oct 18, 2015
1 parent 6891e3f commit 717cde1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
22 changes: 9 additions & 13 deletions rating-service.js
Expand Up @@ -4,12 +4,10 @@ var path = require('path');
var ratingsDB = new PouchDB(path.join(__dirname, 'ratings_db'));

// ratings service
function RatingService() {}
RatingService.prototype = {

module.exports = {
getRatings: function(titleIds, userId) {
userId = userId || 'all';

return ratingsDB.allDocs({
keys: titleIds.map(function(id) {
return userId + "," + id;
Expand All @@ -25,17 +23,17 @@ RatingService.prototype = {
ratings[row.key.substr((userId + ",").length)] = {error: row.error};
}
} else if (row.doc) {
ratings[row.key.substr((userId + ",").length)] = row;
ratings[row.key.substr((userId + ",").length)] = row;
} else {
ratings[row.key.substr((userId + ",").length)] = {doc: null};
}
}
});
return ratings;
});
},
setRatings: function(userId, titlesIdsAndRating) {

setRatings: function(userId, titlesIdsAndRating) {

function coerce(rating) {
if (rating > 5)
return 5;
Expand Down Expand Up @@ -64,7 +62,7 @@ RatingService.prototype = {
};
})
).then(function(setResponse) {

var results = {};
getResponse.rows.forEach(function(response, index) {
if (!setResponse[index]) {
Expand Down Expand Up @@ -93,8 +91,6 @@ RatingService.prototype = {
});
return results;
});
});
});
}
};

module.exports = new RatingService();
31 changes: 14 additions & 17 deletions recommendation-service.js
Expand Up @@ -5,11 +5,10 @@ var recommendationsDB = new PouchDB(path.join(__dirname, 'recommendations_db'));
var batch = require('./batch');

// genrelist service
function RecommendationsService() {}
RecommendationsService.prototype = {
module.exports = {
getGenreList: function(userId) {
userId = (userId || 'all').toString();

var getGenreLists = batch(function(userIds) {
return recommendationsDB.allDocs({
keys: userIds.map(function(x) { return x.toString(); }),
Expand All @@ -20,52 +19,50 @@ RecommendationsService.prototype = {
genreLists[row.key] = row;
});
return genreLists;
});
});
});

return getGenreLists([userId]).then(function(genreLists) {
return genreLists[userId].doc.recommendations;
});
},

addTitleToGenreList: function(userId, genreIndex, titleId) {
userId = userId.toString();
userId = userId.toString();
return recommendationsDB.get(userId)
.then(function(response) {
if (!response.recommendations[genreIndex]) {
return Promise.reject(new Error("genrelist does not exist at index " + genreIndex));
}
}
var titlesLength = response.recommendations[genreIndex].titles.push(titleId);
return recommendationsDB.put({
_id: userId,
_rev: response._rev,
recommendations: response.recommendations
recommendations: response.recommendations
}).then(function() {
return titlesLength;
return titlesLength;
});
});
},

removeTitleFromGenreListByIndex: function(userId, genreIndex, titleIndex) {
userId = userId.toString();
return recommendationsDB.get(userId)
.then(function(response) {
if (!response.recommendations[genreIndex]) {
return Promise.reject(new Error("genrelist does not exist at index " + genreIndex));
}
}
var removedTitleId = response.recommendations[genreIndex].titles.splice(titleIndex, 1)[0];
return recommendationsDB.put({
_id: userId,
_rev: response._rev,
recommendations: response.recommendations
}).then(function() {
return {
titleId: removedTitleId,
titleId: removedTitleId,
length: response.recommendations[genreIndex].titles.length
};
};
});
});
}
}
};

module.exports = new RecommendationsService();
7 changes: 2 additions & 5 deletions title-service.js
Expand Up @@ -4,8 +4,7 @@ var path = require('path');
var titlesDB = new PouchDB(path.join(__dirname, 'titles_db'));

// titles service
function TitleService() {}
TitleService.prototype = {
module.exports = {
getTitles: batch(function(titleIds) {
return titlesDB.allDocs({
keys: titleIds.map(function(x) { return x.toString(); }),
Expand All @@ -20,7 +19,7 @@ TitleService.prototype = {
titles[row.key] = {error: row.error};
}
} else if (row.doc) {
titles[row.key] = row;
titles[row.key] = row;
} else {
titles[row.key] = {doc: null};
}
Expand All @@ -29,5 +28,3 @@ TitleService.prototype = {
});
})
};

module.exports = new TitleService();

0 comments on commit 717cde1

Please sign in to comment.