115 lines
3.4 KiB
Python
115 lines
3.4 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
|
|
from app.models.matchday import Matchday, MatchdayCreate, MatchdayUpdate, \
|
|
MatchdayWithPlayers
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/{matchday_id}", response_model=MatchdayWithPlayers)
|
|
def read_matchday(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
matchday_id: int,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get matchday by firstname and lastname.
|
|
"""
|
|
matchday = crud.matchday.get(
|
|
db=db, id=matchday_id
|
|
)
|
|
if not matchday:
|
|
raise HTTPException(status_code=404, detail="matchday not found")
|
|
if not crud.user.is_superuser(current_user):
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
return matchday
|
|
|
|
|
|
@router.post("/", response_model=Matchday)
|
|
def create_matchday(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
matchday_in: MatchdayCreate,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new user.
|
|
"""
|
|
matchday = crud.matchday.get_unique_day(db, obj_in=matchday_in)
|
|
if matchday:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"The matchday with this day already exists in the "
|
|
f"system. Matchday is {matchday}",
|
|
)
|
|
matchday = crud.matchday.create(db, obj_in=matchday_in)
|
|
|
|
return matchday
|
|
|
|
|
|
@router.get("/", response_model=List[MatchdayWithPlayers])
|
|
def get_matchdays(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Retrieve all matchdays.
|
|
"""
|
|
matchday = crud.matchday.get_multi(db, skip=skip, limit=limit)
|
|
return matchday
|
|
|
|
|
|
@router.post("/{matchday_id}", response_model=Matchday)
|
|
def update_matchday(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
matchday_id: int,
|
|
matchday_in: MatchdayUpdate,
|
|
current_user: User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update matchday.
|
|
"""
|
|
|
|
matchday = crud.matchday.get(db=db, id=matchday_id)
|
|
if not matchday:
|
|
raise HTTPException(status_code=404, detail="Matchday not found")
|
|
|
|
matchday = crud.matchday.update(db=db, db_obj=matchday, obj_in=matchday_in)
|
|
return matchday
|
|
|
|
|
|
@router.put("/players/{matchday_id}", response_model=Matchday)
|
|
def add_player_matchday(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
player_id: int,
|
|
matchday_id: int,
|
|
current_user: User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Add player to matchday.
|
|
"""
|
|
if crud.player.get(db, id=player_id) is None:
|
|
raise HTTPException(status_code=404, detail="Player not found")
|
|
if crud.matchday.get(db=db, id=matchday_id) is None:
|
|
raise HTTPException(status_code=404, detail="Matchday not found")
|
|
if crud.matchday.is_player_in_matchday(
|
|
db=db, player_id=player_id, matchday_id=matchday_id
|
|
):
|
|
raise HTTPException(
|
|
status_code=404, detail="Player is already in team"
|
|
)
|
|
matchday = crud.matchday.add_player_in_matchday(db, matchday_id, player_id)
|
|
return matchday
|