User stats endpoint + profile page #15

Open
opened 2026-04-19 07:23:30 +00:00 by forgejo-lila · 0 comments
Owner

Context

There is currently no way for users to see their game history or performance. The app has auth and game sessions but doesn't record results per user.

What to do

  1. Record game results per user in the database.
  2. Add an API endpoint to retrieve stats.
  3. Build a frontend profile page.

Database changes

  • New table quiz_results (already planned in spec.md as quiz_answers): id, user_id (FK to user.id), session_id, score, total_questions, game_mode, created_at.
  • Generate and run a Drizzle migration. Schema lives in packages/db/src/db/schema.ts.
  • Model functions in packages/db/src/models/.

API changes

  • New file: apps/api/src/routes/userRouter.ts — mount on /api/v1/users.
  • New endpoint: GET /api/v1/users/me/stats — requires auth. Returns games played, total score, average score, recent games.
  • New controller + service following existing layered architecture pattern (see apps/api/src/controllers/gameController.ts for reference).
  • Record results at game completion: update apps/api/src/services/gameService.ts to save results when the last question is answered.

Frontend changes

  • New route: apps/web/src/routes/profile.tsx — displays user info and stats.
  • Add profile link to navigation.
  • Use TanStack Query to fetch stats from the API.

Acceptance criteria

  • After completing a quiz, the result is saved to the database
  • GET /api/v1/users/me/stats returns accurate stats for the authenticated user
  • Profile page shows: display name, games played, average score, recent game history
  • Page is mobile-friendly (consistent with existing Tailwind + shadcn/ui styling)
  • Guest/anonymous games are NOT recorded

Notes

The GameSessionStore currently uses an in-memory store (InMemoryGameSessionStore). Game results should be written to Postgres via the model layer, not to the session store. The session store is ephemeral; stats must be durable.

## Context There is currently no way for users to see their game history or performance. The app has auth and game sessions but doesn't record results per user. ## What to do 1. Record game results per user in the database. 2. Add an API endpoint to retrieve stats. 3. Build a frontend profile page. ## Database changes - New table `quiz_results` (already planned in spec.md as `quiz_answers`): `id`, `user_id` (FK to `user.id`), `session_id`, `score`, `total_questions`, `game_mode`, `created_at`. - Generate and run a Drizzle migration. Schema lives in `packages/db/src/db/schema.ts`. - Model functions in `packages/db/src/models/`. ## API changes - New file: `apps/api/src/routes/userRouter.ts` — mount on `/api/v1/users`. - New endpoint: `GET /api/v1/users/me/stats` — requires auth. Returns games played, total score, average score, recent games. - New controller + service following existing layered architecture pattern (see `apps/api/src/controllers/gameController.ts` for reference). - Record results at game completion: update `apps/api/src/services/gameService.ts` to save results when the last question is answered. ## Frontend changes - New route: `apps/web/src/routes/profile.tsx` — displays user info and stats. - Add profile link to navigation. - Use TanStack Query to fetch stats from the API. ## Acceptance criteria - After completing a quiz, the result is saved to the database - GET /api/v1/users/me/stats returns accurate stats for the authenticated user - Profile page shows: display name, games played, average score, recent game history - Page is mobile-friendly (consistent with existing Tailwind + shadcn/ui styling) - Guest/anonymous games are NOT recorded ## Notes The `GameSessionStore` currently uses an in-memory store (`InMemoryGameSessionStore`). Game results should be written to Postgres via the model layer, not to the session store. The session store is ephemeral; stats must be durable.
forgejo-lila added the
feature
label 2026-04-19 07:23:30 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: forgejo-lila/lila#15
No description provided.