Showing
1 changed file
with
30 additions
and
0 deletions
src/routes/board.js
0 → 100644
1 | +const app = require('./server'); | ||
2 | +const db = require('./db'); | ||
3 | + | ||
4 | +app.get('/board/:bid', async(req, res) => { | ||
5 | + let board = await db.get('board').findOne({name: req.params.bid}); | ||
6 | + if(!board) { | ||
7 | + res.status(404).json({error: 'Board not found'}); | ||
8 | + } | ||
9 | + let threads = await db.get('thread').find({board: board._id}, {sort: '-lastUpdated', limit: 20}); | ||
10 | + res.json({threads: threads}) | ||
11 | +}); | ||
12 | + | ||
13 | +app.post('/board/:bid/thread', async(req, res) => { | ||
14 | + if(!req.body.title || !req.body.content) { | ||
15 | + res.status(400).json({error: 'Invalid input'}); | ||
16 | + } | ||
17 | + let board = await db.get('board').findOne({name: req.params.bid}); | ||
18 | + if(!board) { | ||
19 | + res.status(404).json({error: 'Board not found'}); | ||
20 | + } | ||
21 | + let thread = await db.get('thread').insert({board: board._id, title: req.body.title, content: req.body.content, lastUpdated: Date.now()}); | ||
22 | + res.json({thread}); | ||
23 | +}); | ||
24 | +app.get('/borad/:bid/thread/:tid', async(req, res) => { | ||
25 | + let thread = await db.get('thread').findOne(req.params.tid); | ||
26 | + if(!thread) { | ||
27 | + res.status(404).json({error: 'Thread not found'}); | ||
28 | + } | ||
29 | + res.json(thread); | ||
30 | +}); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment