Skip to content

Commit

Permalink
Refactor code and add redirect endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fvergaracl committed Mar 7, 2024
1 parent 5b54cf8 commit b65d424
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
19 changes: 19 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from app.util.class_object import singleton
from fastapi.openapi.utils import get_openapi
from app.schema.base_schema import RootEndpoint
from fastapi.responses import RedirectResponse


def get_project_data():
Expand All @@ -33,6 +34,7 @@ def custom_openapi():
)
# Eliminar el prefijo /api/v1 de las rutas en la documentación de Swagger
for path in list(openapi_schema["paths"].keys()):
print(path)
if path.startswith("/api/v1"):
openapi_schema["paths"][path[7:]
] = openapi_schema["paths"].pop(path)
Expand Down Expand Up @@ -80,7 +82,18 @@ def __init__(self):
allow_headers=["*"],
)

@self.app.get("/", include_in_schema=False)
def read_root():
"""
Redirect to /docs
return:
RedirectResponse: Redirect to /docs
"""
return RedirectResponse(url='/docs')

# set routes

@self.app.get(
"/api/v1",
tags=["root"],
Expand All @@ -89,6 +102,12 @@ def __init__(self):
description="General information about the API"
)
def root():
"""
Root API v1 endpoint
return:
RootEndpoint: General information about the API
"""
version = configs.GAMIFICATIONENGINE_VERSION_APP
project_name = configs.PROJECT_NAME
return {
Expand Down
11 changes: 7 additions & 4 deletions app/repository/game_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def get_all_games(self, schema):
if page_size == "all":
games = query.all()
else:
games = query.limit(page_size).offset((page - 1) * page_size).all()
games = query.limit(page_size).offset(
(page - 1) * page_size).all()

total_count = filtered_query.count()

Expand All @@ -86,7 +87,7 @@ def get_all_games(self, schema):
game_results[game_id].params.append(
{
"id": game.GamesParams.id,
"paramKey": game.GamesParams.paramKey,
"key": game.GamesParams.paramKey,
"value": game.GamesParams.value,
}
)
Expand All @@ -103,7 +104,8 @@ def get_all_games(self, schema):

def get_game_by_id(self, id: str):
with self.session_factory() as session:
game = session.query(self.model).filter(self.model.id == id).first()
game = session.query(self.model).filter(
self.model.id == id).first()
if not game:
raise NotFoundError(detail=f"Not found id : {id}")
# buscando los parametros del juego
Expand Down Expand Up @@ -134,7 +136,8 @@ def get_game_by_id(self, id: str):

def patch_game_by_id(self, id: str, schema):
with self.session_factory() as session:
game = session.query(self.model).filter(self.model.id == id).first()
game = session.query(self.model).filter(
self.model.id == id).first()
if not game:
raise NotFoundError(detail=f"Not found id : {id}")

Expand Down
8 changes: 5 additions & 3 deletions app/schema/games_params_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@


class BaseGameParams(BaseModel):
paramKey: str
key: str
value: str | int | float | bool

class Config:
orm_mode = True


class BaseCreateGameParams(BaseGameParams): ...
class BaseCreateGameParams(BaseGameParams):
...


class InsertGameParams(BaseGameParams):
gameId: str


class CreateGameParams(BaseCreateGameParams): ...
class CreateGameParams(BaseCreateGameParams):
...


class BaseFindGameParams(BaseGameParams):
Expand Down

0 comments on commit b65d424

Please sign in to comment.