board.js
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const app = require('../server');
const db = require('../db');
app.get('/', async(req, res) => {
res.render('boardlist', {boards: await db.get('board').find()});
})
app.get('/board/:bid', async(req, res) => {
let board = await db.get('board').findOne({name: req.params.bid});
if(!board) {
res.status(404).json({error: 'Board not found'});
}
let threads = await db.get('thread').find({board: board._id}, {sort: '-lastUpdated', limit: 50});
res.render('threadslist', {board, threads});
});
/*
app.post('/board/:bid/thread', async(req, res) => {
if(!req.body.title || !req.body.content) { //
res.status(400).json({error: 'Invalid input'});
}
let board = await db.get('board').findOne({name: req.params.bid});
if(!board) {
res.status(404).json({error: 'Board not found'});
}
let thread = await db.get('thread').insert({board: board._id, title: req.body.title, content: req.body.content, lastUpdated: Date.now(), count: 1});
res.json({thread});
});
*/
app.post('/board/:bid', async(req, res) => {
if(!req.body.title || !req.body.content) {
res.status(400).send('제목이나 내용을 써주세요.');
return;
}
let board = await db.get('board').findOne({name:req.params.bid});
if (!board) {
res.status(404).send('그런 판은 우리에게 있을 수 없어.');
}
let thread = await db.get('thread').insert({board: board._id, title: req.body.title, content: req.body.content, lastUpdated: Date.now(), count: 1});
res.redirect('/thread/'+thread.insertedIds);
});
app.get('/borad/:bid/thread/:tid', async(req, res) => {
let thread = await db.get('thread').findOne(req.params.tid);
if(!thread) {
res.status(404).json({error: 'Thread not found'});
}
res.json(thread);
});