Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Add programs, issuers to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed May 23, 2013
1 parent 4cc5e41 commit 0fbfff9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
10 changes: 10 additions & 0 deletions app.js
Expand Up @@ -236,6 +236,16 @@ app.get('/v2/issuers', api.issuers);
app.get('/v2/programs', api.programs);

app.get('/v2/program/:programShortName', api.program);
app.get('/v2/issuer/:issuerShortName', api.issuer);

// Resources
app.get('/issuer/image/:issuerId', [
issuer.findById
], issuer.image);
app.get('/program/image/:programId', [
issuer.findProgramById
], issuer.programImage);


// Debug endpoints
// ---------------
Expand Down
10 changes: 10 additions & 0 deletions models/issuer.js
Expand Up @@ -152,5 +152,15 @@ Issuer.getAssertionObject = function getAssertionObject(callback) {
});
};

Issuer.prototype.relativeUrl = function relativeUrl(field) {
const formats = {
image: '/issuer/image/%s',
};
return util.format(formats[field], this._id);
};

Issuer.prototype.absoluteUrl = function absoluteUrl(field) {
return env.qualifyUrl(this.relativeUrl(field));
};

module.exports = Issuer;
3 changes: 2 additions & 1 deletion models/program.js
Expand Up @@ -83,7 +83,8 @@ Program.prototype.makeJson = function makeIssuerJson() {
};
Program.prototype.relativeUrl = function relativeUrl(field) {
const formats = {
json: '/program/meta/%s.json'
json: '/program/meta/%s.json',
image: '/program/image/%s',
};
return util.format(formats[field], this._id);
};
Expand Down
79 changes: 51 additions & 28 deletions routes/api.js
Expand Up @@ -273,18 +273,39 @@ exports.issuers = function issuers(req, res) {
var result = { status: 'ok', issuers : {} };
issuers.forEach(function(item) {
result.issuers[item.shortname] = {
name: item.name,
shortname: item.shortname,
url: item.url
name: item.name,
shortname: item.shortname,
url: item.url
};
});
return res.json(200, result);
});
};

/**
* List the programs
*/
exports.issuer = function issuer(req, res, next) {
const issuerShortName = req.params.issuerShortName;
const query = {shortname: issuerShortName};
Issuer.findOne(query, function(err, issuer) {
if (err)
return res.send(500, "There was an error retrieving the issuer");
if (!issuer)
return res.send(404);
const issuerData = [
'shortname',
'name',
'description',
'url',
'contact',
].reduce(function (out, field) {
return (out[field] = issuer[field], out);
}, {});
issuerData.imageUrl = issuer.absoluteUrl('image');
return res.json(200, {
status: 'ok',
issuer: issuerData,
});
});
};

exports.programs = function programs(req, res) {
Program.find({}, function(err, programs) {
Expand All @@ -300,30 +321,32 @@ exports.programs = function programs(req, res) {
};


/**
* List a program
*/
exports.program = function program(req, res) {
if (req.params.programShortName) {
var programShortName = req.params.programShortName;
Program.findOne({shortname: programShortName}, function(err, program) {
if (err) {
return res.send(500, "There was an error retrieving the program");
}
if (program) {
return res.json(200, {
status: 'ok',
program: {
name: program.name
}
});
} else {
return res.send(404, "Not Found");
}
const programShortName = req.params.programShortName;
const query = {shortname: programShortName};
Program.findOne(query, function(err, program) {
if (err)
return res.send(500, "There was an error retrieving the program");
if (!program)
return res.send(404);
const programData = [
'shortname',
'name',
'description',
'url',
'contact',
'startDate',
'endDate',
'phone',
].reduce(function (out, field) {
return (out[field] = program[field], out);
}, {});
programData.imageUrl = program.absoluteUrl('image');
return res.json(200, {
status: 'ok',
program: programData,
});
} else {
res.send(404);
};
});
};

/**
Expand Down
11 changes: 11 additions & 0 deletions routes/issuer.js
Expand Up @@ -152,6 +152,17 @@ exports.updateProgram = function updateProgram(req, res, next) {
});
};

exports.image = function image(req, res, next) {
console.dir(req.issuer);
res.type('png');
return res.send(req.issuer.image);
};
exports.programImage = function image(req, res, next) {
console.dir(req.program);
res.type('png');
return res.send(req.program.image);
};

function handleAccessList(accessList) {
return (
accessList
Expand Down

0 comments on commit 0fbfff9

Please sign in to comment.