From de8bdddbb08621021449cbff2b3f7f3d39143ea9 Mon Sep 17 00:00:00 2001 From: Aotokitsuruya Date: Thu, 23 Nov 2023 21:16:30 +0800 Subject: [PATCH] Refactor puzzle dashboard to OpenAPI route --- worker/controller/puzzleDashboard.ts | 17 +++++++++++++---- worker/index.ts | 6 +----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/worker/controller/puzzleDashboard.ts b/worker/controller/puzzleDashboard.ts index 51867f0..667f062 100644 --- a/worker/controller/puzzleDashboard.ts +++ b/worker/controller/puzzleDashboard.ts @@ -1,16 +1,25 @@ import { IRequest } from 'itty-router' import { json } from '@worker/utils' import * as schema from '@api/schema' -import { get } from '@worker/router' +import { Get } from '@worker/router' import { GetPuzzleStats } from '@api/query' +import { OpenAPIRoute, OpenAPIRouteSchema } from '@cloudflare/itty-router-openapi' export type PuzzleStatsRequest = { getPuzzleStats: GetPuzzleStats } & IRequest -export class PuzzleDashboard { - @get('/event/puzzle/dashboard') - async getStats({ getPuzzleStats, query }: PuzzleStatsRequest) { +@Get('/event/puzzle/dashboard') +export class DisplayPuzzleStats extends OpenAPIRoute { + static schema: OpenAPIRouteSchema = { + description: 'Get puzzle stats', + tags: ['Puzzle'], + parameters: { + event_id: schema.EventIdQuery, + }, + } + + async handle({ getPuzzleStats, query }: PuzzleStatsRequest) { const stats = await getPuzzleStats.execute({ eventId: query.event_id as string }) return json([ diff --git a/worker/index.ts b/worker/index.ts index 6912e3c..6ec6308 100644 --- a/worker/index.ts +++ b/worker/index.ts @@ -2,7 +2,7 @@ import { withParams } from 'itty-router' import { OpenAPIRouter } from '@cloudflare/itty-router-openapi' import { withCommands, withTestability, withQueries } from '@worker/middlewares' import { setup } from '@worker/router' -import * as Controller from '@worker/controller' +import '@worker/controller' const router = OpenAPIRouter({ schema: { @@ -21,8 +21,4 @@ const router = OpenAPIRouter({ router.all('*', withParams).all('*', withCommands).all('*', withQueries).all('*', withTestability) -const _Controllers = [Controller.PuzzleDashboard] - -_Controllers.forEach((Controller: new () => object) => new Controller()) - export default setup(router)