86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from backend.app import crud
|
|
from backend.app.api import deps
|
|
from backend.app.models.team import Team, TeamCreate, TeamWithPlayers
|
|
from backend.app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/{team_id}", response_model=Team)
|
|
def get_team(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
team_id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get team by id.
|
|
"""
|
|
team = crud.team.get_team(db=db, team_id=team_id)
|
|
if not team:
|
|
raise HTTPException(status_code=404, detail="player not found")
|
|
if not crud.user.is_superuser(current_user):
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
return team
|
|
|
|
|
|
@router.get("/", response_model=List[TeamWithPlayers])
|
|
def get_teams(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Retrieve teams.
|
|
"""
|
|
team = crud.team.get_multi(db, skip=skip, limit=limit)
|
|
return team
|
|
|
|
|
|
@router.post("/", response_model=Team)
|
|
def create_team(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
team_in: TeamCreate,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create team.
|
|
"""
|
|
|
|
team = crud.team.create(db, obj_in=team_in)
|
|
|
|
return team
|
|
|
|
|
|
@router.put("/players/{team_id}", response_model=Team)
|
|
def add_player_team(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
player_id: int,
|
|
team_id: int,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Add player to team.
|
|
"""
|
|
if crud.player.get(db, id=player_id) is None:
|
|
raise HTTPException(status_code=404, detail="Player not found")
|
|
if crud.team.get(db=db, id=team_id) is None:
|
|
raise HTTPException(status_code=404, detail="Team not found")
|
|
if crud.team.is_player_in_team(
|
|
db=db, player_id=player_id, team_id=team_id
|
|
):
|
|
raise HTTPException(
|
|
status_code=404, detail="Player is already in team"
|
|
)
|
|
team = crud.team.add_player_in_team(db, team_id, player_id)
|
|
return team
|