diff --git a/leaky-ships/lib/backend/sendResponse.ts b/leaky-ships/lib/backend/sendResponse.ts index d57a729..cc24cc5 100644 --- a/leaky-ships/lib/backend/sendResponse.ts +++ b/leaky-ships/lib/backend/sendResponse.ts @@ -6,6 +6,7 @@ export interface Result { statusCode?: number body?: T type?: Logging[] + redirectUrl?: string } export default function sendResponse( @@ -13,8 +14,12 @@ export default function sendResponse( res: NextApiResponse, result: Result ) { - res.status(result.statusCode ?? 200) - result.body ? res.json(result.body) : res.end() - logging(result.message, result.type ?? ["debug"], req) + if (result.redirectUrl) { + res.redirect(result.statusCode ?? 307, result.redirectUrl) + } else { + res.status(result.statusCode ?? 200) + result.body ? res.json(result.body) : res.end() + logging(result.message, result.type ?? ["debug"], req) + } return "done" as const } diff --git a/leaky-ships/pages/api/game/create.ts b/leaky-ships/pages/api/game/create.ts index bbcdd14..6c1a2fa 100644 --- a/leaky-ships/pages/api/game/create.ts +++ b/leaky-ships/pages/api/game/create.ts @@ -26,8 +26,12 @@ export default async function create( let created = false let game = await getAnyRunningGame(id) - if (!game) { - created = true + if (game) { + return sendResponse(req, res, { + redirectUrl: "/api/game/running", + message: "Running game already exists.", + }) + } else { game = await prisma.game.create({ data: { gamePin: {