add: added some api endpoints

This commit is contained in:
Lukian 2025-04-23 08:25:25 +02:00
parent 217c763abd
commit 736f097d20
4 changed files with 89 additions and 5 deletions

35
back/api/@me.js Normal file
View file

@ -0,0 +1,35 @@
const express = require('express');
const router = express.Router();
const { getConnection, getUserAccounts, getUserCards } = require('../libs/mysql');
const { checkAuth } = require('../libs/middlewares');
router.post('/', checkAuth, async (req, res) => {
const user = req.user;
res.send({ id: user.id, name: user.name, lastname: user.lastname, admin: user.admin });
});
router.post('/accounts', checkAuth, async (req, res) => {
const user = req.user;
const connection = await getConnection();
const accounts = await getUserAccounts(connection, user.id);
connection.end();
if (!accounts[0]) {
return res.status(404).send({ error: 'No accounts found' });
}
res.send({ accounts: accounts });
});
router.post('/cards', checkAuth, async (req, res) => {
const user = req.user;
const connection = await getConnection();
const cards = await getUserCards(connection, user.id);
connection.end();
if (!cards[0]) {
return res.status(404).send({ error: 'No cards found' });
}
res.send({ cards: cards });
});
module.exports = router;

9
back/api/accounts.js Normal file
View file

@ -0,0 +1,9 @@
const express = require('express');
const { getConnection, getUserAccounts } = require('../libs/mysql');
const { checkAuth } = require('../libs/middlewares');
const router = express.Router();
module.exports = router;

View file

@ -49,9 +49,4 @@ router.post('/register', async (req, res) => {
res.send({ message: 'User added' });
});
router.post('/me', checkAuth, async (req, res) => {
const user = req.user;
res.send({ id: user.id, name: user.name, lastname: user.lastname, admin: user.admin });
});
module.exports = router;

View file

@ -72,6 +72,47 @@ function getUserByEmail(connection, email) {
});
}
// +-------------------------------+
// | Accounts |
// +-------------------------------+
function getUserAccounts(connection, id) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT * FROM accounts WHERE client_id = ?`,
[id],
(error, result) => {
if (error) {
reject(new Error(error));
}
resolve(result);
}
);
});
}
// +-------------------------------+
// | Cards |
// +-------------------------------+
function getUserCards(connection, id) {
return new Promise((resolve, reject) => {
connection.query(
`SELECT cards.*
FROM cards
JOIN accounts ON cards.account_id = accounts.id
WHERE client_id = ?`,
[id],
(error, result) => {
if (error) {
reject(new Error(error));
}
resolve(result);
}
);
});
}
module.exports = {
getConnection,
@ -79,4 +120,8 @@ module.exports = {
getUsers,
getUser,
getUserByEmail,
getUserAccounts,
getUserCards,
};