📝 Reflect latest changes

This commit is contained in:
Brian Wiborg 2025-09-29 17:17:54 +02:00
parent e142489ed9
commit 1b830f7bd2
No known key found for this signature in database
3 changed files with 35 additions and 14 deletions

View file

@ -30,7 +30,7 @@ It is ***blazingly fast***, extremely ***fun to use*** and comes with ***batteri
**Creating a Project** **Creating a Project**
``` ```
pip install ohmyapi pipx install ohmyapi
ohmyapi startproject myproject ohmyapi startproject myproject
cd myproject cd myproject
``` ```
@ -127,24 +127,29 @@ from ohmyapi.db.exceptions import DoesNotExist
from .models import Tournament from .models import Tournament
# Expose your app's routes via `router = fastapi.APIRouter`. # OhMyAPI will automatically pick up all instances of `fastapi.APIRouter` and
# Use prefixes wisely to avoid cross-app namespace-collisions. # add their routes to the main project router.
#
# Note:
# Use prefixes wisely to avoid cross-app namespace-collisions!
# Tags improve the UX of the OpenAPI docs at /docs. # Tags improve the UX of the OpenAPI docs at /docs.
router = APIRouter(prefix="/tournament", tags=['Tournament']) #
tournament_router = APIRouter(prefix="/tournament", tags=['Tournament'])
@router.get("/")
@tournament_router.get("/")
async def list(): async def list():
queryset = Tournament.all() queryset = Tournament.all()
return await Tournament.Schema.model.from_queryset(queryset) return await Tournament.Schema.model.from_queryset(queryset)
@router.post("/", status_code=HTTPStatus.CREATED) @tournament_router.post("/", status_code=HTTPStatus.CREATED)
async def post(tournament: Tournament.Schema.readonly): async def post(tournament: Tournament.Schema.readonly):
queryset = Tournament.create(**payload.model_dump()) queryset = Tournament.create(**payload.model_dump())
return await Tournament.Schema.model.from_queryset(queryset) return await Tournament.Schema.model.from_queryset(queryset)
@router.get("/:id") @tournament_router.get("/:id")
async def get(id: str): async def get(id: str):
try: try:
queryset = Tournament.get(id=id) queryset = Tournament.get(id=id)
@ -152,6 +157,16 @@ async def get(id: str):
except DoesNotExist: except DoesNotExist:
raise HTTPException(status_code=404, detail="not found") raise HTTPException(status_code=404, detail="not found")
@tournament_router.delete("/:id")
async def delete(id: str):
try:
tournament = await Tournament.get(id=id)
return await Tournament.Schema.model.from_queryset(tournament.delete())
except DoesNotExist:
raise HTTPException(status_code=404, detail="not found")
... ...
``` ```

View file

@ -211,7 +211,7 @@ class App:
self.model_modules: List[str] = [] self.model_modules: List[str] = []
# The APIRouter # The APIRouter
self.router: Optional[APIRouter] = None self.router: APIRouter = APIRouter()
# Import the app, so its __init__.py runs. # Import the app, so its __init__.py runs.
importlib.import_module(self.name) importlib.import_module(self.name)
@ -226,9 +226,12 @@ class App:
# Locate the APIRouter # Locate the APIRouter
try: try:
routes_mod = importlib.import_module(f"{self.name}.routes") routes_mod = importlib.import_module(f"{self.name}.routes")
router = getattr(routes_mod, "router", None) for attr_name in dir(routes_mod):
if isinstance(router, APIRouter): if attr_name.startswith("__"):
self.router = router continue
attr = getattr(routes_mod, attr_name)
if isinstance(attr, APIRouter):
self.router.include_router(attr)
except ModuleNotFoundError: except ModuleNotFoundError:
pass pass

View file

@ -4,13 +4,16 @@ from . import models
from typing import List from typing import List
# Expose your app's routes via `router = fastapi.APIRouter`. # OhMyAPI will automatically pick up all instances of `fastapi.APIRouter` and
# Use prefixes wisely to avoid cross-app namespace-collisions. # add their routes to the main project router.
#
# Note:
# Use prefixes wisely to avoid cross-app namespace-collisions!
# Tags improve the UX of the OpenAPI docs at /docs. # Tags improve the UX of the OpenAPI docs at /docs.
#
router = APIRouter(prefix="/{{ app_name }}", tags=['{{ app_name }}']) router = APIRouter(prefix="/{{ app_name }}", tags=['{{ app_name }}'])
@router.get("/") @router.get("/")
async def list(): async def list():
"""List all ...""" """List all ..."""