Skip to content

Commit

Permalink
Get list of shopping list, created by or shared with, for a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerio Gheri committed Mar 27, 2013
1 parent 05e90d3 commit faf8b89
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
contains ObjectIds and for these users we still do not have them as they are not
yet registered! Maybe I could put the username into invitees and the username
could be the same as the one chosen in Facebook... think abou it!

3) Re-engineer code using Q or async.js... right now I have pyramid of doom and
callback hell, plus code repetition of if (err) {} else { ... }
62 changes: 60 additions & 2 deletions handlers/ShoppingListHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ function handleCreateShoppingListRequest(req, res) {
function handleGetShoppingListsRequest(req, res) {
var userId = req.params.userId || null;
var query = req.query;
// If we have a query, it means we don't simply want to retrieve the
// lists for the homepage
// If we have a query, it means we want to retrieve templates
if (query && query.isTemplate) {
handleGetTemplateListsForUserRequest(req, res, userId);
} //
else {
handleGetListsForUserRequest(req, res, userId);
}
}

Expand Down Expand Up @@ -161,6 +163,62 @@ function handleGetTemplateListsForUserRequest(req, res, userId) {
});
}

// Returns 404 for a not existing user and for an empty result set
function handleGetListsForUserRequest(req, res, userId) {
// fetch the user
Account.findById(userId, function(err, account) {
if (err) {
winston.log('error', 'An error has occurred while processing a request to '
+ 'retrieve shopping lists for user ' + userId + ' from ' + req.connection.remoteAddress +
'. Stack trace: ' + err.stack);
res.json(500, {
error: err.message
});
}
else {
if (account) {
// get the list of shopping list IDs for the user
var listIDs = account.shoppingLists;
// then do a find on the shoppingLists collection to look for the IDs retrieved
// filtered by isActive = true and isTemplate = false
var query = {
"_id": { $in: listIDs },
isActive: true,
isTemplate: false
};
ShoppingList.find(query, function(err, lists) {
if (err) {
winston.log('error', 'An error has occurred while processing a request to '
+ 'retrieve shopping lists for user ' + userId + ' from ' + req.connection.remoteAddress +
'. Stack trace: ' + err.stack);
res.json(500, {
error: err.message
});
}
else {
if (lists && lists.length > 0) {
winston.log('info', 'Successfully retrieved shopping lists for user ' + userId
+ '. Request from address ' + req.connection.remoteAddress + '.');
res.send(200, lists);
}
else {
winston.log('info', 'No shopping list for user ' + userId
+ '. Request from address ' + req.connection.remoteAddress + '.');
res.json(404, null);
}
}
});
}
else {
winston.log('info', 'No shopping list for user ' + userId
+ '. Request from address ' + req.connection.remoteAddress + '.');
res.json(404, null);
}
}
});
}


function findTemplatesListsForUser(userId, callback) {
// userId must be the creator
// the list must be marked as a template
Expand Down
51 changes: 50 additions & 1 deletion test/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var server = require('../Server.js');
var mongoose = require('mongoose');
var winston = require('winston');

var ShoppingList = require('../models/ShoppingList');


function makeid()
Expand Down Expand Up @@ -365,6 +365,55 @@
done();
});
});
it('should return a list of all the shopping lists (not template) that the ' +
'user vgheri recentrly created or that have been shared with him',
function(done) {
request(url)
.get('/api/lists/' + userId)
.expect('Content-Type', /json/)
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.not.have.length(0);
/*for (var i = 0; i < res.body.length; i++) {
res.body[i].isTemplate.should.equal(false);
res.body[i].createdBy.toString().should.equal(userId.toString());
}*/
res.body.forEach(function(list) {
list.isTemplate.should.equal(false);
list.createdBy.toString().should.equal(userId.toString());
});
done();
});
});
it('should return 404 trying to get shopping lists for a not existing user',
function(done) {
request(url)
.get('/api/lists/507f191e810c19729de860ea')
.expect('Content-Type', /json/)
.expect(404)
.end(function(err,res) {
if (err) {
throw err;
}
done();
});
});
it('should return 404 trying to get shopping lists for an existing user with no saved lists',
function(done) {
request(url)
.get('/api/lists/5149e05ef9566c132b000003')
.expect('Content-Type', /json/)
.expect(404)
.end(function(err,res) {
if (err) {
throw err;
}
done();
});
});
});
});

Expand Down

0 comments on commit faf8b89

Please sign in to comment.