Example #1
0
async def create_attempt(
        *,
        attempt_create: attempt_schemas.Create,
        session: Session = Depends(dal.get_session),
) -> attempt_schemas.Details:
    """ Create new attempt DB record """

    conditions = and_(Team.id == attempt_create.team_id, )

    db_team = team_ops.query_teams_by_(session, conditions).first()
    if not db_team:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Team not found",
        )

    conditions = and_(Challenge.id == attempt_create.challenge_id, )

    db_challenge = challenge_ops.query_challenges_by_(session,
                                                      conditions).first()

    if not db_challenge:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    db_attempt = attempt_ops.create_attempt(session, attempt_create)

    return db_attempt
Example #2
0
def lead_team(
        *,
        team_create: team_schemas.Create,
        db_player: User = Depends(auth_ops.get_current_user),
        session: Session = Depends(dal.get_session),
) -> player_schemas.Details:
    """ Create team and assign this player as the captain """

    # Player is not part of another team
    if db_player.team is not None:
        raise HTTPException(status.HTTP_403_FORBIDDEN,
                            detail="You are already part of a team")

    # Team name is already taken
    conditions = and_(Team.name == team_create.name, )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    if db_team is not None:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=f"The name: { team_create.name } is already taken",
        )

    db_team = team_ops.create_team(session, team_create)
    db_player = player_ops.lead_team(session, db_player, db_team)

    return db_player
Example #3
0
async def get_all_teams(
    *,
    session: Session = Depends(dal.get_session)) -> List[team_schemas.Details]:
    """ Get all team records from DB """

    db_teams = team_ops.query_teams_by_(session).all()

    return db_teams
Example #4
0
async def delete_team(*, id: int, session: Session = Depends(dal.get_session)):
    """ Delete team record from DB """

    conditions = and_(Team.id == id, )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    if db_team is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Team not found",
        )

    team_ops.delete_team(session, db_team)
Example #5
0
async def get_team_by_name(
    *, name: str, session: Session = Depends(dal.get_session)
) -> team_schemas.Details:
    """ Get team record from DB """

    conditions = and_(Team.name == name, )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    if db_team is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Team not found",
        )

    return db_team
Example #6
0
async def create_team(
        *,
        team_create: team_schemas.Create,
        session: Session = Depends(dal.get_session),
) -> team_schemas.Details:
    """ Create new team DB record """

    # Make sure unique fields are not already used
    conditions = and_(Team.name == team_create.name, )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    if db_team is not None:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=f"The name: { team_create.name } is already taken",
        )

    db_team = team_ops.create_team(session, team_create)

    return db_team
Example #7
0
async def update_team(
    *,
    id: int,
    team_update: team_schemas.Update,
    session: Session = Depends(dal.get_session)
) -> team_schemas.Details:
    """ Update team record from DB """

    conditions = and_(Team.id == id, )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    if db_team is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Team not found",
        )

    db_team = team_ops.update_team(session, db_team, team_update)

    return db_team
Example #8
0
def accept_invite(
        *,
        team_id: int,
        db_player: User = Depends(auth_ops.get_current_user),
        session: Session = Depends(dal.get_session),
) -> player_schemas.Details:
    """ Accept player invitation to join the team """

    # User is already in a team
    if db_player.team is not None:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="You are already part of a team",
        )

    conditions = and_(
        Team.id == team_id,
        Team.player_invites.any(id=db_player.id),
    )

    db_team = team_ops.query_teams_by_(session, conditions).first()

    # Team doesn't exist
    if db_team is None:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="The team is not found",
        )

    # The team is full
    if len(db_team.players) >= constants.MAX_TEAM_MEMBERS:
        raise HTTPException(
            status.HTTP_403_FORBIDDEN,
            detail="The team is full",
        )

    player_ops.accept_invite(session, db_player, db_team)