44
55from typing import List
66from fastapi import APIRouter , Body , Depends , HTTPException , status , Path
7- from sqlalchemy .orm import Session
7+ from sqlalchemy .ext . asyncio import AsyncSession
88from fastapi_cache import FastAPICache
99from fastapi_cache .decorator import cache
10- from data .player_database import OrmSession
10+
11+ from data .player_database import generate_async_session
1112from models .player_model import PlayerModel
1213from services import player_service
1314
1415api_router = APIRouter ()
1516
1617CACHING_TIME_IN_SECONDS = 600
1718
18- # https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency
19-
20-
21- def get_orm_session ():
22- """
23- Dependency function to yield a scoped SQLAlchemy ORM session.
24-
25- Yields:
26- OrmSession: An instance of a scoped SQLAlchemy ORM session.
27- """
28- orm_session = OrmSession ()
29- try :
30- yield orm_session
31- finally :
32- orm_session .close ()
33-
3419# POST -------------------------------------------------------------------------
3520
3621
@@ -40,28 +25,25 @@ def get_orm_session():
4025 summary = "Creates a new Player" ,
4126 tags = ["Players" ]
4227)
43- def post (
28+ async def post (
4429 player_model : PlayerModel = Body (...),
45- orm_session : Session = Depends (get_orm_session )
30+ async_session : AsyncSession = Depends (generate_async_session )
4631):
4732 """
4833 Endpoint to create a new player.
4934
5035 Args:
51- player_model (PlayerModel): The data model representing a Player.
52- orm_session (Session ): The SQLAlchemy ORM session.
36+ player_model (PlayerModel): The Pydantic model representing the Player to create .
37+ async_session (AsyncSession ): The async version of a SQLAlchemy ORM session.
5338
5439 Raises:
5540 HTTPException: HTTP 409 Conflict error if the Player already exists.
5641 """
57- player = player_service .retrieve_by_id (orm_session , player_model .id )
58-
42+ player = await player_service .retrieve_by_id (async_session , player_model .id )
5943 if player :
6044 raise HTTPException (status_code = status .HTTP_409_CONFLICT )
61-
62- player_service .create (orm_session , player_model )
63-
64- FastAPICache .clear ()
45+ await player_service .create (async_session , player_model )
46+ await FastAPICache .clear ()
6547
6648# GET --------------------------------------------------------------------------
6749
@@ -74,20 +56,19 @@ def post(
7456 tags = ["Players" ]
7557)
7658@cache (expire = CACHING_TIME_IN_SECONDS )
77- def get_all (
78- orm_session : Session = Depends (get_orm_session )
59+ async def get_all (
60+ async_session : AsyncSession = Depends (generate_async_session )
7961):
8062 """
8163 Endpoint to retrieve all players.
8264
8365 Args:
84- orm_session (Session ): The SQLAlchemy ORM session.
66+ async_session (AsyncSession ): The async version of a SQLAlchemy ORM session.
8567
8668 Returns:
87- List[PlayerModel]: A list of data models representing all players.
69+ List[PlayerModel]: A list of Pydantic models representing all players.
8870 """
89- players = player_service .retrieve_all (orm_session )
90-
71+ players = await player_service .retrieve_all (async_session )
9172 return players
9273
9374
@@ -99,28 +80,26 @@ def get_all(
9980 tags = ["Players" ]
10081)
10182@cache (expire = CACHING_TIME_IN_SECONDS )
102- def get_by_id (
83+ async def get_by_id (
10384 player_id : int = Path (..., title = "The ID of the Player" ),
104- orm_session : Session = Depends (get_orm_session )
85+ async_session : AsyncSession = Depends (generate_async_session )
10586):
10687 """
10788 Endpoint to retrieve a Player by its ID.
10889
10990 Args:
11091 player_id (int): The ID of the Player to retrieve.
111- orm_session (Session ): The SQLAlchemy ORM session.
92+ async_session (AsyncSession ): The async version of a SQLAlchemy ORM session.
11293
11394 Returns:
114- PlayerModel: A data model representing the Player.
95+ PlayerModel: The Pydantic model representing the matching Player.
11596
11697 Raises:
11798 HTTPException: Not found error if the Player with the specified ID does not exist.
11899 """
119- player = player_service .retrieve_by_id (orm_session , player_id )
120-
100+ player = await player_service .retrieve_by_id (async_session , player_id )
121101 if not player :
122102 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
123-
124103 return player
125104
126105
@@ -132,28 +111,26 @@ def get_by_id(
132111 tags = ["Players" ]
133112)
134113@cache (expire = CACHING_TIME_IN_SECONDS )
135- def get_by_squad_number (
114+ async def get_by_squad_number (
136115 squad_number : int = Path (..., title = "The Squad Number of the Player" ),
137- orm_session : Session = Depends (get_orm_session )
116+ async_session : AsyncSession = Depends (generate_async_session )
138117):
139118 """
140119 Endpoint to retrieve a Player by its Squad Number.
141120
142121 Args:
143122 squad_number (int): The Squad Number of the Player to retrieve.
144- orm_session (Session): SQLAlchemy ORM session.
123+ async_session (AsyncSession): The async version of a SQLAlchemy ORM session.
145124
146125 Returns:
147- PlayerModel: A data model representing the Player.
126+ PlayerModel: The Pydantic model representing the matching Player.
148127
149128 Raises:
150129 HTTPException: HTTP 404 Not Found error if the Player with the specified Squad Number does not exist.
151130 """
152- player = player_service .retrieve_by_squad_number (orm_session , squad_number )
153-
131+ player = await player_service .retrieve_by_squad_number (async_session , squad_number )
154132 if not player :
155133 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
156-
157134 return player
158135
159136# PUT --------------------------------------------------------------------------
@@ -165,30 +142,27 @@ def get_by_squad_number(
165142 summary = "Updates an existing Player" ,
166143 tags = ["Players" ]
167144)
168- def put (
145+ async def put (
169146 player_id : int = Path (..., title = "The ID of the Player" ),
170147 player_model : PlayerModel = Body (...),
171- orm_session : Session = Depends (get_orm_session )
148+ async_session : AsyncSession = Depends (generate_async_session )
172149):
173150 """
174151 Endpoint to entirely update an existing Player.
175152
176153 Args:
177154 player_id (int): The ID of the Player to update.
178- player_model (PlayerModel): The data model representing the Player to update.
179- orm_session (Session ): The SQLAlchemy ORM session.
155+ player_model (PlayerModel): The Pydantic model representing the Player to update.
156+ async_session (AsyncSession ): The async version of a SQLAlchemy ORM session.
180157
181158 Raises:
182159 HTTPException: HTTP 404 Not Found error if the Player with the specified ID does not exist.
183160 """
184- player = player_service .retrieve_by_id (orm_session , player_id )
185-
161+ player = await player_service .retrieve_by_id (async_session , player_id )
186162 if not player :
187163 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
188-
189- player_service .update (orm_session , player_model )
190-
191- FastAPICache .clear ()
164+ await player_service .update (async_session , player_model )
165+ await FastAPICache .clear ()
192166
193167# DELETE -----------------------------------------------------------------------
194168
@@ -199,25 +173,22 @@ def put(
199173 summary = "Deletes an existing Player" ,
200174 tags = ["Players" ]
201175)
202- def delete (
176+ async def delete (
203177 player_id : int = Path (..., title = "The ID of the Player" ),
204- orm_session : Session = Depends (get_orm_session )
178+ async_session : AsyncSession = Depends (generate_async_session )
205179):
206180 """
207181 Endpoint to delete an existing Player.
208182
209183 Args:
210184 player_id (int): The ID of the Player to delete.
211- orm_session (Session ): The SQLAlchemy ORM session.
185+ async_session (AsyncSession ): The async version of a SQLAlchemy ORM session.
212186
213187 Raises:
214188 HTTPException: HTTP 404 Not Found error if the Player with the specified ID does not exist.
215189 """
216- player = player_service .retrieve_by_id (orm_session , player_id )
217-
190+ player = await player_service .retrieve_by_id (async_session , player_id )
218191 if not player :
219192 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
220-
221- player_service .delete (orm_session , player_id )
222-
223- FastAPICache .clear ()
193+ await player_service .delete (async_session , player_id )
194+ await FastAPICache .clear ()
0 commit comments