63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
# backend/app/routes/tournaments/tournaments.py
|
|
from fastapi import Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ... import crud, schemas
|
|
from ...constants import SUCCESS
|
|
from ...core.websocket_manager import send_ws_update
|
|
from ...database import get_db
|
|
from ...core.auth import get_current_user
|
|
from . import router
|
|
|
|
|
|
@router.post("", response_model=schemas.TournamentUpdateResponse)
|
|
async def create_tournament(
|
|
data: schemas.TournamentCreate,
|
|
db: Session = Depends(get_db),
|
|
user: str = Depends(get_current_user),
|
|
):
|
|
new_t = crud.create_tournament(db, data)
|
|
await send_ws_update(new_t.id)
|
|
return new_t
|
|
|
|
|
|
@router.get("", response_model=list[schemas.TournamentOut])
|
|
def list_tournaments(db: Session = Depends(get_db)):
|
|
return crud.get_tournaments(db)
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.TournamentDetail)
|
|
def get_tournament(id: str, db: Session = Depends(get_db)):
|
|
t = crud.get_tournament(db, id)
|
|
if not t:
|
|
raise HTTPException(404, "Tournament not found")
|
|
return t
|
|
|
|
|
|
@router.patch("/{id}", response_model=schemas.TournamentUpdateResponse)
|
|
async def update_settings(
|
|
id: str,
|
|
data: schemas.TournamentUpdate,
|
|
db: Session = Depends(get_db),
|
|
user: str = Depends(get_current_user),
|
|
):
|
|
t = crud.update_tournament_details(db, id, data)
|
|
if not t:
|
|
raise HTTPException(404, "Not found")
|
|
|
|
await send_ws_update(id)
|
|
return t
|
|
|
|
|
|
@router.delete("/{id}")
|
|
async def delete_tournament(
|
|
id: str, db: Session = Depends(get_db), user: str = Depends(get_current_user)
|
|
):
|
|
success = crud.delete_tournament(db, id)
|
|
if not success:
|
|
raise HTTPException(404, "Tournament not found")
|
|
|
|
await send_ws_update(id)
|
|
|
|
return SUCCESS
|