22
22
from sqlalchemy .ext .asyncio import AsyncSession
23
23
from aiocache import SimpleMemoryCache
24
24
25
- from databases .player_database import generate_async_session
26
- from models .player_model import PlayerModel
27
- from services import player_service
25
+ from app . databases .player import generate_async_session
26
+ from app . models .player import PlayerModel
27
+ from app . services import player
28
28
29
- api_router = APIRouter ()
29
+ router = APIRouter ()
30
30
simple_memory_cache = SimpleMemoryCache ()
31
31
32
32
CACHE_KEY = "players"
33
33
CACHE_TTL = 600 # 10 minutes
34
34
35
+ PLAYER_TITLE = "The ID of the Player"
36
+
35
37
# POST -------------------------------------------------------------------------
36
38
37
39
38
- @api_router .post (
40
+ @router .post (
39
41
"/players/" ,
40
42
status_code = status .HTTP_201_CREATED ,
41
43
summary = "Creates a new Player" ,
@@ -56,17 +58,17 @@ async def post_async(
56
58
Raises:
57
59
HTTPException: HTTP 409 Conflict error if the Player already exists.
58
60
"""
59
- player = await player_service .retrieve_by_id_async (async_session , player_model .id )
60
- if player :
61
+ player_res = await player .retrieve_by_id_async (async_session , player_model .id )
62
+ if player_res :
61
63
raise HTTPException (status_code = status .HTTP_409_CONFLICT )
62
- await player_service .create_async (async_session , player_model )
64
+ await player .create_async (async_session , player_model )
63
65
await simple_memory_cache .clear (CACHE_KEY )
64
66
65
67
66
68
# GET --------------------------------------------------------------------------
67
69
68
70
69
- @api_router .get (
71
+ @router .get (
70
72
"/players/" ,
71
73
response_model = List [PlayerModel ],
72
74
status_code = status .HTTP_200_OK ,
@@ -88,21 +90,21 @@ async def get_all_async(
88
90
players = await simple_memory_cache .get (CACHE_KEY )
89
91
response .headers ["X-Cache" ] = "HIT"
90
92
if not players :
91
- players = await player_service .retrieve_all_async (async_session )
93
+ players = await player .retrieve_all_async (async_session )
92
94
await simple_memory_cache .set (CACHE_KEY , players , ttl = CACHE_TTL )
93
95
response .headers ["X-Cache" ] = "MISS"
94
96
return players
95
97
96
98
97
- @api_router .get (
99
+ @router .get (
98
100
"/players/{player_id}" ,
99
101
response_model = PlayerModel ,
100
102
status_code = status .HTTP_200_OK ,
101
103
summary = "Retrieves a Player by its Id" ,
102
104
tags = ["Players" ],
103
105
)
104
106
async def get_by_id_async (
105
- player_id : int = Path (..., title = "The ID of the Player" ),
107
+ player_id : int = Path (..., title = PLAYER_TITLE ),
106
108
async_session : AsyncSession = Depends (generate_async_session ),
107
109
):
108
110
"""
@@ -119,13 +121,13 @@ async def get_by_id_async(
119
121
HTTPException: Not found error if the Player with the specified ID does not
120
122
exist.
121
123
"""
122
- player = await player_service .retrieve_by_id_async (async_session , player_id )
123
- if not player :
124
+ player_res = await player .retrieve_by_id_async (async_session , player_id )
125
+ if not player_res :
124
126
raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
125
- return player
127
+ return player_res
126
128
127
129
128
- @api_router .get (
130
+ @router .get (
129
131
"/players/squadnumber/{squad_number}" ,
130
132
response_model = PlayerModel ,
131
133
status_code = status .HTTP_200_OK ,
@@ -150,25 +152,25 @@ async def get_by_squad_number_async(
150
152
HTTPException: HTTP 404 Not Found error if the Player with the specified
151
153
Squad Number does not exist.
152
154
"""
153
- player = await player_service .retrieve_by_squad_number_async (
155
+ player_res = await player .retrieve_by_squad_number_async (
154
156
async_session , squad_number
155
157
)
156
- if not player :
158
+ if not player_res :
157
159
raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
158
- return player
160
+ return player_res
159
161
160
162
161
163
# PUT --------------------------------------------------------------------------
162
164
163
165
164
- @api_router .put (
166
+ @router .put (
165
167
"/players/{player_id}" ,
166
168
status_code = status .HTTP_204_NO_CONTENT ,
167
169
summary = "Updates an existing Player" ,
168
170
tags = ["Players" ],
169
171
)
170
172
async def put_async (
171
- player_id : int = Path (..., title = "The ID of the Player" ),
173
+ player_id : int = Path (..., title = PLAYER_TITLE ),
172
174
player_model : PlayerModel = Body (...),
173
175
async_session : AsyncSession = Depends (generate_async_session ),
174
176
):
@@ -185,24 +187,24 @@ async def put_async(
185
187
HTTPException: HTTP 404 Not Found error if the Player with the specified ID
186
188
does not exist.
187
189
"""
188
- player = await player_service .retrieve_by_id_async (async_session , player_id )
189
- if not player :
190
+ player_res = await player .retrieve_by_id_async (async_session , player_id )
191
+ if not player_res :
190
192
raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
191
- await player_service .update_async (async_session , player_model )
193
+ await player .update_async (async_session , player_model )
192
194
await simple_memory_cache .clear (CACHE_KEY )
193
195
194
196
195
197
# DELETE -----------------------------------------------------------------------
196
198
197
199
198
- @api_router .delete (
200
+ @router .delete (
199
201
"/players/{player_id}" ,
200
202
status_code = status .HTTP_204_NO_CONTENT ,
201
203
summary = "Deletes an existing Player" ,
202
204
tags = ["Players" ],
203
205
)
204
206
async def delete_async (
205
- player_id : int = Path (..., title = "The ID of the Player" ),
207
+ player_id : int = Path (..., title = PLAYER_TITLE ),
206
208
async_session : AsyncSession = Depends (generate_async_session ),
207
209
):
208
210
"""
@@ -216,8 +218,8 @@ async def delete_async(
216
218
HTTPException: HTTP 404 Not Found error if the Player with the specified ID
217
219
does not exist.
218
220
"""
219
- player = await player_service .retrieve_by_id_async (async_session , player_id )
220
- if not player :
221
+ player_res = await player .retrieve_by_id_async (async_session , player_id )
222
+ if not player_res :
221
223
raise HTTPException (status_code = status .HTTP_404_NOT_FOUND )
222
- await player_service .delete_async (async_session , player_id )
224
+ await player .delete_async (async_session , player_id )
223
225
await simple_memory_cache .clear (CACHE_KEY )
0 commit comments