1+ import requests , os
12from app .db import get_db
3+ from sqlalchemy import func
24from app .utils import serialize
35from sqlalchemy .orm import Session
4- from fastapi .responses import JSONResponse
56from fastapi import Depends , APIRouter
6- from app .models import SeasonEntrantConstructor , SeasonEntrantDriver , SeasonEntrantChassis , SeasonEntrant , SeasonConstructor , SeasonDriver , SeasonTyreManufacturer , SeasonEngineManufacturer , SeasonConstructorStanding , SeasonDriverStanding , Race
7+ from fastapi .responses import JSONResponse
8+ from app .schema .season_grand_prix import SeasonGrandPrix
9+ from app .models import SeasonEntrantConstructor , SeasonEntrantDriver , SeasonEntrantChassis , SeasonEntrant , SeasonConstructor , SeasonDriver , SeasonTyreManufacturer , SeasonEngineManufacturer , SeasonConstructorStanding , SeasonDriverStanding , Race , GrandPrix
710
811router = APIRouter ()
912
@@ -109,4 +112,25 @@ async def get_season_constructor_standing_by_year(year: int, db: Session = Depen
109112 SeasonConstructorStanding .year == year
110113 ).all ()
111114 constructor_standing_json = serialize (constructor_standing )
112- return JSONResponse (content = constructor_standing_json )
115+ return JSONResponse (content = constructor_standing_json )
116+
117+ @router .post ("/races/search" )
118+ async def post_season_grand_prix (request : SeasonGrandPrix , db : Session = Depends (get_db )) -> JSONResponse :
119+ """Obtiene un grand prix de una temporada"""
120+
121+ grand_prix = db .query (GrandPrix )\
122+ .order_by (
123+ func .similarity (GrandPrix .short_name , request .grand_prix ).desc ()
124+ )\
125+ .filter (
126+ func .similarity (GrandPrix .short_name , request .grand_prix ) > 0.3
127+ )\
128+ .first ()
129+
130+ grand_prix = db .query (Race )\
131+ .filter (
132+ Race .year == request .year ,
133+ Race .grand_prix_id == grand_prix .id
134+ ).first ()
135+ grand_prix_json = serialize (grand_prix )
136+ return JSONResponse (content = grand_prix_json )
0 commit comments