floorball_stas/app/crud/crud_team.py

48 lines
1.5 KiB
Python

from typing import Any, Dict, Optional, Union, List
from sqlalchemy.orm import Session
from app.crud.base import CRUDBase
from app.crud import player as crud_player
from app.models.team import Team
from app.schemas.team import TeamCreate, TeamUpdate
class CRUDTeam(CRUDBase[Team, TeamCreate, TeamUpdate]):
def get_team(self, db: Session, *, team_id: int) -> Optional[Team]:
return db.query(Team).filter(Team.team_id == team_id).first()
def get_teams(
self, db: Session, skip: int = 0, limit: int = 100
) -> List[Team]:
return db.query(Team).offset(skip).limit(limit).all()
def create(self, db: Session, *, obj_in: TeamCreate) -> Team:
db_obj = Team(teamname=obj_in.teamname)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def add_player_in_team(
self, db: Session, team_id: int, player_id: int
) -> Team:
team = self.get_team(db=db, team_id=team_id)
db_player = crud_player.get_player(db=db, player_id=player_id)
team.players.append(db_player)
db.commit()
return team
def is_player_in_team(
self, db: Session, *, player_id: int, team_id: int
) -> bool:
team = self.get_team(db=db, team_id=team_id)
if team is None:
return False
db_player = crud_player.get_player(db=db, player_id=player_id)
return db_player in team.players
team = CRUDTeam(Team)