Created
February 19, 2016 23:27
-
-
Save rastalamm/c3409ab7353d38df3b9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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