-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-controller.js
145 lines (123 loc) · 3.48 KB
/
account-controller.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
144
145
const { AccountService } = require('./account-service');
const accountService = new AccountService();
exports.createAccount = async (event) => {
try {
// extract request body
const accountRequest = JSON.parse(event.body);
// validation
if (!accountRequest.name || !accountRequest.iban) {
const response = {
statusCode: 400,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: 'invalid_data', message: 'name and iban are mandatory' }),
};
return response;
}
// create new account
const account = await accountService.createAccount(accountRequest);
// prepare response
const response = {
statusCode: 201,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(account),
};
return response;
} catch (err) {
console.log(err);
throw err;
}
};
exports.getAccount = async (event) => {
try {
// validation
if (!event.pathParameters.id) {
const response = {
statusCode: 400,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: 'invalid_data', message: 'account id is mandatory' }),
};
return response;
}
// get account
const account = await accountService.getAccount(event.pathParameters.id);
// prepare response
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(account),
};
return response;
} catch (err) {
console.log(err);
throw err;
}
};
exports.getAccounts = async (event) => {
try {
// get accounts
const accounts = await accountService.getAccounts();
// prepare response
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(accounts),
};
return response;
} catch (err) {
console.log(err);
throw err;
}
};
exports.updateAccount = async (event) => {
try {
// extract request body
const accountUpdateRequest = JSON.parse(event.body);
// validation
if (!event.pathParameters.id || !accountUpdateRequest.name || !accountUpdateRequest.iban) {
const response = {
statusCode: 400,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: 'invalid_data', message: 'id, name and iban are mandatory' }),
};
return response;
}
// update account
const account = await accountService.updateAccount(event.pathParameters.id,
accountUpdateRequest);
// prepare response
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(account),
};
return response;
} catch (err) {
console.log(err);
throw err;
}
};
exports.deleteAccount = async (event) => {
try {
// validation
if (!event.pathParameters.id) {
const response = {
statusCode: 400,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: 'invalid_data', message: 'account id is mandatory' }),
};
return response;
}
// delete account
const account = await accountService.deleteAccount(event.pathParameters.id);
// prepare response
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(account),
};
return response;
} catch (err) {
console.log(err);
throw err;
}
};