Exemplo n.º 1
0
async def create_league(
    league: League, current_user: User = Depends(get_current_active_user)
):
    exists = await verify_exists(league)
    if not exists:
        operation = "insert"
        data = json.loads(league.json())
        fixed_id = verify_id(league)
        if fixed_id:
            database_obj = await run(operation, data)
        else:
            data.pop("id")

        payload = {"database": DATABASE, "table": TABLE, "data": data}
        database_obj = await run(operation, payload)
        if database_obj.get("status_code") != 200:
            raise HTTPException(
                status_code=500,
                detail=f"Database couldn't create the object. Check the database connection and parameters. Traceback: {database_obj.get('response_message')}",
            )
        else:
            if not fixed_id:
                data.update(
                    {
                        "id": database_obj.get("response_message").get(
                            "generated_keys"
                        )[0]
                    }
                )
            return League.parse_obj(data)
    else:
        raise HTTPException(
            status_code=403, detail=f"Object already exists on database."
        )
Exemplo n.º 2
0
async def create_participation(
        participation: Participation,
        race: str,
        current_user: User = Depends(get_current_active_user),
):
    race_exist = await verify_exists_by_id(race, DATABASE, RACE_TABLE)
    if not race_exist:
        raise HTTPException(status_code=404,
                            detail=f"The Race with ID {race} doesn't exist")

    driver_exist = await verify_exists_by_id(str(participation.driver),
                                             DATABASE, DRIVER_TABLE)
    if not driver_exist:
        raise HTTPException(
            status_code=404,
            detail=f"The Driver with ID {participation.driver} doesn't exist",
        )
    exists = await verify_exists(participation)
    if not exists:
        operation = "insert"
        data = json.loads(participation.json())
        fixed_id = verify_id(participation)
        if fixed_id:
            database_obj = await run(operation, data)
        else:
            data.pop("id")

        payload = {"database": DATABASE, "table": TABLE, "data": data}
        database_obj = await run(operation, payload)
        if database_obj.get("status_code") != 200:
            raise HTTPException(
                status_code=500,
                detail=
                f"Database couldn't create the object. Check the database connection and parameters. Traceback: {database_obj.get('response_message')}",
            )
        else:
            if not fixed_id:
                data.update({
                    "id":
                    database_obj.get("response_message").get("generated_keys")
                    [0]
                })
            return Participation.parse_obj(data)
    else:
        raise HTTPException(
            status_code=403,
            detail=f"Participation already exists for the race.")
Exemplo n.º 3
0
async def create_contract(
    contract: Contract,
    team: str,
    current_user: User = Depends(get_current_active_user)):
    team_exist = await verify_exists_by_id(team, DATABASE, TEAM_TABLE)
    if not team_exist:
        raise HTTPException(status_code=404,
                            detail=f"The Team with ID {team} doesn't exist")

    driver_exist = await verify_exists_by_id(str(contract.driver), DATABASE,
                                             DRIVER_TABLE)
    if not driver_exist:
        raise HTTPException(
            status_code=404,
            detail=f"The Driver with ID {contract.driver} doesn't exist",
        )

    exists = await verify_exists(contract)
    if not exists:
        operation = "insert"
        data = json.loads(contract.json())
        fixed_id = verify_id(contract)
        if fixed_id:
            database_obj = await run(operation, data)
        else:
            data.pop("id")

        payload = {"database": DATABASE, "table": TABLE, "data": data}
        database_obj = await run(operation, payload)
        if database_obj.get("status_code") != 200:
            raise HTTPException(
                status_code=500,
                detail=
                f"Database couldn't create the object. Check the database connection and parameters. Traceback: {database_obj.get('response_message')}",
            )
        else:
            if not fixed_id:
                data.update({
                    "id":
                    database_obj.get("response_message").get("generated_keys")
                    [0]
                })
            return Contract.parse_obj(data)
    else:
        raise HTTPException(status_code=403,
                            detail=f"Driver already have a contract running.")