diff --git a/api/api/shares.js b/api/api/shares.js index d65f75d..64f8346 100644 --- a/api/api/shares.js +++ b/api/api/shares.js @@ -45,6 +45,10 @@ router.post('/buy', async (req, res) => { return res.status(500).send({message: "This shareholder already owns that share."}) } + if (shareholder[0].capital < price) { + return res.status(500).send({message: "Insufficient founds."}) + } + await addTransaction(connection, price, share[0].owner_id, buyer_id, share_id) await setShareOwner(connection, share_id, buyer_id) if (share[0].owner != -1) { diff --git a/client/company/main.py b/client/company/main.py index 2ca432d..19b0d65 100644 --- a/client/company/main.py +++ b/client/company/main.py @@ -1,6 +1,8 @@ import requests +from time import sleep +from random import randint -HOST = "http://localhost:3000/" +HOST = "http://192.168.200.15/" """Affichage des actions mises en vente par une entreprise input : @@ -38,5 +40,13 @@ def getCompanies(): return r.json() if __name__ == "__main__": - pass + while True: + companies = getCompanies() + + for company in companies: + price = randint(20, 2000) + for i in range(randint(0, 10)): + emmitShare(company["id"], price) + + sleep(30) diff --git a/client/docker-compose.yml b/client/docker-compose.yml index 241f9a7..be7491a 100644 --- a/client/docker-compose.yml +++ b/client/docker-compose.yml @@ -6,23 +6,9 @@ services: networks: - bourse - shareholder1: + shareholders: build: ./shareholder - container_name: shareholder1 - restart: always - networks: - - bourse - - shareholder2: - build: ./shareholder - container_name: shareholder2 - restart: always - networks: - - bourse - - shareholder3: - build: ./shareholder - container_name: shareholder3 + container_name: shareholders restart: always networks: - bourse diff --git a/client/shareholder/main.py b/client/shareholder/main.py index b8ab1a8..9668589 100644 --- a/client/shareholder/main.py +++ b/client/shareholder/main.py @@ -1,15 +1,17 @@ import requests +from time import sleep +from random import randint -HOST = "http://localhost:3000/" +HOST = "http://192.168.200.15/" """Achat d'une action par un actionnaire à une entreprise input : - idBuyer : identifiant de l'actionnaire - idShare : identifiant de l'action """ -def buyShare(idBuyer, idShare): +def buyShare(idBuyer, idShare, price): url = HOST + 'api/shares/buy' - obj={'id':idBuyer,'share_id':idShare} + obj={'id':idBuyer,'share_id':idShare, 'price': price} r = requests.post(url, json=obj) return r.json() @@ -53,5 +55,13 @@ def getAllBuyers(): return r.json() if __name__ == "__main__": - pass + while True: + shareholders = getAllBuyers() + shares = getAllShares() + + for shareholder in shareholders: + shareId = randint(0, len(shares) - 1) + buyShare(shareholder["id"], shareId, shares[shareId][price] + randint(-20, 20)) + + sleep(30)