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
6
Dockerfile
Normal file
6
Dockerfile
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
FROM node:alpine
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
RUN npm i
|
||||||
|
CMD ["npm", "run", "start"]
|
||||||
|
|
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;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { getConnection, getShareholders } = require("../libs/mysql.js")
|
const { getConnection, getShareholders, getShareholder, getShareholderShares } = require("../libs/mysql.js")
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const connection = await getConnection()
|
const connection = await getConnection()
|
||||||
|
@ -14,5 +14,31 @@ router.get('/', async (req, res) => {
|
||||||
return res.status(200).send(shareholders)
|
return res.status(200).send(shareholders)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res) => {
|
||||||
|
const id = req.params.id
|
||||||
|
const connection = await getConnection()
|
||||||
|
const shareholder = await getShareholder(connection, id)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
if (!shareholder[0]) {
|
||||||
|
return res.status(500).send({message: "There are no shareholder for this id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(shareholder[0])
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/:id/shares', async (req, res) => {
|
||||||
|
const id = req.params.id
|
||||||
|
const connection = await getConnection()
|
||||||
|
const shares = await getShareholderShares(connection, id)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
if (!shares[0]) {
|
||||||
|
return res.status(500).send({message: "There are no shares for that shareholder id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(shares)
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { getConnection, getShares } = require("../libs/mysql.js")
|
const { getConnection, getShares, getShare, getShareholder, setShareOwner, addTransaction, getCompany, addShare, setCompanyCapital, setShareholderCapital } = require("../libs/mysql.js")
|
||||||
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const connection = await getConnection()
|
const connection = await getConnection()
|
||||||
const shares = await getShares(connection)
|
const shares = await getShares(connection)
|
||||||
connection.end()
|
connection.end()
|
||||||
|
|
||||||
if (!shares[0]) {
|
if (!shares[0]) {
|
||||||
return res.status(500).send({message: "There are no shares in the databse."})
|
return res.status(500).send({message: "There are no shares in the databse."})
|
||||||
}
|
}
|
||||||
|
@ -13,5 +14,64 @@ router.get('/', async (req, res) => {
|
||||||
return res.status(200).send(shares)
|
return res.status(200).send(shares)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res) => {
|
||||||
|
const id = req.params.id
|
||||||
|
const connection = await getConnection()
|
||||||
|
const share = await getShare(connection, id)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
if (!share[0]) {
|
||||||
|
return res.status(500).send({message: "There are no share for that id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(share)
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/buy', async (req, res) => {
|
||||||
|
const { buyer_id, share_id, price } = req.body
|
||||||
|
const connection = await getConnection()
|
||||||
|
const share = await getShare(connection, share_id)
|
||||||
|
const shareholder = await getShareholder(connection, buyer_id)
|
||||||
|
|
||||||
|
if (!share[0]) {
|
||||||
|
return res.status(500).send({message: "There are no share for that id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!shareholder[0]) {
|
||||||
|
return res.status(500).send({message: "There are no shareholder for that id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (share[0].owner_id == buyer_id) {
|
||||||
|
return res.status(500).send({message: "This shareholder already owns that share."})
|
||||||
|
}
|
||||||
|
|
||||||
|
await addTransaction(connection, price, share[0].owner_id, buyer_id, share_id)
|
||||||
|
await setShareOwner(connection, share_id, buyer_id)
|
||||||
|
if (share[0].owner != -1) {
|
||||||
|
const seller = await getShareholder(connection, share[0].owner_id)
|
||||||
|
await setShareholderCapital(connection, share[0].owner_id, seller[0].capital + price)
|
||||||
|
}
|
||||||
|
await setShareholderCapital(connection, buyer_id, shareholder[0].capital - price)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
return res.status(200).send({ message: "Success." })
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/emit', async (req, res) => {
|
||||||
|
const { company_id, price } = req.body
|
||||||
|
const connection = await getConnection()
|
||||||
|
const company = await getCompany(connection, company_id)
|
||||||
|
|
||||||
|
if (!company[0]) {
|
||||||
|
return res.status(500).send({message: "There are no company for that id."})
|
||||||
|
}
|
||||||
|
|
||||||
|
await addShare(connection, price, company_id)
|
||||||
|
await setCompanyCapital(connection, company_id, company[0].capital + price)
|
||||||
|
connection.end()
|
||||||
|
|
||||||
|
return res.status(200).send({ message: "Success." })
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
|
|
9
docker-compose.yml
Normal file
9
docker-compose.yml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
container_name: api
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- 80:3000
|
167
libs/mysql.js
167
libs/mysql.js
|
@ -23,6 +23,76 @@ function getShares(connection) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getShare(connection, id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Share WHERE id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getShareholderShares(connection, owner_id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Share WHERE owner_id = ${owner_id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompanyShares(connection, id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Share WHERE company_id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setShareOwner(connection, share_id, owner_id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`UPDATE Share SET owner_id = ${owner_id} WHERE id = ${share_id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addShare(connection, price, company_id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`INSERT INTO Share (price, owner_id, company_id) VALUES (${price}, -1, ${company_id})`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getShareholders(connection) {
|
function getShareholders(connection) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
connection.query(
|
connection.query(
|
||||||
|
@ -37,11 +107,108 @@ function getShareholders(connection) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getShareholder(connection, id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Shareholder WHERE id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setShareholderCapital(connection, id, capital) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`UPDATE Shareholder SET capital = ${capital} WHERE id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompanies(connection) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Company`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCompany(connection, id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM Company WHERE id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCompanyCapital(connection, id, capital ) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`UPDATE Company SET capital = ${capital} WHERE id = ${id}`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addTransaction(connection, price, seller_id, buyer_id, share_id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query(
|
||||||
|
`INSERT INTO Transaction (sell_price, seller_id, buyer_id, share_id) VALUES (${price}, ${seller_id}, ${buyer_id}, ${share_id})`,
|
||||||
|
(error, result) => {
|
||||||
|
if (error) {
|
||||||
|
throw(new Error(error));
|
||||||
|
}
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getConnection,
|
getConnection,
|
||||||
|
|
||||||
getShares,
|
getShares,
|
||||||
|
getShare,
|
||||||
|
getShareholderShares,
|
||||||
|
getCompanyShares,
|
||||||
|
setShareOwner,
|
||||||
|
addShare,
|
||||||
|
|
||||||
getShareholders,
|
getShareholders,
|
||||||
|
getShareholder,
|
||||||
|
setShareholderCapital,
|
||||||
|
|
||||||
|
getCompanies,
|
||||||
|
getCompany,
|
||||||
|
setCompanyCapital,
|
||||||
|
|
||||||
|
addTransaction,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue