-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
98 lines (88 loc) · 2.78 KB
/
app.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
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { DataService, queryResultType } from './dataService';
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
const { requestContext } = event;
const { method: httpMethod, path } = (requestContext as any).http;
const dataService = new DataService();
try {
let data;
if (httpMethod === 'GET' && path === '/init') {
data = initDB(dataService);
} else if (httpMethod === 'GET' && path === '/version') {
data = getTiDBVersion(dataService);
} else if (httpMethod === 'GET' && path === '/player') {
const id = Number(event.queryStringParameters?.id);
data = getPlayer(dataService, id);
} else if (httpMethod === 'POST' && path === '/player') {
const { coins, goods } = JSON.parse(event.body || '{}');
data = createPlayer(dataService, coins, goods);
} else if (httpMethod === 'PUT' && path === '/player') {
const { id, coins, goods } = JSON.parse(event.body || '{}');
data = updatePlayer(dataService, id, coins, goods);
} else if (httpMethod === 'DELETE' && path === '/player') {
const id = Number(event.queryStringParameters?.id);
data = deletePlayer(dataService, id);
}
if (data) {
const { results } = (await data) as queryResultType;
return wrapResponse(results);
}
const { results } = (await dataService.singleQuery(
'SELECT "Hello World";'
)) as queryResultType;
return wrapResponse(results);
} catch (err: any) {
console.error(err);
return {
statusCode: 500,
body: JSON.stringify({
error: err,
}),
};
}
};
const wrapResponse = (results: queryResultType) => {
return {
statusCode: 200,
body: JSON.stringify({
results,
}),
};
};
const initDB = async (service: DataService) => {
await service.createTable();
const results = await service.insert();
return results;
};
const getTiDBVersion = async (service: DataService) => {
const results = await service.getTiDBVersion();
return results;
};
const getPlayer = async (service: DataService, id: number) => {
const results = await service.getPlayerByID(id);
return results;
};
const createPlayer = async (
service: DataService,
coins: number,
goods: number
) => {
const results = await service.createPlayer(coins, goods);
return results;
};
const updatePlayer = async (
service: DataService,
id: number,
coins: number,
goods: number
) => {
const results = await service.updatePlayer(id, coins, goods);
return results;
};
const deletePlayer = async (service: DataService, id: number) => {
const results = await service.deletePlayerByID(id);
return results;
};