-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrepository.ts
264 lines (240 loc) · 8.79 KB
/
repository.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import {
BatchWriteItemCommand,
BatchWriteItemInput,
DeleteItemCommand,
DeleteItemCommandInput,
DynamoDBClient,
GetItemCommand,
GetItemCommandInput,
GetItemCommandOutput,
PutItemCommand,
PutItemCommandInput,
QueryCommand,
QueryCommandInput,
QueryCommandOutput,
UpdateItemCommand,
UpdateItemCommandInput,
WriteRequest
} from "@aws-sdk/client-dynamodb";
import {marshall, unmarshall} from "@aws-sdk/util-dynamodb";
import {ApiError, Movie, Vote} from "./model";
import {SdkError} from "@aws-sdk/types";
import {getIamToken, Token} from "./iam";
import {HttpRequest} from "@aws-sdk/protocol-http";
const MOVIES_TABLE = "movies";
const VOTES_TABLE = "votes";
const MAX_LIMIT = 100;
const ddbClient = new DynamoDBClient({
region: "ru-central-1",
endpoint: process.env.DOCUMENT_API_ENDPOINT
});
export async function saveMovie(movie: Movie): Promise<Movie | ApiError> {
const params: UpdateItemCommandInput = {
TableName: MOVIES_TABLE,
Key: {
"id": {
"N": movie.id.toString()
}
},
UpdateExpression: "SET " +
"type = :type, " +
"original_title = :original_title, " +
"title = :title, " +
"original_language = :original_language, " +
"release_date = :release_date," +
"poster_path = :poster_path," +
"popularity = :popularity, " +
"video = :video, " +
"vote_count = :vote_count, " +
"vote_average = :vote_average, " +
"genres = :genres, " +
"backdrop_path = :backdrop_path, " +
"adult = :adult, " +
"overview = :overview",
ExpressionAttributeValues: {
":type": {"S": movie.type},
":original_title": {"S": movie.original_title},
":title": {"S": movie.title || ""},
":original_language": {"S": movie.original_language || ""},
":release_date": {"S": movie.release_date || ""},
":poster_path": {"S": movie.poster_path || ""},
":popularity": {"N": movie.popularity?.toString() || "0"},
":video": {"S": movie.video || ""},
":vote_count": {"N": movie.vote_count?.toString() || "0"},
":vote_average": {"N": movie.vote_average?.toString() || "0"},
":genres": {"S": movie.genres || ""},
":backdrop_path": {"S": movie.backdrop_path || ""},
":adult": {"S": movie.adult || ""},
":overview": {"S": movie.overview || ""},
}
};
console.debug("Save movie...");
try {
await callWithToken(() => ddbClient.send(new UpdateItemCommand(params)));
console.debug(`Save movie id=${movie.id}, title=${movie.title}`);
return movie;
} catch (e) {
console.error("Failed to save movie: ", e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function batchWriteMovies(movies: Movie[]): Promise<number | ApiError> {
const requests: WriteRequest[] = movies.map(movie => {
return {
PutRequest: {
Item: marshall(movie)
}
} as WriteRequest
});
const params: BatchWriteItemInput = {
RequestItems: {
"movies": requests
}
};
try {
await callWithToken(() => ddbClient.send(new BatchWriteItemCommand(params)));
console.debug(`Wrote ${movies.length} movies`);
return movies.length;
} catch (e) {
console.error("Failed to write movies: ", e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function getMovieById(id: number): Promise<Movie | undefined | ApiError> {
const params: GetItemCommandInput = {
TableName: MOVIES_TABLE,
Key: marshall({"id": id}),
};
try {
const result: GetItemCommandOutput = await callWithToken(() => ddbClient.send(new GetItemCommand(params)));
return result.Item ? unmarshall(result.Item) as Movie : undefined;
} catch (e) {
console.error(e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function deleteMovieById(id: number): Promise<number | ApiError> {
const params: DeleteItemCommandInput = {
TableName: MOVIES_TABLE,
Key: marshall({"id": id})
};
try {
await callWithToken(() => ddbClient.send(new DeleteItemCommand(params)));
console.debug(`Deleted movie id=${id}.`);
return id;
} catch (e) {
console.error(`Failed to delete movie id=${id}: `, e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function getMovies(limit?: number): Promise<Movie[] | ApiError> {
const params: QueryCommandInput = {
TableName: MOVIES_TABLE,
Limit: !limit || (limit > MAX_LIMIT) ? MAX_LIMIT : limit,
IndexName: "PopularityIndex",
KeyConditionExpression: "#t = :type AND #p > :popularity",
ExpressionAttributeNames: {"#t": "type", "#p": "popularity"},
ExpressionAttributeValues: {
":type": {
S: "FILM"
},
":popularity": {
N: "0"
}
},
ScanIndexForward: false
};
try {
const result: QueryCommandOutput = await callWithToken(() => ddbClient.send(new QueryCommand(params)));
return result.Items ? result.Items.map(value => unmarshall(value) as Movie) : [];
} catch (e) {
console.error("Failed to get movies: ", e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function putVote(vote: Vote): Promise<Vote | ApiError> {
const item = marshall(vote);
const params: PutItemCommandInput = {
TableName: VOTES_TABLE,
Item: item
};
console.debug("Adding a new vote...");
try {
await callWithToken(() => ddbClient.send(new PutItemCommand(params)));
console.debug(`Added new vote with user_id=${vote.user_id}, movie_id=${vote.movie_id}, value=${vote.value}`);
return vote;
} catch (e) {
console.error("Failed to add new vote: ", e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function getVote(userId: string, movieId: number): Promise<Vote | undefined | ApiError> {
const params: GetItemCommandInput = {
TableName: VOTES_TABLE,
Key: marshall({"id": userId + "#" + movieId}),
};
try {
const result: GetItemCommandOutput = await callWithToken(() => ddbClient.send(new GetItemCommand(params)));
return result.Item ? unmarshall(result.Item) as Vote : undefined;
} catch (e) {
console.error(e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function getVotes(movieId: number): Promise<Vote[] | ApiError> {
const params: QueryCommandInput = {
TableName: VOTES_TABLE,
IndexName: "MovieIndex",
KeyConditionExpression: "#m = :movie_id",
ExpressionAttributeNames: {"#m": "movie_id"},
ExpressionAttributeValues: {
":movie_id": {
N: movieId.toString()
}
}
};
try {
console.error(`Get votes for movie ${movieId} ...`);
const result: QueryCommandOutput = await callWithToken(() => ddbClient.send(new QueryCommand(params)));
return result.Items ? result.Items.map(value => unmarshall(value) as Vote) : [];
} catch (e) {
console.error(`Failed to get votes for movie ${movieId}: `, e);
return {message: (e as SdkError).message} as ApiError;
}
}
export async function updateRating(movieId: number, rating: number): Promise<number | ApiError> {
const params: UpdateItemCommandInput = {
TableName: MOVIES_TABLE,
Key: {
"id": {
"N": movieId.toString()
}
},
UpdateExpression: "SET rating = :rating",
ExpressionAttributeValues: {
":rating": {"N": rating.toString() || "0"},
}
};
try {
await callWithToken(() => ddbClient.send(new UpdateItemCommand(params)));
console.debug(`Updated rating for movie id = ${movieId}, new rating = ${rating}`);
return rating;
} catch (e) {
console.error("Failed to update rating: ", e);
return {message: (e as SdkError).message} as ApiError;
}
}
function callWithToken(operation: () => Promise<any>): Promise<any> {
ddbClient.middlewareStack.add(
(next) => async (arguments_) => {
const request = arguments_.request as HttpRequest;
const token: Token = await getIamToken();
request.headers["Authorization"] = "Bearer " + token.access_token;
return next(arguments_);
},
{
step: "finalizeRequest",
}
);
return operation.apply({});
}