added endpoints

This commit is contained in:
Lukian 2024-12-18 09:39:20 +01:00
parent 8a61aaa68f
commit 7d0406ef6e
6 changed files with 314 additions and 2 deletions

44
api/companies.js Normal file
View 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;

View file

@ -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;

View file

@ -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;