45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from typing import Any, Dict, Union, Optional
|
|
|
|
from sqlmodel import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud import player as crud_player
|
|
from app.crud.base import CRUDBase
|
|
from app.models.matchday import Matchday, MatchdayCreate, MatchdayUpdate
|
|
|
|
|
|
class CRUDMatchday(CRUDBase[Matchday, MatchdayCreate, MatchdayUpdate]):
|
|
|
|
def create(self, db: Session, *, obj_in: MatchdayCreate) -> Matchday:
|
|
db_obj = Matchday(
|
|
day=obj_in.day)
|
|
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def get_unique_day(self, db: Session, *, obj_in: Matchday) -> Any:
|
|
day = obj_in.day
|
|
statement = select(Matchday).where(Matchday.day == day)
|
|
result = db.execute(statement).all()
|
|
return result
|
|
|
|
def add_player_in_matchday(self, db: Session, matchday_id: int, player_id: int
|
|
) -> Matchday:
|
|
matchday_in = self.get(db=db, id=matchday_id)
|
|
db_player = crud_player.get(db=db, id=player_id)
|
|
matchday_in.players.append(db_player)
|
|
db.commit()
|
|
return matchday_in
|
|
|
|
def is_player_in_matchday(
|
|
self, db: Session, *, player_id: int, matchday_id: int
|
|
) -> bool:
|
|
matchday = self.get(db=db, id=matchday_id)
|
|
if matchday is None:
|
|
return False
|
|
db_player = crud_player.get(db=db, id=player_id)
|
|
return db_player in matchday.players
|
|
|
|
matchday = CRUDMatchday(Matchday) |