feat: scores
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
from django.conf import settings
|
||||
from ninja import NinjaAPI
|
||||
from ninja.security import APIKeyHeader
|
||||
|
||||
|
||||
class SecretKeyAuth(APIKeyHeader):
|
||||
param_name = "X-Secret-Key"
|
||||
|
||||
def authenticate(self, request, key):
|
||||
return key if key == settings.API_SECRET else None
|
||||
|
||||
|
||||
api = NinjaAPI(auth=SecretKeyAuth())
|
||||
|
||||
from scores.api import router as scores_router # noqa: E402
|
||||
|
||||
api.add_router("/scores", scores_router)
|
||||
@@ -37,8 +37,11 @@ INSTALLED_APPS = [
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"scores",
|
||||
]
|
||||
|
||||
API_SECRET = "selecta-secret"
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
|
||||
@@ -18,6 +18,9 @@ Including another URLconf
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
|
||||
from app.api import api
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("api/", api.urls),
|
||||
]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,34 @@
|
||||
from django.http import HttpRequest
|
||||
from ninja import Router, Schema
|
||||
from ninja.pagination import PageNumberPagination, paginate
|
||||
|
||||
from .models import Score
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
class ScoreIn(Schema):
|
||||
username: str
|
||||
points: int
|
||||
|
||||
|
||||
class ScoreOut(Schema):
|
||||
id: int
|
||||
username: str
|
||||
points: int
|
||||
created_at: str
|
||||
|
||||
@staticmethod
|
||||
def resolve_created_at(obj: Score) -> str:
|
||||
return obj.created_at.isoformat()
|
||||
|
||||
|
||||
@router.post("", response=ScoreOut)
|
||||
def submit_score(request: HttpRequest, payload: ScoreIn) -> Score:
|
||||
return Score.objects.create(username=payload.username, points=payload.points)
|
||||
|
||||
|
||||
@router.get("", response=list[ScoreOut])
|
||||
@paginate(PageNumberPagination, page_size=20)
|
||||
def list_scores(request: HttpRequest) -> list[Score]:
|
||||
return Score.objects.all()
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ScoresConfig(AppConfig):
|
||||
name = "scores"
|
||||
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 6.0.6 on 2026-06-11 22:22
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Score",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("username", models.CharField(max_length=100)),
|
||||
("points", models.IntegerField()),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
],
|
||||
options={
|
||||
"ordering": ["-points"],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Score(models.Model):
|
||||
username = models.CharField(max_length=100)
|
||||
points = models.IntegerField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-points"]
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user