Added seperate courts
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
# backend/app/routes/courts.py
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime, date
|
||||
|
||||
from .. import crud, schemas, models
|
||||
from ..constants import SUCCESS
|
||||
from ..core.auth import get_admin_user
|
||||
from ..database import get_db
|
||||
|
||||
router = APIRouter(prefix="/courts", tags=["Courts"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[schemas.CourtSchema])
|
||||
def get_all_courts(db: Session = Depends(get_db)):
|
||||
"""Public route to list all global courts"""
|
||||
return db.query(models.Court).all()
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.CourtSchema)
|
||||
def create_global_court(
|
||||
data: schemas.CourtCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(get_admin_user),
|
||||
):
|
||||
"""Admin route to register a new physical court"""
|
||||
return crud.create_court(db, data)
|
||||
|
||||
|
||||
@router.delete("/{court_id}")
|
||||
def delete_global_court(
|
||||
court_id: int, db: Session = Depends(get_db), user: dict = Depends(get_admin_user)
|
||||
):
|
||||
success = crud.delete_court(db, court_id)
|
||||
if not success:
|
||||
raise HTTPException(404, "Court not found")
|
||||
return SUCCESS
|
||||
|
||||
|
||||
@router.get("/{court_id}/schedule")
|
||||
def get_court_schedule(court_id: int, db: Session = Depends(get_db)):
|
||||
court = db.query(models.Court).filter(models.Court.id == court_id).first()
|
||||
if not court:
|
||||
raise HTTPException(404, "Court not found")
|
||||
|
||||
today = date.today()
|
||||
|
||||
matches = (
|
||||
db.query(models.Match)
|
||||
.join(models.Tournament)
|
||||
.filter(models.Match.court_id == court_id)
|
||||
.filter(
|
||||
models.Tournament.timestamp >= datetime.combine(today, datetime.min.time())
|
||||
)
|
||||
.filter(
|
||||
models.Tournament.timestamp < datetime.combine(today, datetime.max.time())
|
||||
)
|
||||
.order_by(models.Match.start_time)
|
||||
.all()
|
||||
)
|
||||
|
||||
schedule = []
|
||||
for m in matches:
|
||||
schedule.append(
|
||||
{
|
||||
"id": m.id,
|
||||
"tournament_id": m.tournament_id,
|
||||
"tournament_name": m.tournament.name,
|
||||
"time": m.start_time.strftime("%H:%M") if m.start_time else "TBD",
|
||||
"status": m.status,
|
||||
"match_number": m.match_number,
|
||||
"p1": m.p1_team.name if m.p1_team else "TBD",
|
||||
"p2": m.p2_team.name if m.p2_team else "TBD",
|
||||
"p1_sets": sum(1 for s in m.sets if s["p1"] > s["p2"]) if m.sets else 0,
|
||||
"p2_sets": sum(1 for s in m.sets if s["p2"] > s["p1"]) if m.sets else 0,
|
||||
"ref_name": m.ref_team.name if m.ref_team else m.ref_label,
|
||||
}
|
||||
)
|
||||
|
||||
return {"court": court.name, "matches": schedule}
|
||||
@@ -3,4 +3,4 @@ from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/tournaments", tags=["Tournaments"])
|
||||
|
||||
from . import tournaments, settings, teams, courts, matches, report
|
||||
from . import tournaments, settings, teams, matches, report
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
# backend/app/routes/tournaments/courts.py
|
||||
from fastapi import Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ... import crud, schemas
|
||||
from ...constants import SUCCESS
|
||||
from ...core.auth import get_admin_user
|
||||
from ...core.websocket_manager import send_ws_update
|
||||
from ...database import get_db
|
||||
from . import router
|
||||
|
||||
|
||||
@router.get("/{id}/courts", response_model=list[schemas.CourtSchema])
|
||||
def get_courts(id: str, db: Session = Depends(get_db)):
|
||||
return crud.get_courts(db, id)
|
||||
|
||||
|
||||
@router.post("/{id}/courts", response_model=schemas.CourtSchema)
|
||||
async def create_court(
|
||||
id: str,
|
||||
court: schemas.CourtCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(get_admin_user),
|
||||
):
|
||||
new_court = crud.create_court(db, id, court)
|
||||
if not new_court:
|
||||
raise HTTPException(404, "Tournament not found")
|
||||
|
||||
await send_ws_update(id)
|
||||
return new_court
|
||||
|
||||
|
||||
@router.patch("/{id}/courts", response_model=schemas.TournamentDetail)
|
||||
async def update_courts(
|
||||
id: str,
|
||||
courts: list[str],
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(get_admin_user),
|
||||
):
|
||||
t = crud.update_tournament_courts(db, id, courts)
|
||||
if not t:
|
||||
raise HTTPException(404, "Not found")
|
||||
|
||||
await send_ws_update(id)
|
||||
return t
|
||||
|
||||
|
||||
@router.delete("/{id}/courts/{court_id}")
|
||||
async def delete_court(
|
||||
id: str,
|
||||
court_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(get_admin_user),
|
||||
):
|
||||
success = crud.delete_court(db, id, court_id)
|
||||
if not success:
|
||||
raise HTTPException(404, "Court or Tournament not found")
|
||||
|
||||
await send_ws_update(id)
|
||||
return SUCCESS
|
||||
@@ -60,3 +60,17 @@ async def delete_tournament(
|
||||
await send_ws_update(id)
|
||||
|
||||
return SUCCESS
|
||||
|
||||
|
||||
@router.patch("/{id}/courts", response_model=schemas.TournamentOut)
|
||||
def update_tournament_courts(
|
||||
id: str,
|
||||
court_ids: list[int],
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(get_admin_user),
|
||||
):
|
||||
"""Updates the global courts assigned to a specific tournament"""
|
||||
t = crud.update_tournament_courts(db, id, court_ids)
|
||||
if not t:
|
||||
raise HTTPException(404, "Tournament not found")
|
||||
return t
|
||||
|
||||
Reference in New Issue
Block a user