-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-repository.js
154 lines (120 loc) · 4.25 KB
/
account-repository.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
146
147
148
149
150
151
152
153
154
const uuid = require("uuid");
const dynamoDbClient = require('./dynamodb-client');
// Get account table name from env variable configured through serverless.yml
const ACCOUNT_TABLE = process.env.ACCOUNT_TABLE;
class AccountRepository {
async createAccount(accountRequest) {
try {
// Prepare account item
const timeStamp = new Date().getTime();
const accountItem = {
TableName: ACCOUNT_TABLE,
Item: {
id: uuid.v1(),
name: accountRequest.name,
iban: accountRequest.iban,
createdAt: timeStamp,
updatedAt: timeStamp
}
};
const client = await dynamoDbClient.dynamoDbClient();
console.log("Registering new account");
// insert account in DynamoDB
await client.put(accountItem).promise();
console.log("Registered new account with id " + accountItem.Item.id);
return accountItem.Item;
} catch (err) {
console.log(err);
throw err;
}
}
async getAccount(id) {
try {
// Prepare get account query
const accountParams = {
TableName: ACCOUNT_TABLE,
Key: {
id: id
}
};
const client = await dynamoDbClient.dynamoDbClient();
console.log("Retrieving account with id " + id);
// get account from DynamoDB
const accountItem = await client.get(accountParams).promise();
console.log("Retrieved account with id " + id);
return accountItem.Item;
} catch (err) {
console.log(err);
throw err;
}
}
async getAccounts() {
try {
// Prepare account scan query
const accountParams = {
TableName: ACCOUNT_TABLE,
};
const client = await dynamoDbClient.dynamoDbClient();
console.log("Retrieving accounts");
// get account from DynamoDB
const accountItems = await client.scan(accountParams).promise();
console.log("Retrieved accounts");
return accountItems.Items;
} catch (err) {
console.log(err);
throw err;
}
}
async updateAccount(id, accountUpdateRequest) {
try {
// Prepare update account query
const timeStamp = new Date().getTime();
const accountItem = {
TableName: ACCOUNT_TABLE,
Key: {
id: id,
},
ExpressionAttributeNames: {
'#account_name': 'name',
},
ExpressionAttributeValues: {
':newName': accountUpdateRequest.name,
':newIban': accountUpdateRequest.iban,
':updatedAt': timeStamp
},
UpdateExpression: 'SET #account_name = :newName, iban = :newIban, updatedAt = :updatedAt',
ReturnValues: 'NONE'
};
const client = await dynamoDbClient.dynamoDbClient();
console.log("Updating account with id " + id);
// insert account in DynamoDB
await client.update(accountItem).promise();
console.log("Updated account with id " + id);
return accountItem.Item;
} catch (err) {
console.log(err);
throw err;
}
}
async deleteAccount(id) {
try {
// Prepare delete account query
const accountParams = {
TableName: ACCOUNT_TABLE,
Key: {
id: id
}
};
const client = await dynamoDbClient.dynamoDbClient();
console.log("Deleting account with id " + id);
// delete account from DynamoDB
await client.delete(accountParams).promise();
console.log("Deleted account with id " + id);
return;
} catch (err) {
console.log(err);
throw err;
}
}
}
exports.AccountRepository = AccountRepository;