Skip to content

Commit

Permalink
add response types for swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Dec 29, 2023
1 parent e7d0be2 commit 093825b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
40 changes: 34 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
from fastapi import FastAPI
import sqlite3
from typing import Optional
from pydantic import BaseModel

app = FastAPI(title="DLV", docs_url="/swagger",
openapi_url="/swagger-json", redoc_url=None)

class Athlete(BaseModel):
guid: str
id: str
firstname: str
lastname: str
clubId: str
country: str
birthyear: int
sex: str
worldAthleticsId: int | None
club: str
lv: str

class Club(BaseModel):
lv: str
name: str
id: str
type: str

def create_connection():
conn = sqlite3.connect("file:stammdaten.db?mode=ro",
Expand All @@ -24,14 +43,14 @@ def query_db(query, args=(), one=False):


@app.get("/clubs/{lv}")
def read_clubs_by_lv(lv: str, q: Union[str, None] = None):
def read_clubs_by_lv(lv: str, q: Union[str, None] = None) -> list[Club]:
query = f"SELECT * FROM Club WHERE lv = '{lv}'"
clubs = query_db(query)
return clubs


@app.get("/lv")
def read_lv():
def read_lv() -> list[str]:
query = f"SELECT DISTINCT lv FROM Club"
connection = create_connection()
lv = connection.execute(query).fetchall()
Expand All @@ -41,7 +60,7 @@ def read_lv():


@app.get("/athletes/{guid}")
def get_athlete_by_guid(guid: str):
def get_athlete_by_guid(guid: str) -> Athlete:
query = f"SELECT * FROM Athlete WHERE guid = '{guid}'"
athlete = query_db(query)
if (len(athlete) == 0):
Expand All @@ -56,23 +75,32 @@ def get_athletes(
clubId: Optional[str] = None,
worldAthleticsId: Optional[int] = None,
lv: Optional[str] = None,
sex: Optional[str] = None,
country: Optional[str] = None,
birthyear: Optional[int] = None,
limit: Optional[int] = 100,
page: Optional[int] = 0,
):
) -> list[Athlete]:
query = "SELECT Athlete.*,C.name as club,lv FROM Athlete JOIN main.Club C on Athlete.clubId = C.id"

conditions = []

if firstname:
conditions.append(f"firstname LIKE '%{firstname}%'")
conditions.append(f"firstname LIKE '{firstname}'")
if lastname:
conditions.append(f"lastname LIKE '%{lastname}%'")
conditions.append(f"lastname LIKE '{lastname}'")
if clubId:
conditions.append(f"clubId = '{clubId}'")
if worldAthleticsId:
conditions.append(f"worldAthleticsId = '{worldAthleticsId}'")
if lv:
conditions.append(f"lv = '{lv}'")
if birthyear:
conditions.append(f"birthyear = '{birthyear}'")
if country:
conditions.append(f"country = '{country}'")
if sex:
conditions.append(f"sex ='{sex}'")

if conditions:
query += " WHERE " + " AND ".join(conditions)
Expand Down
2 changes: 1 addition & 1 deletion taf2db.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def main(taf_path) -> None:
df = df[["ExternalId", "Code", "Firstname", "Lastname", "ClubCode", "Nation", "Yob", "Gender", "WorldAthleticsId"]]

df['WorldAthleticsId'] = df['WorldAthleticsId'].apply(lambda x: None if x == 0 else int(x))
df["Gender"] = df['Gender'].apply(lambda x: "M" if x == 0 else "W")
df["Gender"] = df['Gender'].apply(lambda x: "MALE" if x == 0 else "FEMALE")

df = df.rename(columns={"Code": "id", "ExternalId": "guid", "Firstname": "firstname", "Lastname": "lastname", "ClubCode": "clubId", "Nation": "country", "Yob": "birthyear", "Gender": "sex", "WorldAthleticsId": "worldAthleticsId" })

Expand Down

0 comments on commit 093825b

Please sign in to comment.