generated from lucien/api-template
added endpoints
This commit is contained in:
parent
8a61aaa68f
commit
7d0406ef6e
6 changed files with 314 additions and 2 deletions
44
api/companies.js
Normal file
44
api/companies.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
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;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue