-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhellowAPI.js
More file actions
177 lines (124 loc) · 4.07 KB
/
Copy pathhellowAPI.js
File metadata and controls
177 lines (124 loc) · 4.07 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//const express = require ('express')();
//const app =express();
// const Joi = require ('joi');
const Joi = require('@hapi/joi');
const express = require ('express');
const app =express();
app.use(express.json());
//app.use(express.json())
const port = process.env.PORT || 8080 ;
app.listen(
port,() => console.log(`it is live on http://localhost:${port}`)
)
app.get('/cust_info',(req, res)=> {
res.status(200).send({
acc_number: '10002211623',
cust_name: 'TinsaE Teka',
acc_type: 'SAVING',
acc_open_date: '12-03-18',
balance: '60000'
})
});
app.get('/',(req, res)=> {
res.status(200).send({
Hellow : 'FROM the API team AT DASHEN BANK |'
})
});
app.get('/trx_info', (req, res)=>{
res.status(200).send({
acc_number: '10002211623',
cust_name: 'Tinsae Teka',
acc_type: 'SAVING',
acc_open_date: '12-03-18',
trx_type: 'internet transfer',
amount: '7000',
date: '5-03-19',
credited_acc: '51236494949',
bank: 'CBE'
})
});
app.get('/posts/:year/:month',(req, res)=> {
res.status(200).send(req.params);
});
const transactions = [
{ id: 1, acc_n0: '5556425252474', balance:'25000', amount: '2000', currency : 'ETB' } ,
{ id: 2, acc_n0: '5022301252014', balance:'25000', amount: '1000', currency : 'ETB' } ,
{ id: 3, acc_n0: '5532235322585', balance:'25000', amount: '500', currency : 'USD' } ,
{ id: 4, acc_n0: '0002525555225', balance:'25000',amount: '15000', currency : 'ETB' } ,
];
app.get('/transaction/:id',(req, res)=> {
//res.status(200).send(req.params);
let trx= transactions.find(c => c.id=== parseInt(req.params.id));
if(!trx) res.status(404).send('record not found on the server. <br> 404');// 404
res.send(trx);
});
// all transactions
app.get('/transactions/',(req, res)=> {
res.status(200).send(transactions);
});
const schema = Joi.object({
acc_n0:Joi.string().min(10).required(),
});
// creating resources
//req ,res --- route handler || OR
app.post('/transactions', (req,res) => {
// if(req.body.acc_n0.length != 10){
// //400 bad request
// res.status(400).send('Account number is not valid. <br> Account number must be only ten (10) diigits');
// return;
// }
// if (!req.body.amount || !req.body.balance || !req.body.acc_n0 || !req.body.amount ){
// res.status(400).send('All fields are required. Acc_n0, balance, amount and currency');
// }
// joi
// the shape of the object properties ... data tyoe... length...
const { error } = schema.validate(req.body);
// Error in response
res.send(error.details[0].message);
const transaction = {
id : transactions.length + 1,
acc_n0: req.body.acc_no,
balance: req.body.balance,
amount: req.body.amount,
currency: req.body.currency
};
transactions.push(transaction);
res.send(transaction);
});
app.put('/transaction/:id', (req, res) =>{
})
/*
all transactions http://localhost:8080/transactions
one transaction detail http://localhost:8080/transaction/5/
unavilable resource on the server http://localhost:8080/transaction/90/
*/
// const express = require ('express')();
// const app =express();
// // const app = require ('express')();
// const port =8080;
// app.use(express.json())
// app.listen(
// port,() => console.log('it is live on http://localhost:8080')
// )
// app.get('/cust_info',(req, res)=> {
// res.status(200).send({
// acc_number: '10002211623',
// cust_name: 'Tinsae Teka',
// acc_type: 'SAVING',
// acc_open_date: '12-03-18',
// balance: '60000'
// })
// });
// app.get('/trx_detail', (req, res)=>{
// res.status(200).send({
// acc_number: '10002211623',
// cust_name: 'Tinsae Teka',
// acc_type: 'SAVING',
// acc_open_date: '12-03-18',
// trx_type: 'internet transfer',
// amount: '7000',
// date: '5-03-19',
// credited_acc: '51236494949',
// bank: 'CBE'
// })
// });