-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransfer-Server.js
65 lines (48 loc) · 1.53 KB
/
Transfer-Server.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
const express = require ('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
//Creds
const users = [
{ username: 'admin', password: 'admin123'},
{ username: 'user1', password: 'password1'},
{ username: 'user2', password: 'password2'},
];
let accounts = [
{ id: 1, username: 'user1', balance:1000},
{ id: 2, username: 'user2', balance:5000},
];
app.use(bodyParser.json());
//User Auth
app.post('/login', (req, res) => {
const { username, password } =
req.body;
const user = users.find(user => user.username === username && user.password === password);
if (!user) {
return res.status(401).json({ message: 'Invalid Credentials'});
}
return res.json ({ message: 'Login Successful', user});
});
//Money Transfer
app.post ( '/transfer', (req, res) => {
const { from, to, amount } = req.body;
//Search for accounts
const fromAccount = accounts.find(acc => acc.username === from);
const toAccount = accounts.find(acc => acc.username === to) ;
if (!fromAccount || !toAccount) {
return
res.status(404).json({ message: 'Account not found'});
}
//Check Balance
if (fromAccount.balance < amount) {
return
res.status(400).json({ message: 'Insufficient Balance!' });
}
//Transfer Money
fromAccount.balance -= amount;
toAccount.balance += amount;
return res.json({ message: 'Transfer successful'});
} );
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});