40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from typing import Any, Dict, Optional, Union, List
|
|
|
|
from sqlalchemy.orm import Session
|
|
from sqlmodel import select
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.crud import player as crud_player
|
|
from app.models.team import Team, TeamCreate, TeamUpdate
|
|
|
|
|
|
class CRUDTeam(CRUDBase[Team, TeamCreate, TeamUpdate]):
|
|
|
|
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_in = self.get(db=db, id=team_id)
|
|
db_player = crud_player.get(db=db, id=player_id)
|
|
team_in.players.append(db_player)
|
|
db.commit()
|
|
return team_in
|
|
|
|
def is_player_in_team(
|
|
self, db: Session, *, player_id: int, team_id: int
|
|
) -> bool:
|
|
team = self.get(db=db, id=team_id)
|
|
if team is None:
|
|
return False
|
|
db_player = crud_player.get(db=db, id=player_id)
|
|
return db_player in team.players
|
|
|
|
|
|
team = CRUDTeam(Team)
|