Added API endpoints and dockerfile

This commit is contained in:
Lukian 2025-03-24 10:17:37 +01:00
parent 924e55b4f3
commit 5ddad2ed61
31 changed files with 3885 additions and 942 deletions

72
back/api/channels.js Normal file
View file

@ -0,0 +1,72 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const { getConnection, getUser, getChannels, getChannel, addChannel, getMessages, addMessage } = require('../libs/mysql');
const router = express.Router();
router.get('/', async (req, res) => {
const connection = await getConnection();
const channels = await getChannels(connection);
connection.end();
res.send(channels);
});
router.get('/:id', async (req, res) => {
const id = req.params.id;
const connection = await getConnection();
const channel = await getChannel(connection, id);
connection.end();
if (channel[0]) {
res.send(channel[0]);
} else {
res.send('No channel found');
}
});
router.get('/:id/messages', async (req, res) => {
const id = req.params.id;
const connection = await getConnection();
const channel = await getChannel(connection, id);
if (!channel[0]) {
connection.end();
return res.send('No channel found');
}
const messages = await getMessages(connection, id);
connection.end();
res.send(messages);
});
router.post('/:id/messages/send', async (req, res) => {
const { token, message } = req.body;
const id = req.params.id;
const connection = await getConnection();
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await getUser(connection, decoded.id);
if (!user[0]) {
connection.end();
return res.status(401).send({ error: 'Invalid token' });
}
await addMessage(connection, id, user[0].id, message.replace("\"", "'"));
connection.end();
res.send({ message: 'Message sent' });
});
router.post('/add', async (req, res) => {
const { name, description, token } = req.body;
const connection = await getConnection();
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await getUser(connection, decoded.id);
if (!user[0]) {
connection.end();
return res.status(401).send({ error: 'Invalid token' });
}
await addChannel(connection, name, description);
connection.end();
res.send({ message: 'Channel added' });
});
module.exports = router;