Created
April 27, 2017 16:38
-
-
Save ndugger/c4ada2cc028ba886db7c8c038911cab9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import router from 'redshirt'; | |
| import { admin, body } from '../middleware'; | |
| import { db, slugify } from '../util'; | |
| const NEW = 'new'; | |
| router.group('/forums/categories') | |
| .get('/', async () => { | |
| const categories = await db.forums.categories.all(); | |
| return { | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(categories) | |
| } | |
| }) | |
| .post('/', [ admin, body ], async request => { | |
| const category = JSON.parse(request.body.toString()); | |
| const slug = slugify(category.title); | |
| const created = await db.forums.categories.insert({ ...category, slug }); | |
| return { | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(created); | |
| } | |
| }) | |
| .group('/{ id }') | |
| .get('/', async (request, { id }) => { | |
| const category = await db.forums.categories.get(Number(id)); | |
| const forums = await db.forums.of(category.id); | |
| category.forums = forums; | |
| return { | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(category) | |
| } | |
| }) | |
| .put('/', [ admin, body ], async (request, { id }) => { | |
| const category = JSON.parse(request.body.toString()); | |
| const slug = slugify(category.title); | |
| const updated = await db.forums.categories.update({ ...category, slug }); | |
| return { | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(updated); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment