🍱 Add ohmyapi_demo

This commit is contained in:
Brian Wiborg 2025-09-28 19:26:37 +02:00
parent 90f257ae38
commit 61ef27936c
No known key found for this signature in database
3 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,2 @@
from . import models
from . import routes

View file

@ -0,0 +1,50 @@
from ohmyapi.db import Model, field
from ohmyapi_auth.models import User
from datetime import datetime
from decimal import Decimal
from uuid import UUID
class Team(Model):
id: UUID = field.data.UUIDField(primary_key=True)
name: str = field.TextField()
members: field.ManyToManyRelation[User] = field.ManyToManyField(
'ohmyapi_auth.User',
related_name="tournament_teams",
through='user_tournament_teams',
)
def __str__(self):
return self.name
class Tournament(Model):
id: UUID = field.data.UUIDField(primary_key=True)
name: str = field.TextField()
created: datetime = field.DatetimeField(auto_now_add=True)
def __str__(self):
return self.name
class Event(Model):
id: UUID = field.data.UUIDField(primary_key=True)
name: str = field.TextField()
tournament: field.ForeignKeyRelation[Tournament] = field.ForeignKeyField(
'ohmyapi_demo.Tournament',
related_name='events',
)
participants: field.ManyToManyRelation[Team] = field.ManyToManyField(
'ohmyapi_demo.Team',
related_name='events',
through='event_team',
)
modified: datetime = field.DatetimeField(auto_now=True)
prize: Decimal = field.DecimalField(max_digits=10, decimal_places=2, null=True)
class Schema:
exclude = ['tournament_id']
def __str__(self):
return self.name

View file

@ -0,0 +1,54 @@
from ohmyapi.router import APIRouter, HTTPException, HTTPStatus
from ohmyapi.db.exceptions import DoesNotExist
from . import models
from typing import List
# Expose your app's routes via `router = fastapi.APIRouter`.
# Use prefixes wisely to avoid cross-app namespace-collisions.
# Tags improve the UX of the OpenAPI docs at /docs.
router = APIRouter(prefix="/tournemant")
@router.get("/",
tags=["tournament"],
response_model=List[models.Tournament.Schema.model])
async def list():
"""List all tournaments."""
return await models.Tournament.Schema.model.from_queryset(Tournament.all())
@router.post("/",
tags=["tournament"],
status_code=HTTPStatus.CREATED)
async def post(tournament: models.Tournament.Schema.readonly):
"""Create tournament."""
return await models.Tournament.Schema.model.from_queryset(models.Tournament.create(**tournament.model_dump()))
@router.get("/{id}",
tags=["tournament"],
response_model=models.Tournament.Schema.model)
async def get(id: str):
"""Get tournament by id."""
return await models.Tournament.Schema.model.from_queryset(models.Tournament.get(id=id))
@router.put("/{id}",
tags=["tournament"],
response_model=models.Tournament.Schema.model,
status_code=HTTPStatus.ACCEPTED)
async def put(tournament: models.Tournament.Schema.model):
"""Update tournament."""
return await models.Tournament.Schema.model.from_queryset(models.Tournament.update(**tournament.model_dump()))
@router.delete("/{id}", tags=["tournament"])
async def delete(id: str):
try:
tournament = await models.Tournament.get(id=id)
return await tournament.delete()
except DoesNotExist:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="not found")