From f08823dc9518fe3a76c88ba21b0924b95ed34b82 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 11 Nov 2023 21:25:47 +0300 Subject: [PATCH] added /api/getThreads/:boardId --- src/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e26cf2d..b2c7a3b 100644 --- a/src/index.js +++ b/src/index.js @@ -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');