moving to koa 2.0; + api
This commit is contained in:
1 parent
44724132c0
commit
0ffa403b60
17 files changed
+398
-339
No files matched your search
+39
-32
@@ -1,38 +1,45 @@
|
||||
import compose from 'koa-compose';
|
||||
import co from 'co';
|
||||
import route from 'koa-route';
|
||||
import {json} from './';
|
||||
|
||||
import {getSchedule, getScheduleList, populateSchedule} from '../core/schedule';
|
||||
|
||||
export default function (app) {
|
||||
app.use(
|
||||
route.get('/schedule/list', json(
|
||||
function *() {
|
||||
this.type = 'application/json';
|
||||
let data = yield getScheduleList();
|
||||
if (!data) this.status = 404;
|
||||
else this.body = data;
|
||||
}
|
||||
))
|
||||
);
|
||||
|
||||
app.use(
|
||||
route.get('/schedule/:period?/:semester?', json(
|
||||
function *(period, semester) {
|
||||
this.type = 'application/json';
|
||||
|
||||
let data = yield getSchedule(period, semester);
|
||||
if (!data) {
|
||||
this.status = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
let doPopulate = this.query.populate || false;
|
||||
if (doPopulate) {
|
||||
data.schedule = yield populateSchedule(data.schedule);
|
||||
}
|
||||
|
||||
this.body = data;
|
||||
}
|
||||
))
|
||||
);
|
||||
export default function () {
|
||||
return compose([
|
||||
list(),
|
||||
schedule()
|
||||
]);
|
||||
};
|
||||
|
||||
function list() {
|
||||
return route.get('/schedule/list', json(
|
||||
async(ctx) => {
|
||||
ctx.type = 'application/json';
|
||||
let data = await getScheduleList();
|
||||
if (!data) ctx.status = 404;
|
||||
else ctx.body = data;
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
return route.get('/schedule/:period?/:semester?', json(
|
||||
async(ctx, period, semester) => {
|
||||
ctx.type = 'application/json';
|
||||
|
||||
let data = await getSchedule(period, semester);
|
||||
if (!data) {
|
||||
ctx.status = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
let doPopulate = ctx.query.populate || false;
|
||||
if (doPopulate) {
|
||||
data.schedule = await populateSchedule(data.schedule);
|
||||
}
|
||||
|
||||
ctx.body = data;
|
||||
}
|
||||
))
|
||||
}
|
||||
Reference in new issue
Block a user