floorball_stas/backend/app/api/api_v1/endpoints/match.py

84 lines
2.0 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.match import Match, MatchCreate, MatchUpdate
from backend.app.models.user import User
router = APIRouter()
@router.get("/{match_id}", response_model=Match)
def read_match(
*,
db: Session = Depends(deps.get_db),
match_id: int,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get match by id.
"""
match = crud.match.get(
db=db, id=match_id
)
if not match:
raise HTTPException(status_code=404, detail="match not found")
if not crud.user.is_superuser(current_user):
raise HTTPException(status_code=400, detail="Not enough permissions")
return match
@router.post("/", response_model=Match)
def create_match(
*,
db: Session = Depends(deps.get_db),
match_in: MatchCreate,
current_user: User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Create new user.
"""
match = crud.match.create(db, obj_in=match_in)
return match
@router.get("/", response_model=List[Match])
def get_matchs(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Retrieve all matchs.
"""
match = crud.match.get_multi(db, skip=skip, limit=limit)
return match
@router.post("/{match_id}", response_model=Match)
def update_match(
*,
db: Session = Depends(deps.get_db),
match_id: int,
match_in: MatchUpdate,
current_user: User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update match.
"""
match = crud.match.get(db=db, id=match_id)
if not match:
raise HTTPException(status_code=404, detail="Match not found")
match = crud.match.update(db=db, db_obj=match, obj_in=match_in)
return match