feat(api): add REST endpoints for lobby create and join
- POST /api/v1/lobbies creates a lobby with a Crockford-Base32 6-char code, retrying on unique violation up to 5 times - POST /api/v1/lobbies/:code/join validates lobby state then calls the model's atomic addPlayer, idempotent for repeat joins from the same user - Routes require authentication via requireAuth
This commit is contained in:
parent
8c241636bf
commit
4d1ebe2450
4 changed files with 123 additions and 0 deletions
37
apps/api/src/controllers/lobbyController.ts
Normal file
37
apps/api/src/controllers/lobbyController.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import type { Request, Response, NextFunction } from "express";
|
||||
import { createLobby, joinLobby } from "../services/lobbyService.js";
|
||||
|
||||
export const createLobbyHandler = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
try {
|
||||
const userId = req.session!.user.id;
|
||||
const lobby = await createLobby(userId);
|
||||
res.json({ success: true, data: lobby });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const joinLobbyHandler = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
try {
|
||||
const userId = req.session!.user.id;
|
||||
const code = req.params["code"];
|
||||
if (!code) {
|
||||
return next(new Error("Missing code param"));
|
||||
}
|
||||
if (typeof code !== "string") {
|
||||
return next(new Error("Missing or invalid code param"));
|
||||
}
|
||||
const lobby = await joinLobby(code, userId);
|
||||
res.json({ success: true, data: lobby });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue