Skip to content

Commit

Permalink
Refactor game endpoints and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
fvergaracl committed Mar 11, 2024
1 parent bea240a commit f3a915c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 90 deletions.
175 changes: 94 additions & 81 deletions app/api/v1/endpoints/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,53 @@
)


summary_get_games_list = "Get Games List"
description_get_games_list = """
## Find Game
### Find all games and params
"""


@router.get(
"",
response_model=FindGameResult,
description=description_get_games_list,
summary=summary_get_games_list,
)
@inject
def get_games_list(
schema: PostFindGame = Depends(),
service: GameService = Depends(Provide[Container.game_service]),
):
return service.get_all_games(schema)


summary_get_game_by_id = "Get Game by Id"
description_get_game_by_id = """
## Get Game by Id
### Get game by id
"""


@router.get(
"/{id}",
response_model=FindGameById,
description=description_get_game_by_id,
summary=summary_get_game_by_id,
)
@inject
def get_game_by_id(
id: UUID,
service: GameService = Depends(Provide[Container.game_service]),
):

response = service.get_by_id(id)
return response
# summary_get_games_list = "Get Games List"
# description_get_games_list = """
# ## Find Game
# ### Find all games and params
# """


# @router.get(
# "",
# response_model=FindGameResult,
# description=description_get_games_list,
# summary=summary_get_games_list,
# )
# @inject
# def get_games_list(
# schema: PostFindGame = Depends(),
# service: GameService = Depends(Provide[Container.game_service]),
# ):
# return service.get_all_games(schema)


# summary_get_game_by_id = "Get Game by Id"
# description_get_game_by_id = """
# ## Get Game by Id
# ### Get game by id
# """


# @router.get(
# "/{id}",
# response_model=FindGameById,
# description=description_get_game_by_id,
# summary=summary_get_game_by_id,
# )
# @inject
# def get_game_by_id(
# id: UUID,
# service: GameService = Depends(Provide[Container.game_service]),
# ):

# response = service.get_by_id(id)
# return response


summary_create_game = "Create Game"
description_create_game = """
## Create Game
Create Game
"""


Expand All @@ -79,41 +79,54 @@ def create_game(
):
return service.create(schema)


summary_patch_game = "Update Game"
description_patch_game = """
## Update Game
can update even the GameParams
"""


@router.patch("/{id}", response_model=ResponsePatchGame)
@inject
def patch_game(
id: str,
schema: PatchGame,
service: GameService = Depends(Provide[Container.game_service]),
):
return service.patch_game_by_id(id, schema)


summary_get_task_by_id_game = "Get Task by Id Game"
description_get_task_by_id_game = """
## Get Task by Id Game
"""


@router.get(
"/{id}/tasks",
response_model=FindTaskGameById,
description=description_get_task_by_id_game,
summary=summary_get_task_by_id_game,
)
@inject
def get_task_by_id_game(
id: UUID,
service: GameService = Depends(Provide[Container.game_service]),
):

response = service.get_tasks_by_gameId(id)
return response
# @router.post(
# "",
# response_model=GameCreated,
# summary=summary_create_game,
# description=description_create_game,
# )
# @inject
# def create_game(
# schema: PostCreateGame,
# service: GameService = Depends(Provide[Container.game_service]),
# ):
# return service.create(schema)


# summary_patch_game = "Update Game"
# description_patch_game = """
# Update Game
# can update even the GameParams
# """


# @router.patch("/{id}", response_model=ResponsePatchGame)
# @inject
# def patch_game(
# id: str,
# schema: PatchGame,
# service: GameService = Depends(Provide[Container.game_service]),
# ):
# return service.patch_game_by_id(id, schema)


# summary_get_task_by_id_game = "Get Task by Id Game"
# description_get_task_by_id_game = """
# Get Task by Id Game
# """


# @router.get(
# "/{id}/tasks",
# response_model=FindTaskGameById,
# description=description_get_task_by_id_game,
# summary=summary_get_task_by_id_game,
# )
# @inject
# def get_task_by_id_game(
# id: UUID,
# service: GameService = Depends(Provide[Container.game_service]),
# ):

# response = service.get_tasks_by_gameId(id)
# return response
11 changes: 8 additions & 3 deletions app/schema/games_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BaseGameResult(BaseModel):
id: Optional[UUID] = None
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None
strategyId: Optional[str] = None
externalGameId: Optional[str] = None
platform: Optional[str] = None
endDateTime: Optional[datetime] = None
Expand All @@ -34,6 +35,7 @@ class Config:
class PostCreateGame(BaseModel):
externalGameId: str
platform: str
strategyId: Optional[str] = "default"
endDateTime: Optional[datetime]
params: Optional[List[CreateGameParams]]

Expand Down Expand Up @@ -79,17 +81,20 @@ class FindTaskGameById(ModelBaseInfo):
tasks: Optional[List[Task]]


class Game(ModelBaseInfo, BaseGame, metaclass=AllOptional): ...
class Game(ModelBaseInfo, BaseGame, metaclass=AllOptional):
...


class PostFindGame(FindBase, BaseGame, metaclass=AllOptional): ...
class PostFindGame(FindBase, BaseGame, metaclass=AllOptional):
...


class FindGameByExternalId(FindBase, BaseGame, metaclass=AllOptional):
externalGameId: str


class UpsertGame(BaseGame, metaclass=AllOptional): ...
class UpsertGame(BaseGame, metaclass=AllOptional):
...


class UpsertGameWithGameParams(BaseGame, metaclass=AllOptional):
Expand Down
20 changes: 14 additions & 6 deletions app/services/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def get_all_games(self, schema):
return self.game_repository.get_all_games(schema)

def get_by_externalId(self, externalGameId: str):
return self.game_repository.read_by_column("externalGameId", externalGameId)
return self.game_repository.read_by_column(
"externalGameId", externalGameId)

def create(self, schema: PostCreateGame):
params = schema.params
Expand All @@ -41,7 +42,9 @@ def create(self, schema: PostCreateGame):
)
if externalGameId_exist:
raise ConflictError(
detail=(f"Game already exist with externalGameId: {externalGameId}")
detail=(
f"Game already exist with externalGameId: "
f"{externalGameId}")
)
created_params = []
game = self.game_repository.create(schema)
Expand All @@ -55,12 +58,15 @@ def create(self, schema: PostCreateGame):
params_dict["gameId"] = str(game.id)

params_to_insert = InsertGameParams(**params_dict)
created_param = self.game_params_repository.create(params_to_insert)
created_param = self.game_params_repository.create(
params_to_insert)

created_params.append(created_param)

response = GameCreated(
**game.dict(), params=created_params, message="Successfully created"
**game.dict(), params=created_params,
message=f"Game with externalGameId: {externalGameId} created"
f" successfully"
)
return response

Expand All @@ -72,14 +78,16 @@ def patch_game_by_id(self, id: UUID, schema: PatchGame):
updated_params = []
if params:
for param in params:
self.game_params_repository.patch_game_params_by_id(param.id, param)
self.game_params_repository.patch_game_params_by_id(
param.id, param)
updated_params.append(param)

updated_game_dict = updated_game.dict()
updated_game_dict.pop("params", None)

response = ResponsePatchGame(
**updated_game_dict, params=updated_params, message="Successfully updated"
**updated_game_dict, params=updated_params,
message="Successfully updated"
)
return response

Expand Down

0 comments on commit f3a915c

Please sign in to comment.