diff --git a/back/api/@me.js b/back/api/@me.js new file mode 100644 index 0000000..28b5bac --- /dev/null +++ b/back/api/@me.js @@ -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; \ No newline at end of file diff --git a/back/api/accounts.js b/back/api/accounts.js new file mode 100644 index 0000000..99c1e9d --- /dev/null +++ b/back/api/accounts.js @@ -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; \ No newline at end of file diff --git a/back/api/auth.js b/back/api/auth.js index 55226af..c2b2db2 100644 --- a/back/api/auth.js +++ b/back/api/auth.js @@ -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; \ No newline at end of file diff --git a/back/libs/mysql.js b/back/libs/mysql.js index 311ffd1..6120765 100644 --- a/back/libs/mysql.js +++ b/back/libs/mysql.js @@ -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, };