Skip to content

Instantly share code, notes, and snippets.

@rastalamm
Created February 19, 2016 23:27
Show Gist options
  • Save rastalamm/c3409ab7353d38df3b9c to your computer and use it in GitHub Desktop.
Save rastalamm/c3409ab7353d38df3b9c to your computer and use it in GitHub Desktop.
/*jslint es6, node: true */
(function () {
"use strict";
const express = require('express');
const router = express.Router();
const Lesson = require('../models').Lesson;
const getAllLessons = function () {
return Lesson.findAll();
};
const getLessonById = function (id) {
return Lesson.findById(id)
.then(function (lesson) {
if (!lesson) {
throw {code: 404};
}
return lesson;
});
};
router.get('/', function (ignore, res) {
getAllLessons()
.then(function (lessons) {
res.json(lessons);
})
.catch(function () {
res.sendStatus(500);
});
});
router.get('/:id', function (req, res) {
getLessonById(req.params.id)
.then(function (lesson) {
res.json(lesson);
})
.catch({code: 404}, function () {
res.sendStatus(404);
})
.catch(function () {
res.sendStatus(500);
});
});
module.exports = {router};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment