diff --git a/server.ts b/server.ts index 946fa07..337a3b3 100644 --- a/server.ts +++ b/server.ts @@ -187,6 +187,25 @@ memberRouter.post("/games/:gameId", async (ctx) => { : ``; }); +memberRouter.post("/games/:gameId/switch", async (ctx) => { + if (!(await loginService.isLoggedIn(ctx))) { + ctx.response.redirect("/membre"); + return; + } + const success = await gameService.switchVisitors(ctx.params.gameId); + if (!success) { + ctx.response.body = ``; + } + const template = await env.load("templates/partials/single-game-editor.vto"); + const game = await gameService.getGameById(ctx.params.gameId); + const result = await template({ + tab: "membre", + game: game, + }); + ctx.response.headers.set("game-id", game?.id ?? ""); + ctx.response.body = result.content; +}); + const apiRouter = new Router(); apiRouter.get("/results/:year.json", async (ctx) => { diff --git a/server/game-service.ts b/server/game-service.ts index 8780fe3..e7dc256 100644 --- a/server/game-service.ts +++ b/server/game-service.ts @@ -173,6 +173,23 @@ export class GameService { return res.ok; } + async switchVisitors(id: string): Promise { + const game = await this.getGameById(id); + if (game === null) { + return false; + } + delete game.id; + const [home, away] = [game.home, game.away]; + const [homeScore, awayScore] = [game.homeScore, game.awayScore]; + game.home = away; + game.away = home; + game.homeScore = awayScore; + game.awayScore = homeScore; + console.log(`game ${game}`); + + return this.updateGame(id, game); + } + async updateGame(id: string, partial: Partial): Promise { delete partial.id; const precond = await this.kv.get(this.gamePrimaryKey(id)); diff --git a/templates/includes/game-editor.vto b/templates/includes/game-editor.vto index 0de3527..598de98 100644 --- a/templates/includes/game-editor.vto +++ b/templates/includes/game-editor.vto @@ -58,6 +58,10 @@
+