def save(self, text: str) -> None: with db.create_session() as session: session.add(db.Message(text))
def get_challenge(self, challenge_id: int) -> Optional[ContextChallenge]: with db.create_session() as session: challenge = session.query(db.Challenge).get(challenge_id) if challenge is None: return None return cast(ContextChallenge, ContextChallenge.from_orm(challenge))
def get_finished_challenge_ids(self) -> Sequence[int]: with db.create_session() as session: return cast(Sequence[int], session.query(db.Challenge).get_finished_ids())
def increment_score(self, participant_id: int) -> None: with db.create_session() as session: db_participant = session.query(db.Participant).get(participant_id) db_participant.scores += 1
def finish_participation(self, participant_id: int, finished_at: datetime.datetime) -> None: with db.create_session() as session: db_participant = session.query(db.Participant).get(participant_id) db_participant.finished_at = finished_at
def has_all_winners(self, challenge_id: int, winner_amount: int) -> bool: with db.create_session() as session: db_pretenders: Sequence[db.Participant] = session.query( db.Participant).get_sorted_pretenders( challenge_id=challenge_id, limit=winner_amount) return len(db_pretenders) == winner_amount
def finish_phase(self, result: ContextResult, finish_time: datetime) -> None: with db.create_session() as session: db_result = session.query(db.Result).get(result.id) db_result.finished_at = finish_time
def create_result(self, participant_id: int, phase: int) -> ContextResult: with db.create_session() as session: db_result = db.Result(participant_id=participant_id, phase=phase) session.add(db_result) session.flush() return cast(ContextResult, ContextResult.from_orm(db_result))
def get_user_by_nick_name(self, nick_name: str) -> Optional[ContextUser]: with db.create_session() as session: internal_user = session.query(db.User).get_by_nick_name(nick_name) if internal_user is None: return None return cast(ContextUser, ContextUser.from_orm(internal_user))
def get_user(self, user: telebot.types.User) -> Optional[ContextUser]: with db.create_session() as session: internal_user = session.query(db.User).get_by_external_id(user.id) if internal_user is None: return None return cast(ContextUser, ContextUser.from_orm(internal_user))