11from app .db import get_db
22from app .utils import serialize
33from sqlalchemy .orm import Session
4- from fastapi .responses import JSONResponse
54from fastapi import Depends , APIRouter
6- from app .models import SeasonEntrantConstructor , SeasonEntrantDriver , SeasonEntrantChassis , SeasonEntrant , SeasonConstructor , SeasonDriver , SeasonTyreManufacturer , SeasonEngineManufacturer , SeasonConstructorStanding , SeasonDriverStanding , Race
5+ from fastapi .responses import JSONResponse
6+ from app .models import SeasonEntrantConstructor , SeasonEntrantDriver , SeasonEntrantChassis , SeasonConstructor , SeasonDriver , SeasonTyreManufacturer , SeasonEngineManufacturer , SeasonConstructorStanding , SeasonDriverStanding , Race , GrandPrix
77
88router = APIRouter ()
99
10- @router .get ("/{year}/races " )
11- async def get_season_races_by_year (year :int , db : Session = Depends (get_db )) -> JSONResponse :
12- """Obtiene todas las carreras de la temporada"""
13- season_races_year = db .query (
10+ @router .get ("/{year}/grand_prix " )
11+ async def get_season_grand_prix_by_year (year :int , db : Session = Depends (get_db )) -> JSONResponse :
12+ """Obtiene todos los grand prix de la temporada"""
13+ season_grand_prix_year = db .query (
1414 Race .id ,
1515 Race .year ,
1616 Race .round ,
1717 Race .grand_prix_id ,
1818 Race .circuit_id
1919 ) \
2020 .filter (Race .year == year ).all ()
21- season_races_year_json = serialize (season_races_year )
22- return JSONResponse (content = season_races_year_json )
21+ season_grand_prix_year_json = serialize (season_grand_prix_year )
22+ return JSONResponse (content = season_grand_prix_year_json )
23+
24+ @router .get ("/{year}/grand_prix/{race_id}" )
25+ async def get_grand_prix_by_race_id (year : int , race_id : int , db : Session = Depends (get_db )) -> JSONResponse :
26+ """Obtiene un grand prix de una temporada por race_id"""
27+ grand_prix = db .query (
28+ Race .id ,
29+ Race .year ,
30+ Race .round ,
31+ Race .grand_prix_id ,
32+ Race .circuit_id
33+ ) \
34+ .filter (
35+ Race .year == year ,
36+ Race .id == race_id
37+ ) \
38+ .first ()
39+ grand_prix_json = serialize (grand_prix )
40+ return JSONResponse (content = grand_prix_json )
2341
24- @router .get ("/{year}/entrants" )
25- async def get_season_entrants_by_year (year :int , db : Session = Depends (get_db )) -> JSONResponse :
26- """Obtiene todos los participantes de una temporada"""
27- season_entrants_year = db .query (SeasonEntrant ).filter (SeasonEntrant .year == year ).all ()
28- season_entrants_year_json = serialize (season_entrants_year )
29- return JSONResponse (content = season_entrants_year_json )
42+ @router .get ("/{year}/{round}" )
43+ async def get_grand_prix_by_year_and_round (year : int , round : int , db : Session = Depends (get_db )) -> JSONResponse :
44+ """Obtener un grand prix por el año y la ronda"""
45+ grand_prix = db .query (
46+ Race .id ,
47+ Race .year ,
48+ Race .round ,
49+ Race .grand_prix_id ,
50+ Race .circuit_id
51+ )\
52+ .filter (
53+ Race .year == year ,
54+ Race .round == round
55+ ) \
56+ .first ()
57+ grand_prix_json = serialize (grand_prix )
58+ return JSONResponse (content = grand_prix_json )
3059
3160@router .get ("/{year}/constructors" )
32- async def get_season_constructors_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
61+ async def get_constructor_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
3362 """Obtiene todos los constructores entrantes de una temporada"""
3463 season_constructors = db .query (SeasonEntrantConstructor ).filter (SeasonEntrantConstructor .year == year ).distinct ().all ()
3564 season_constructors_json = serialize (season_constructors )
3665 return JSONResponse (content = season_constructors_json )
3766
3867@router .get ("/{year}/constructors/{constructor_id}" )
39- async def get_season_constructor_by_year_and_constructor_id (year : int , constructor_id : str , db : Session = Depends (get_db )) -> JSONResponse :
68+ async def get_constructor_by_year_and_constructor_id (year : int , constructor_id : str , db : Session = Depends (get_db )) -> JSONResponse :
4069 """Obtiene las estadísticas de un constructor de una temporada"""
4170 season_constructor = db .query (SeasonConstructor )\
4271 .filter (
@@ -48,14 +77,14 @@ async def get_season_constructor_by_year_and_constructor_id(year: int, construct
4877 return JSONResponse (content = season_constructor_json )
4978
5079@router .get ("/{year}/drivers" )
51- async def get_season_drivers_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
80+ async def get_driver_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
5281 """Obtiene todos los pilotos entrantes en una temporada"""
5382 season_drivers = db .query (SeasonEntrantDriver ).filter (SeasonEntrantDriver .year == year ).distinct ().all ()
5483 season_drivers_json = serialize (season_drivers )
5584 return JSONResponse (content = season_drivers_json )
5685
5786@router .get ("/{year}/drivers/{driver_id}" )
58- async def get_season_driver_by_year_and_driver_id (year : int , driver_id : str , db : Session = Depends (get_db )) -> JSONResponse :
87+ async def get_driver_by_year_and_driver_id (year : int , driver_id : str , db : Session = Depends (get_db )) -> JSONResponse :
5988 """Obtiene las estadísticas de un driver de una temporada"""
6089 season_driver = db .query (SeasonDriver )\
6190 .filter (
@@ -67,21 +96,21 @@ async def get_season_driver_by_year_and_driver_id(year: int, driver_id: str, db:
6796 return JSONResponse (content = season_driver_json )
6897
6998@router .get ("/{year}/tyres/stats" )
70- async def get_season_tyres_stats_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
99+ async def get_tyres_stats_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
71100 """Obtiene las estadísticas de los neumáticos por año"""
72101 season_tyre_manufacturers_stats = db .query (SeasonTyreManufacturer ).filter (SeasonTyreManufacturer .year == year ).all ()
73102 season_tyre_manufacturers_stats_json = serialize (season_tyre_manufacturers_stats )
74103 return JSONResponse (content = season_tyre_manufacturers_stats_json )
75104
76105@router .get ("/{year}/engines/stats" )
77- async def get_season_engines_stats_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
106+ async def get_engines_stats_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
78107 """Obtiene las estadísticas de los motoristas por año"""
79108 season_engines_manufacturers_stats = db .query (SeasonEngineManufacturer ).filter (SeasonEngineManufacturer .year == year ).all ()
80109 season_engines_manufacturers_stats_json = serialize (season_engines_manufacturers_stats )
81110 return JSONResponse (content = season_engines_manufacturers_stats_json )
82111
83112@router .get ("/{year}/chassis" )
84- async def get_season_chassis_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
113+ async def get_chassis_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
85114 """Obtiene todos los chassis usados en una temporada específica"""
86115 season_chassis = db .query (SeasonEntrantChassis )\
87116 .filter (
@@ -92,21 +121,43 @@ async def get_season_chassis_by_year(year: int, db: Session = Depends(get_db)) -
92121 return JSONResponse (content = season_chassis_json )
93122
94123@router .get ("/{year}/standing/drivers" )
95- async def get_season_driver_standing_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
124+ async def get_driver_standing_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
96125 """Obtiene el campeonato de pilotos de una temporada"""
97- driver_standing = db .query (SeasonDriverStanding ) \
126+ drivers_standing = db .query (SeasonDriverStanding ) \
98127 .filter (
99128 SeasonDriverStanding .year == year
100129 ).all ()
130+ drivers_standing_json = serialize (drivers_standing )
131+ return JSONResponse (content = drivers_standing_json )
132+
133+ @router .get ("/{year}/standing/drivers/{driver_id}" )
134+ async def get_driver_standing_specific_by_year_and_driver_id (year : int , driver_id : str , db : Session = Depends (get_db )) -> JSONResponse :
135+ """Obtiene una posición específica del campeonato de pilotos de una temporada"""
136+ driver_standing = db .query (SeasonDriverStanding ) \
137+ .filter (
138+ SeasonDriverStanding .year == year ,
139+ SeasonDriverStanding .driver_id == driver_id
140+ ).first ()
101141 driver_standing_json = serialize (driver_standing )
102142 return JSONResponse (content = driver_standing_json )
103143
104144@router .get ("/{year}/standing/constructors" )
105- async def get_season_constructor_standing_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
145+ async def get_constructor_standing_by_year (year : int , db : Session = Depends (get_db )) -> JSONResponse :
106146 """Obtiene el campeonato de constructores de una temporada"""
107- constructor_standing = db .query (SeasonConstructorStanding ) \
147+ constructors_standing = db .query (SeasonConstructorStanding ) \
108148 .filter (
109149 SeasonConstructorStanding .year == year
110150 ).all ()
151+ constructors_standing_json = serialize (constructors_standing )
152+ return JSONResponse (content = constructors_standing_json )
153+
154+ @router .get ("/{year}/standing/constructors/{constructor_id}" )
155+ async def get_constructor_standing_specific_by_year_and_constructor_id (year : int , constructor_id : str , db : Session = Depends (get_db )) -> JSONResponse :
156+ """Obtiene una posición específica del campeonato de constructores de una temporada"""
157+ constructor_standing = db .query (SeasonConstructorStanding ) \
158+ .filter (
159+ SeasonConstructorStanding .year == year ,
160+ SeasonConstructorStanding .constructor_id == constructor_id
161+ ).first ()
111162 constructor_standing_json = serialize (constructor_standing )
112163 return JSONResponse (content = constructor_standing_json )
0 commit comments