add: added homepage

This commit is contained in:
Lukian 2025-03-24 14:41:48 +01:00
parent 1f24907c80
commit 4ea8c9f2b3
11 changed files with 239 additions and 68 deletions

View file

@ -11,10 +11,10 @@ router.get('/', async (req, res) => {
res.send(channels);
});
router.get('/:id', async (req, res) => {
const id = req.params.id;
router.get('/:name', async (req, res) => {
const name = req.params.name;
const connection = await getConnection();
const channel = await getChannel(connection, id);
const channel = await getChannel(connection, name);
connection.end();
if (channel[0]) {
res.send(channel[0]);
@ -23,22 +23,22 @@ router.get('/:id', async (req, res) => {
}
});
router.get('/:id/messages', async (req, res) => {
const id = req.params.id;
router.get('/:name/messages', async (req, res) => {
const name = req.params.name;
const connection = await getConnection();
const channel = await getChannel(connection, id);
const channel = await getChannel(connection, name);
if (!channel[0]) {
connection.end();
return res.send('No channel found');
}
const messages = await getMessages(connection, id);
const messages = await getMessages(connection, channel[0].id);
connection.end();
res.send(messages);
});
router.post('/:id/messages/send', async (req, res) => {
router.post('/:name/messages/send', async (req, res) => {
const { token, message } = req.body;
const id = req.params.id;
const name = req.params.name;
const connection = await getConnection();
const decoded = jwt.verify(token, process.env.JWT_SECRET);
@ -48,7 +48,13 @@ router.post('/:id/messages/send', async (req, res) => {
return res.status(401).send({ error: 'Invalid token' });
}
await addMessage(connection, id, user[0].id, message.replace("\"", "'"));
const channel = await getChannel(connection, name);
if (!channel[0]) {
connection.end();
return res.send('No channel found');
}
await addMessage(connection, channel[0].id, user[0].id, message.replace("\"", "'"));
connection.end();
res.send({ message: 'Message sent' });
});