Skip to content

Commit 8cee9f5

Browse files
committed
Refactor game_service.py and add is_valid_slug utility function
1 parent 336882b commit 8cee9f5

File tree

6 files changed

+67
-64
lines changed

6 files changed

+67
-64
lines changed

app/api/v1/endpoints/games.py

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,55 @@
1616
)
1717

1818

19-
# summary_get_games_list = "Get Games List"
20-
# description_get_games_list = """
21-
# ## Find Game
22-
# ### Find all games and params
23-
# """
19+
summary_get_games_list = "Get Games List"
20+
description_get_games_list = """
21+
## Find Game
22+
### Find all games and params
23+
"""
2424

2525

26-
# @router.get(
27-
# "",
28-
# response_model=FindGameResult,
29-
# description=description_get_games_list,
30-
# summary=summary_get_games_list,
31-
# )
32-
# @inject
33-
# def get_games_list(
34-
# schema: PostFindGame = Depends(),
35-
# service: GameService = Depends(Provide[Container.game_service]),
36-
# ):
37-
# return service.get_all_games(schema)
26+
@router.get(
27+
"",
28+
response_model=FindGameResult,
29+
description=description_get_games_list,
30+
summary=summary_get_games_list,
31+
)
32+
@inject
33+
def get_games_list(
34+
schema: PostFindGame = Depends(),
35+
service: GameService = Depends(Provide[Container.game_service]),
36+
):
37+
return service.get_all_games(schema)
3838

3939

40-
# summary_get_game_by_id = "Get Game by Id"
41-
# description_get_game_by_id = """
42-
# ## Get Game by Id
43-
# ### Get game by id
44-
# """
40+
summary_get_game_by_id = "Get Game by externalId"
41+
description_get_game_by_id = """
42+
Get Game by externalId
4543
44+
"""
4645

47-
# @router.get(
48-
# "/{id}",
49-
# response_model=FindGameById,
50-
# description=description_get_game_by_id,
51-
# summary=summary_get_game_by_id,
52-
# )
53-
# @inject
54-
# def get_game_by_id(
55-
# id: UUID,
56-
# service: GameService = Depends(Provide[Container.game_service]),
57-
# ):
5846

59-
# response = service.get_by_id(id)
60-
# return response
47+
@router.get(
48+
"/{id}",
49+
response_model=FindGameById,
50+
description=description_get_game_by_id,
51+
summary=summary_get_game_by_id,
52+
)
53+
@inject
54+
def get_game_by_id(
55+
id: UUID,
56+
service: GameService = Depends(Provide[Container.game_service]),
57+
):
58+
59+
response = service.get_by_id(id)
60+
return response
6161

6262

6363
summary_create_game = "Create Game"
6464
description_create_game = """
6565
Create Game
6666
"""
6767

68-
6968
@router.post(
7069
"",
7170
response_model=GameCreated,
@@ -79,18 +78,6 @@ def create_game(
7978
):
8079
return service.create(schema)
8180

82-
# @router.post(
83-
# "",
84-
# response_model=GameCreated,
85-
# summary=summary_create_game,
86-
# description=description_create_game,
87-
# )
88-
# @inject
89-
# def create_game(
90-
# schema: PostCreateGame,
91-
# service: GameService = Depends(Provide[Container.game_service]),
92-
# ):
93-
# return service.create(schema)
9481

9582

9683
# summary_patch_game = "Update Game"

app/repository/game_repository.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def get_all_games(self, schema):
4343

4444
query = session.query(
4545
Games.id.label("id"),
46+
Games.updated_at.label("updated_at"),
47+
Games.strategyId.label("strategyId"),
4648
Games.created_at.label("created_at"),
4749
Games.platform.label("platform"),
4850
Games.endDateTime.label("endDateTime"),
@@ -73,14 +75,16 @@ def get_all_games(self, schema):
7375
games = query.limit(page_size).offset(
7476
(page - 1) * page_size).all()
7577

76-
total_count = filtered_query.count()
78+
7779

7880
game_results = {}
7981
for game in games:
8082
game_id = game.id
8183
if game_id not in game_results:
8284
game_results[game_id] = BaseGameResult(
8385
id=game.id,
86+
updated_at=game.updated_at,
87+
strategyId=game.strategyId,
8488
created_at=game.created_at,
8589
externalGameId=game.externalGameId,
8690
platform=game.platform,
@@ -90,11 +94,13 @@ def get_all_games(self, schema):
9094
game_results[game_id].params.append(
9195
{
9296
"id": game.GamesParams.id,
93-
"key": game.GamesParams.paramKey,
97+
"key": game.GamesParams.key,
9498
"value": game.GamesParams.value,
9599
}
96100
)
97101

102+
total_count = list(game_results.values()).__len__()
103+
98104
return FindGameResult(
99105
items=list(game_results.values()),
100106
search_options={
@@ -122,7 +128,7 @@ def get_game_by_id(self, id: str):
122128
game_params.append(
123129
{
124130
"id": param.id,
125-
"paramKey": param.paramKey,
131+
"key": param.key,
126132
"value": param.value,
127133
}
128134
)

app/schema/games_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class UpsertGame(BaseGame, metaclass=AllOptional):
9898

9999

100100
class UpsertGameWithGameParams(BaseGame, metaclass=AllOptional):
101-
paramKey: str
101+
key: str
102102
value: str
103103

104104

app/services/game_service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ResponsePatchGame)
1010
from app.services.base_service import BaseService
1111
from app.engine.all_engine_strategies import all_engine_strategies
12-
12+
from app.util.is_valid_slug import is_valid_slug
1313
class GameService(BaseService):
1414
def __init__(
1515
self,
@@ -40,6 +40,16 @@ def create(self, schema: PostCreateGame):
4040
externalGameId_exist = self.game_repository.read_by_column(
4141
"externalGameId", externalGameId, not_found_raise_exception=False
4242
)
43+
44+
is_valid_externalGameId = is_valid_slug(externalGameId)
45+
if not is_valid_externalGameId:
46+
raise ConflictError(
47+
detail=(
48+
f"Invalid externalGameId: {externalGameId}. externalGameId should be a valid slug (Should have only alphanumeric characters and Underscore . Length should be between 3 and 60)"
49+
)
50+
)
51+
52+
# externalGameId
4353
if externalGameId_exist:
4454
raise ConflictError(
4555
detail=(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22

33

4-
def validate_slug(slug):
4+
def is_valid_slug(slug):
55
"""
66
Validates a slug.
77
:param slug: The slug to validate.
@@ -11,4 +11,4 @@ def validate_slug(slug):
1111
1212
1313
"""
14-
return re.match(r'^[a-z0-9_-]{3,60}$', slug) is not None
14+
return re.match(r"^[a-zA-Z0-9_]{3,60}$", slug) is not None

tests/unit_tests/model/test_game_params.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
def test_games_params_creation_and_representation():
77
# Prepare data for creating a GamesParams instance
8-
param_key = "difficulty"
8+
key = "difficulty"
99
value = "hard"
1010
game_id = str(uuid4()) # Assuming UUID is used for game IDs
1111

1212
# Create a GamesParams instance
1313
game_params = GamesParams(
14-
paramKey=param_key,
14+
key=key,
1515
value=value,
1616
gameId=game_id
1717
)
1818

1919
# Test the string representation
2020
expected_str = (
2121
f"GameParams: (id={game_params.id}, created_at={game_params.created_at}, updated_at={game_params.updated_at}, "
22-
f"paramKey={param_key}, value={value}, gameId={game_id})"
22+
f"key={key}, value={value}, gameId={game_id})"
2323
)
2424
assert str(game_params) == expected_str
2525
assert repr(game_params) == expected_str
@@ -28,17 +28,17 @@ def test_games_params_creation_and_representation():
2828
def test_games_params_equality():
2929
game_id = uuid4()
3030
game_params1 = GamesParams(
31-
paramKey="difficulty",
31+
key="difficulty",
3232
value="hard",
3333
gameId=game_id
3434
)
3535
game_params2 = GamesParams(
36-
paramKey="difficulty",
36+
key="difficulty",
3737
value="hard",
3838
gameId=game_id
3939
)
4040
game_params3 = GamesParams(
41-
paramKey="level",
41+
key="level",
4242
value="10",
4343
gameId=game_id
4444
)
@@ -51,17 +51,17 @@ def test_games_params_equality():
5151
def test_games_params_hash():
5252
game_id = uuid4()
5353
game_params1 = GamesParams(
54-
paramKey="difficulty",
54+
key="difficulty",
5555
value="hard",
5656
gameId=game_id
5757
)
5858
game_params2 = GamesParams(
59-
paramKey="difficulty",
59+
key="difficulty",
6060
value="hard",
6161
gameId=game_id
6262
)
6363
game_params3 = GamesParams(
64-
paramKey="level",
64+
key="level",
6565
value="10",
6666
gameId=game_id
6767
)

0 commit comments

Comments
 (0)