const express = require('express'); const router = express.Router(); const { getConnection, getCompanies, getCompany, getCompanyShares } = require("../libs/mysql.js") router.get('/', async (req, res) => { const connection = await getConnection() const companies = await getCompanies(connection) connection.end() if (!companies[0]) { return res.status(500).send({message: "There are no companies in the databse."}) } return res.status(200).send(companies) }); router.get('/:id', async (req, res) => { const id = req.params.id const connection = await getConnection() const company = await getCompany(connection, id) connection.end() if (!company[0]) { return res.status(500).send({message: "There are no company for that id."}) } return res.status(200).send(company[0]) }); router.get('/:id/shares', async (req, res) => { const id = req.params.id const connection = await getConnection() const shares = await getCompanyShares(connection, id) connection.end() if (!shares[0]) { return res.status(500).send({message: "There are no shares for that company."}) } return res.status(200).send(shares) }); module.exports = router;