added /api/getThreads/:boardId

This commit is contained in:
leca 2023-11-11 21:25:47 +03:00
parent a7f6899164
commit f08823dc95
1 changed files with 10 additions and 1 deletions

View File

@ -100,6 +100,7 @@ app.post('/api/createThread', async (req, res) => {
const boardOptions = (await db.query('SELECT * FROM boards WHERE board_id = $1', [boardId])).rows[0].options
let postId = (await db.query('SELECT nextval(pg_get_serial_sequence(\'posts\', \'post_id\'))')).rows[0].nextval;
let threadId = (await db.query('SELECT nextval(pg_get_serial_sequence(\'threads\', \'thread_id\'))')).rows[0].nextval;
let validateResults = validateThread(threadTitle, isLocked,
isPinned, content, options,
@ -107,10 +108,18 @@ app.post('/api/createThread', async (req, res) => {
if (validateResults != "ok") return res.status(400).send(validateResults);
await db.query('INSERT INTO posts (post_id, content, is_root, timestamp, user_ip) VALUES($1, $2, $3, NOW(), $4)', [postId, content, true, req.socket.remoteAddress]);
await db.query('INSERT INTO threads (thread_name, posts_ids, is_locked, is_pinned, options) VALUES ($1, $2, $3, $4, $5)', [threadTitle, [postId], isLocked, isPinned, options]);
await db.query('INSERT INTO threads (thread_id, thread_name, posts_ids, is_locked, is_pinned, options) VALUES ($1, $2, $3, $4, $5, $6)', [threadId, threadTitle, [postId], isLocked, isPinned, options]);
await db.query('UPDATE boards SET threads_ids = ARRAY_APPEND(threads_ids, $1) WHERE board_id = $2', [threadId, boardId]);
res.redirect(`/${boardId}/${postId}`);
});
app.get('/api/getThreads/:boardId', async (req, res) => {
let queryRes = (await db.query('SELECT * FROM boards WHERE board_id = $1', [req.params.boardId])).rows[0];
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(queryRes.threads_ids));
});
app.get('/api/getBoards', async (req, res) => {
let queryRes = await db.query('SELECT * FROM boards');