-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
129 lines (105 loc) · 3.31 KB
/
handler.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
"use strict";
import * as AWS from "aws-sdk";
import { APIGatewayProxyHandler, APIGatewayProxyEvent } from "aws-lambda";
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" });
const playersTable = process.env.PLAYERS_TABLE || "players";
interface Player {
id: number, // fide player id
name: string,
rating: number | null,
country: string,
createdAt: string,
}
enum StatusCode {
Success = 200,
Created = 201,
BadRequest = 400,
NotFound = 404,
ServiceUnavailable = 503
}
/**
* Response - helper
* @param statusCode number
* @param message string
*/
const response: any = (statusCode: number, message: string) => ({ statusCode, body: JSON.stringify(message) });
/**
* SortByDate - helper
* @param a Player
* @param b Player
*/
const sortByDate: any = (a: Player, b: Player) => (a.createdAt > b.createdAt) ? -1 : 1;
/**
* POST /players
*/
module.exports.createPlayer = async (event: APIGatewayProxyEvent, callback: Function): Promise<APIGatewayProxyHandler> => {
const { id, name, rating, country }: Player = JSON.parse(event.body || "{}");
if (!id || !name || !rating) {
return callback(
null,
response(StatusCode.BadRequest, {
error: "Player must have an id, name and rating"
})
);
}
const player: Player = {
id,
name,
rating,
country,
createdAt: new Date().toISOString()
};
const res = await db.put({ TableName: playersTable, Item: player }).promise()
.catch((err) => response(null, response(err.statusCode, err)));
return callback(null, response(StatusCode.Created, res.Item));
};
/**
* GET /players
*/
module.exports.getPlayers = async (event: APIGatewayProxyEvent, callback: Function): Promise<APIGatewayProxyHandler> => {
const res = await db.scan({ TableName: playersTable }).promise()
.catch((err) => callback(null, response(err.statusCode, err)));
return callback(null, response(StatusCode.Success, res.Items?.sort(sortByDate)));
};
/**
* GET /player/{id}
*/
module.exports.getPlayer = async (event: APIGatewayProxyEvent, callback: Function): Promise<APIGatewayProxyHandler> => {
const { id } = event.pathParameters!;
const params = {
TableName: playersTable,
Key: {
id
}
};
const res = await db.get(params).promise()
.catch((err) => callback(null, response(err.statusCode, err)));
if (res.Item) {
return callback(null, response(StatusCode.Success, res.Item));
} else {
return callback(null, response(StatusCode.NotFound, { error: "Player not found" }));
}
};
/**
* PUT /player/{id}
*/
module.exports.updatePlayer = async (event: APIGatewayProxyEvent, callback: Function): Promise<APIGatewayProxyHandler> => {
const { id } = event.pathParameters!;
const reqBody = JSON.parse(event.body || "{}");
const { rating }: Player = reqBody;
const params = {
Key: {
id
},
TableName: playersTable,
ConditionExpression: "attribute_exists(id)",
UpdateExpression: "SET rating = :rating",
ExpressionAttributeValues: {
":rating": rating,
},
ReturnValues: "ALL_NEW"
};
const res = await db.update(params).promise()
.catch((err) => callback(null, response(err.statusCode, err)));
return callback(null, response(StatusCode.Success, res.Attributes));
};