This part of the 42 Transcendence Project covers the Gamestats API, which handles the storage and retrieval of finished game results.
Before using the gamestats
API, make sure the backend container is correctly prepared.
Run:
make test15
This will ensure the environment is correctly built, migrations are applied, and the backend is up.
curl -X GET http://localhost:8000/api/gamestats/
β Response (empty):
[]
β Response (with data):
[
{
"game_id": 1,
"player1": 1,
"player2": 2,
"player1_username": "Alice",
"player2_username": "Bob",
"player1_score": 10,
"player2_score": 7,
"winner": 1,
"created_at": "2025-02-27T14:23:15.123456Z"
}
]
curl -X POST http://localhost:8000/api/gamestats/ \
-H "Content-Type: application/json" \
-d '{
"player1": 1,
"player2": 2,
"player1_username": "Alice",
"player2_username": "Bob",
"player1_score": 10,
"player2_score": 7,
"winner": 1
}'
β Response (created):
{
"game_id": 1,
"player1": 1,
"player2": 2,
"player1_username": "Alice",
"player2_username": "Bob",
"player1_score": 10,
"player2_score": 7,
"winner": 1,
"created_at": "2025-02-27T14:23:15.123456Z"
}
curl -X GET http://localhost:8000/api/gamestats/1/
curl -X DELETE http://localhost:8000/api/gamestats/1/
sequenceDiagram
participant Client
participant Backend
participant Database
Client->>Backend: POST /api/gamestats/ (save finished match)
Backend->>Database: Save players, scores & winner
Backend-->>Client: Return saved data (JSON)
Client->>Backend: GET /api/gamestats/
Backend->>Database: Fetch all saved matches
Backend-->>Client: Return array of matches (JSON)
Client->>Backend: DELETE /api/gamestats/1/
Backend->>Database: Remove the record
Backend-->>Client: HTTP 204 No Content
# Make sure everything is up and running
make test15
# Check if gamestats API works
curl -X GET http://localhost:8000/api/gamestats/
Wenn du die API gerne ΓΌber Postman oder Insomnia testen willst, kannst du diese cURL-Befehle direkt importieren!
docker exec
for migrations inside the backend containerdocker exec -it ft_transcendence-backend-1 python manage.py makemigrations gamestats
docker exec -it ft_transcendence-backend-1 python manage.py migrate