diff --git a/Dockerfile b/Dockerfile index ce0a394..18d60df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9 +FROM python:3.10-alpine WORKDIR /code @@ -6,6 +6,6 @@ COPY ./requirements.txt /code/requirements.txt RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt -COPY main.py /code/app +COPY . /code/app CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers"] \ No newline at end of file diff --git a/main.py b/main.py index bf20248..411811b 100644 --- a/main.py +++ b/main.py @@ -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", @@ -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() @@ -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): @@ -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) diff --git a/taf2db.py b/taf2db.py index 2ca487c..5842e4f 100644 --- a/taf2db.py +++ b/taf2db.py @@ -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" })