Skip to content
This repository has been archived by the owner on Nov 13, 2017. It is now read-only.

Commit

Permalink
Added /api/submissions/:submissionId endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed May 22, 2013
1 parent 0f86931 commit 7edf6b7
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
15 changes: 15 additions & 0 deletions doc/api-raw.html
Expand Up @@ -39,6 +39,8 @@ <h3><a id="schemas_submission" href="#schemas_submission">Submission</a></h3>
<p>The submission model is complex, and contains a number of
sub-models.</p>
<dl>
<dt>_id</dt>
<dd>The unique identifier for the submission, e.g. <code>519d2036a8318da30e000004</code>. Assigned by the server when a submission is first created.</dd>
<dt>learner</dt>
<dd>The email address of the learner who is submitting the
application.</dd>
Expand Down Expand Up @@ -230,6 +232,19 @@ <h4>Example Response</h4>
}</pre>
</section>

<section id="submission-get">
<h2><a href="#submission-get">Get a submission</a></h2>

<pre>GET /api/submissions/:submissionId</pre>

<p>This endpoint retrieves an existing submission.</p>

<h4>Example Response</h4>

<pre class="http headers">Status: 200 OK</pre>
<pre class="http response">{{examples.awarded}}</pre>
</section>

<section id="submission-list">
<h2><a href="#submission-list">List submissions</a></h2>

Expand Down
4 changes: 4 additions & 0 deletions lib/api.js
Expand Up @@ -23,6 +23,10 @@ function makeConciseValidationError(err) {
};
}

exports.getSubmission = function(req, res, next) {
res.send(res.locals.submission);
};

exports.listSubmissions = function(req, res, next) {
if (!req.query.learner)
return res.send(422, {"message": "invalid search query"});
Expand Down
2 changes: 2 additions & 0 deletions lib/app.js
Expand Up @@ -124,6 +124,7 @@ exports.build = function(options) {

app.use(function CsrfOrApiAuth(req, res, next) {
if (req.path.match(/^\/api\//)) {
req.isApi = true;
return apiAuth(req, res, next);
} else
return csrf(req, res, next);
Expand All @@ -143,6 +144,7 @@ exports.build = function(options) {
app.get('/demo', website.demo);
app.get('/docs', website.docs);

app.get('/api/submissions/:submissionId', api.getSubmission);
app.get('/api/submissions', api.listSubmissions);
app.post('/api/submission', api.submit);
app.get('/api/mentors', api.listMentors);
Expand Down
13 changes: 9 additions & 4 deletions lib/website.js
Expand Up @@ -8,21 +8,26 @@ const RESULTS_PER_PAGE = 10;

exports.findSubmissionById = function(req, res, next, id) {
Submission.findOne({_id: id}, function(err, submission) {
function proceed() {
res.locals.submission = submission;
next();
}

if (err) {
if (err.name == "CastError")
return res.send(404);
return next(err);
}
if (!submission)
return res.send(404);
if (req.isApi)
return proceed();
if (!req.session.email)
return res.status(401).render('access-denied.html');
submission.canBeReviewedBy(req.session.email, function(err, result) {
if (err) return next(err);
if (result) {
res.locals.submission = submission;
return next();
}
if (result)
return proceed();
return res.status(403).render('access-denied.html');
});
});
Expand Down
4 changes: 4 additions & 0 deletions static/js/demo.js
Expand Up @@ -114,6 +114,10 @@ $(window).ready(function() {
encodeURIComponent($('#js-learner').val()))
});

$("#js-get-submission").click(function() {
$.get('/api/submissions/' + $('#js-submission-id').val());
});

$("form.js-submission").submit(function() {
postJSON("/api/submission", $(this.elements['json']).val());
return false;
Expand Down
22 changes: 22 additions & 0 deletions test/api.test.js
Expand Up @@ -44,6 +44,28 @@ describe('API', function() {
.expect(422, done);
});

it('GET /submission/:submissionId should work', function(done) {
async.series([
db.removeAll(aestimia.models.Submission),
db.create(aestimia.models.Submission, data.baseSubmission({
_id: "000000000000000000000001",
learner: "foo@bar.org",
classifications: ["math"],
})),
function(cb) {
request(app)
.get('/api/submissions/000000000000000000000001')
.set('Authorization', authHeader)
.expect(200, function(err, res) {
if (err) return cb(err);
res.body._id.should.eql("000000000000000000000001");
res.body.learner.should.eql("foo@bar.org");
cb();
});
}
], done);
});

it('GET /submissions should list learner submissions', function(done) {
async.series([
db.removeAll(aestimia.models.Submission),
Expand Down
1 change: 1 addition & 0 deletions test/data.js
Expand Up @@ -54,6 +54,7 @@ exports.submissions = {

exports.reviewedSubmissions = {
'awarded': baseSubmission({
_id: "000000000000000000000001",
reviews: [{
author: "baz@bar.org",
response: "cool yo",
Expand Down
16 changes: 16 additions & 0 deletions views/demo.html
Expand Up @@ -115,6 +115,22 @@ <h5>Input</h5>
{% endfor %}
</div>
</div>
<div data-section-id="submission-get">
<h5>URL Arguments</h5>

<table class="table http route-args">
<tr>
<th>Argument</th>
<th>Value</th>
</tr>
<tr>
<td><code>submissionId</code></td>
<td><input type="text" id="js-submission-id"></td>
</tr>
</table>

<button id="js-get-submission" class="btn">Get Submission</button>
</div>
<div data-section-id="submission-list">
<h5>Query String Arguments</h5>

Expand Down

0 comments on commit 7edf6b7

Please sign in to comment.