-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiworkshop.js
143 lines (100 loc) · 3.65 KB
/
apiworkshop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require('express');
const app = express();
const port = 3000;
const INITIAL_BALANCE = 100;
let totalAccounts = 1;
let accounts = {
"0": {
name: "Account 1",
balance: 100,
creatorId: "admin",
transactions: []
}
}
app.use(express.json());
app.listen(port, () => console.log(`listening on ${port}`))
app.get('/ping', (req, res) => res.status(200).send("Jordan is great."));
app.post('/accounts', (req,res) => {
const accountName = req.body['name'];
const callerId = req.headers['authorization'];
const newAccount = createAccount(accountName, INITIAL_BALANCE, callerId);
res.status(201).send(newAccount);
});
app.get('/accounts/:accountId', (req,res) => {
const accountId = req.params['accountId'];
const callerId = req.headers['authorization'];
const requestedAccount = getAccountById(accountId);
if (!requestedAccount) {
return res.status(404).send("account doesn't exist.")
}
if (callerId != requestedAccount.creatorId) {
return res.status(401).send("you can't access this!");
}
res.status(200).send(requestedAccount);
})
app.post('/accounts/:sourceId/transactions', (req,res) => {
const sourceId = req.params['sourceId'];
const amount = req.body['amount'];
const destinationId = req.body['destinationId'];
const callerId = req.headers['authorization'];
const requestedAccount = getAccountById(sourceId);
if (!requestedAccount) {
return res.status(404).send("source account not found")
}
if (sourceId == destinationId) {
return res.status(400).send("invalid target");
}
if (callerId != requestedAccount.creatorId) {
return res.status(401).send('no access');
}
if (amount > requestedAccount.balance || amount <= 0) {
return res.status(400).send("invalid balance")
}
const newTransaction = performTransaction(sourceId, destinationId, amount);
res.status(201).send(newTransaction)
})
app.get('/accounts/:accountId/transactions', (req,res) => {
const accountId = req.params['accountId'];
const callerId = req.headers['authorization'];
const requestedAccount = getAccountById(accountId);
if (!requestedAccount) {
return res.status(404).send("account doesn't exist.")
}
if (callerId != requestedAccount.creatorId) {
return res.status(401).send('no access');
}
res.status(200).send(requestedAccount.transactions);
})
function performTransaction(sourceAccountId, destAccountId, amount) {
const sourceAccount = getAccountById(sourceAccountId);
const destAccount = getAccountById(destAccountId);
const newTransaction = {
from: sourceAccountId,
to: destAccountId,
amount: amount
}
sourceAccount.balance -= amount;
destAccount.balance += amount;
sourceAccount.transactions.push(newTransaction);
destAccount.transactions.push(newTransaction);
updateAccount(sourceAccount);
updateAccount(destAccount);
return newTransaction;
}
function createAccount(accountName, initialBalance, creatorId) {
const newAccount = {
id: totalAccounts++,
name: accountName,
balance: initialBalance,
creatorId: creatorId,
transactions: []
}
accounts[String(newAccount.id)] = newAccount;
return accounts[String(newAccount.id)];
}
function getAccountById(accountId) {
return accounts[String(accountId)];
}
function updateAccount(updatedAccount) {
accounts[updatedAccount.id] = updatedAccount;
}