Skip to content

Commit a59f171

Browse files
author
jing
committed
refactor - routes
1 parent b9dbd5d commit a59f171

File tree

8 files changed

+52
-34
lines changed

8 files changed

+52
-34
lines changed

src/app.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const debug = require('debug')('app');
66
const morgan = require('morgan');
77

88
const {accounts, users, writeJSON} = require('./data');
9+
const accountRoutes = require('./routes/accounts');
10+
const servicesRoutes = require('./routes/services');
911

1012
const app = express();
1113
const PORT = process.env.PORT || 3000;
@@ -18,6 +20,8 @@ app.set('view engine', 'ejs');
1820
app.use(express.static(path.join(__dirname, 'public')));
1921
app.use(morgan('tiny'));
2022
app.use(express.urlencoded({extended: true}));
23+
app.use('/account', accountRoutes);
24+
app.use('/services', servicesRoutes);
2125

2226

2327
app.get('/', (req, res) => {
@@ -26,37 +30,10 @@ app.get('/', (req, res) => {
2630
accounts,
2731
});
2832
})
29-
app.get('/savings', (req, res) => {
30-
res.render('account', {account: accounts.savings});
31-
})
32-
app.get('/checking', (req, res) => {
33-
res.render('account', {account: accounts.checking});
34-
})
35-
app.get('/credit', (req, res) => {
36-
res.render('account', {account: accounts.credit});
37-
})
33+
3834
app.get('/profile', (req, res) => {
3935
res.render('profile', {user: users[0]});
4036
})
41-
app.get('/transfer', (req, res) => {
42-
res.render('transfer');
43-
})
44-
app.post('/transfer', (req, res) => {
45-
accounts[req.body.from].balance = accounts[req.body.from].balance - req.body.amount;
46-
accounts[req.body.to].balance = parseInt(accounts[req.body.to].balance) + parseInt(req.body.amount, 10);
47-
writeJSON();
48-
res.render('transfer', {message: 'Transfer Completed'});
49-
})
50-
51-
app.get('/payment', (req, res) => {
52-
res.render('payment', {account: accounts.credit});
53-
})
5437

55-
app.post('/payment', (req, res) => {
56-
accounts.credit.balance -= req.body.amount;
57-
accounts.credit.available += parseInt(req.body.amount, 10);
58-
writeJSON();
59-
res.render('payment', {message: 'Payment successful', account: accounts.credit});
60-
})
6138

6239
app.listen(PORT, () => debug(`Listerning on port ${chalk.green(PORT)}`));

src/routes/accounts.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const {accounts} = require('../data');
4+
5+
router.get('/savings', (req, res) => {
6+
res.render('account', {account: accounts.savings});
7+
})
8+
router.get('/checking', (req, res) => {
9+
res.render('account', {account: accounts.checking});
10+
})
11+
router.get('/credit', (req, res) => {
12+
res.render('account', {account: accounts.credit});
13+
})
14+
15+
module.exports = router;

src/routes/services.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const {accounts, writeJSON} = require('../data');
4+
5+
router.get('/transfer', (req, res) => {
6+
res.render('transfer');
7+
})
8+
router.post('/transfer', (req, res) => {
9+
accounts[req.body.from].balance = accounts[req.body.from].balance - req.body.amount;
10+
accounts[req.body.to].balance = parseInt(accounts[req.body.to].balance) + parseInt(req.body.amount, 10);
11+
writeJSON();
12+
res.render('transfer', {message: 'Transfer Completed'});
13+
})
14+
15+
router.get('/payment', (req, res) => {
16+
res.render('payment', {account: accounts.credit});
17+
})
18+
19+
router.post('/payment', (req, res) => {
20+
accounts.credit.balance -= req.body.amount;
21+
accounts.credit.available += parseInt(req.body.amount, 10);
22+
writeJSON();
23+
res.render('payment', {message: 'Payment successful', account: accounts.credit});
24+
})
25+
26+
module.exports = router;

src/views/account.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<%- include('transactions', {account: account}); %>
44

55
<% if (account.unique_name == 'credit' && account.balance > 0) { %>
6-
<br><a href="/payment">Make a Payment</a>
6+
<br><a href="/services/payment">Make a Payment</a>
77
<% } %>
88

99
<br><a href="/">Back to Accounts Summary</a>

src/views/index.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111

1212
<br>
13-
<a href="/transfer">Transfer</a>
13+
<a href="/services/transfer">Transfer</a>
1414
<%- include('footer.ejs'); %>

src/views/payment.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<span class="error"></span>
99

1010
<p>Current Balance: $<%= account.balance %></p>
11-
<form id="paymentForm" action="/payment" method="POST">
11+
<form id="paymentForm" action="/services/payment" method="POST">
1212
<label for="amount">Payment: <input type="number" name="amount" id="amount" required placeholder="amount" step="0.01"></label>
1313
<button type="submit">Submit Payment</button>
1414
</form>

src/views/summary.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
<th>Current Balance</th>
1313
</tr>
1414
<tr>
15-
<td><a href="/<%= account.unique_name %>"><%= account.nickname %></a></td>
15+
<td><a href="/account/<%= account.unique_name %>"><%= account.nickname %></a></td>
1616
<td><%= account.account_number %></td>
1717
<% if (account.unique_name == 'credit') { %>
1818
<td>$<%= account.available %></td>
1919
<% } else { %>
2020
<td>$<%= account.prior_balance %></td>
2121
<% } %>
22-
22+
2323
<td>$<%= account.balance %></td>
2424
</tr>
2525
</table>

src/views/transfer.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<% } %>
88
<span class="error"></span>
99

10-
<form id="transferForm" action="/transfer" method="post">
10+
<form id="transferForm" action="/services/transfer" method="post">
1111
<span>Transfer From: </span>
1212
<select required name="from" id="from">
1313
<option value="checking">Checking</option>

0 commit comments

Comments
 (0)