-
Notifications
You must be signed in to change notification settings - Fork 1
/
StatsApiImpl.ts
143 lines (140 loc) · 5.21 KB
/
StatsApiImpl.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
import { ExpressParameters } from '@oats-ts/openapi-http-server/lib/express';
import { fluent } from '@oats-ts/try';
import { StatsServiceApi } from './generated-oats/api/StatsServiceApi';
import { Issue, stringify } from '@oats-ts/validators';
import { GetNumberOfRoundsByPokemonServerRequest } from './generated-oats/requests/GetNumberOfRoundsByPokemonServerRequest';
import { GetNumberOfVictoriesByPokemonServerRequest } from './generated-oats/requests/GetNumberOfVictoriesByPokemonServerRequest';
import { GetRoundsAdayLast30DaysResponse } from './generated-oats/responses/GetRoundsAdayLast30DaysResponse';
import { GetNumberOfRoundsByPokemonResponse } from './generated-oats/responses/GetNumberOfRoundsByPokemonResponse';
import { GetNumberOfVictoriesByPokemonResponse } from './generated-oats/responses/GetNumberOfVictoriesByPokemonResponse';
import { GetPokemonsWithNumberOfRoundsResponse } from './generated-oats/responses/GetPokemonsWithNumberOfRoundsResponse';
import { UploadStatRowServerRequest } from './generated-oats/requests/UploadStatRowServerRequest';
import { UploadStatRowResponse } from './generated-oats/responses/UploadStatRowResponse';
import { PrismaClient } from '@prisma/client/stats';
import { AppError } from './generated-oats/types/AppError';
import { ArrayOfRounds } from './generated-oats/types/ArrayOfRounds';
export class StatsApiImpl implements StatsServiceApi<ExpressParameters> {
prisma;
constructor() {
this.prisma = new PrismaClient();
}
uploadStatRow(request: UploadStatRowServerRequest): Promise<UploadStatRowResponse> {
return fluent(request.body).get(
async (body): Promise<UploadStatRowResponse> => {
if (!body) {
return {
body: null,
statusCode: 400,
headers: undefined,
mimeType: 'application/json',
};
}
return {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
body: await this.prisma.statRound.upsert({
where: {
idPokemon_dateMatch_idMatch_team: {
dateMatch: body.dateMatch,
idMatch: body.idMatch,
idPokemon: body.idPokemon,
team: body.team,
},
},
update: body,
create: body,
}),
statusCode: 200,
headers: undefined,
mimeType: 'application/json',
};
},
async (issues: Issue[]): Promise<UploadStatRowResponse> => ({
body: issues.map(stringify).map((message): AppError => ({ message, code: 0 })),
headers: undefined,
mimeType: 'application/json',
statusCode: 400,
}),
);
}
async getRoundsAdayLast30Days(): Promise<GetRoundsAdayLast30DaysResponse> {
return {
body: await this.prisma.$queryRaw<ArrayOfRounds>`
SELECT
strftime('%d-%m-%Y', dateMatch / 1000, 'unixepoch') as date,
COUNT(*) as numberOfRounds
FROM
'StatRound'
WHERE
dateMatch >= CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER) - 1000 * 60 * 60 * 24 * 30
GROUP BY
date;
`,
statusCode: 200,
headers: undefined,
mimeType: 'application/json',
};
}
async getNumberOfRoundsByPokemon(request: GetNumberOfRoundsByPokemonServerRequest): Promise<GetNumberOfRoundsByPokemonResponse> {
return fluent(request.path).get(
async ({ id }): Promise<GetNumberOfRoundsByPokemonResponse> => ({
body: {
id,
numberOfRounds: await this.prisma.statRound.count({
where: {
idPokemon: id,
},
}),
},
headers: undefined,
mimeType: 'application/json',
statusCode: 200,
}),
async (issues: Issue[]): Promise<GetNumberOfRoundsByPokemonResponse> => ({
body: issues.map(stringify).map((message): AppError => ({ message, code: 0 })),
headers: undefined,
mimeType: 'application/json',
statusCode: 400,
}),
);
}
async getNumberOfVictoriesByPokemon(request: GetNumberOfVictoriesByPokemonServerRequest): Promise<GetNumberOfVictoriesByPokemonResponse> {
return fluent(request.path).get(
async ({ id }): Promise<GetNumberOfVictoriesByPokemonResponse> => ({
body: {
id,
numberOfVictories: await this.prisma.statRound.count({
where: {
idPokemon: id,
victory: true,
},
}),
},
headers: undefined,
mimeType: 'application/json',
statusCode: 200,
}),
async (issues: Issue[]): Promise<GetNumberOfVictoriesByPokemonResponse> => ({
body: issues.map(stringify).map((message): AppError => ({ message, code: 0 })),
headers: undefined,
mimeType: 'application/json',
statusCode: 400,
}),
);
}
async getPokemonsWithNumberOfRounds(): Promise<GetPokemonsWithNumberOfRoundsResponse> {
return {
body: (
await this.prisma.statRound.groupBy({
by: ['idPokemon'],
_count: {
_all: true,
},
})
).map(({ idPokemon, _count }) => ({ id: idPokemon, numberOfRounds: _count._all })),
headers: undefined,
mimeType: 'application/json',
statusCode: 200,
};
}
}