diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c560b35 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM node:alpine +WORKDIR /app +COPY . . +RUN npm i +CMD ["npm", "run", "start"] + diff --git a/api/companies.js b/api/companies.js new file mode 100644 index 0000000..b1b373c --- /dev/null +++ b/api/companies.js @@ -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; + diff --git a/api/shareholders.js b/api/shareholders.js index bcb0f8b..4b1ba26 100644 --- a/api/shareholders.js +++ b/api/shareholders.js @@ -1,6 +1,6 @@ const express = require('express'); 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) => { const connection = await getConnection() @@ -14,5 +14,31 @@ router.get('/', async (req, res) => { 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; diff --git a/api/shares.js b/api/shares.js index d919e2c..d65f75d 100644 --- a/api/shares.js +++ b/api/shares.js @@ -1,11 +1,12 @@ const express = require('express'); 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) => { const connection = await getConnection() const shares = await getShares(connection) connection.end() + if (!shares[0]) { 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) }); +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; diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..32b48bb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + api: + build: + context: . + dockerfile: Dockerfile + container_name: api + restart: always + ports: + - 80:3000 diff --git a/libs/mysql.js b/libs/mysql.js index 8fcaba9..98d93e1 100644 --- a/libs/mysql.js +++ b/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) { return new Promise((resolve, reject) => { 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 = { getConnection, getShares, + getShare, + getShareholderShares, + getCompanyShares, + setShareOwner, + addShare, getShareholders, + getShareholder, + setShareholderCapital, + + getCompanies, + getCompany, + setCompanyCapital, + + addTransaction, }