As a vision user I want to see a list of projects So that I can select a project I want to monitor
Let's add a test to ./test/project.js
for our feature List projects
. This resource will GET all projects from route /project
and return a 200 Ok
status.
describe('when requesting resource get all projects', function(){ it('should respond with 200', function(done){ request(app) .get('/project/?user=' + login.user) .expect('Content-Type', /json/) .expect(200) .end(function (err, res) { var proj = _.first(JSON.parse(res.text)) assert(_.has(proj, '_id')); assert(_.has(proj, 'name')); assert(_.has(proj, 'user')); assert(_.has(proj, 'token')); assert(_.has(proj, 'created')); assert(_.has(proj, 'repositories')); done(); }); }); });
Let's implement the List projects
feature ./lib/project/index.js
and add an all
function. We attempt to retrieve all projects by calling the static function Project.find
and querying by a user id
. If we get an error we return it, if we find the projects, we return the projects:
Project.prototype.all = function(id, callback){ var query = {"user" : id}; ProjectSchema.find(query, function(error, projects) { if (error) return callback(error, null); return callback(null, projects); }); };
Let's add a new route ./lib/routes/project.js
. We start by defining a route called all
. We start by retrieving a users id
. In order to accommodate the fact that we have not implemented an authentication strategy; we get the user details from our hard-coded login.user
object. We will clean this up in a future chapter. We attempt to retrieve a project by calling Project.all
, passing the userId
. If we get an error, we return 500 Internal Server Error
; if we find projects, we return the projects and a 200 OK
response.
exports.all = function(req, res){ logger.info('Request.' + req.url); var userId = login.user || req.query.user || req.user.id; Project.all(userId, function(error, projects) { if (error) return res.json(500, 'Internal Server Error'), if (projects == null) projects = {}; return res.json(200, projects); }); };
Now, add the following route to the Express server ./lib/express/index.js
:
app.get('/project', project.all);
3.137.164.210