Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
services:
# db:
# image: postgres:16-alpine
# restart: always
# profiles: [ "local" ]
# environment:
# POSTGRES_USER: postgres
# POSTGRES_PASSWORD: postgres
# POSTGRES_DB: code_executor
# ports:
# - "5432:5432"
# volumes:
# - postgres_data:/var/lib/postgresql/data
# healthcheck:
# test: [ "CMD-SHELL", "pg_isready -U postgres -d code_executor" ]
# interval: 5s
# timeout: 5s
# retries: 5
db:
image: postgres:16-alpine
restart: always
profiles: [ "local" ]
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: code_executor
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -d code_executor" ]
interval: 5s
timeout: 5s
retries: 5

# local-code-api:
# image: code-api:latest
# build: .
# profiles: [ "local" ]
# ports:
# - "3000:3000"
# environment:
# DATABASE_URL: ${POSTGRES_URL}
# depends_on:
# db:
# condition: service_healthy
# command: >
# sh -c "python3 -m alembic upgrade head &&
# python3 -m scripts.seed &&
# gunicorn --bind 0.0.0.0:3000 app:app"
local-code-api:
image: code-api:latest
build: .
profiles: [ "local" ]
ports:
- "3000:3000"
environment:
DATABASE_URL: ${POSTGRES_URL}
depends_on:
db:
condition: service_healthy
command: >
sh -c "python3 -m alembic upgrade head &&
gunicorn --bind 0.0.0.0:3000 app:app"

code-api:
image: code-api:latest
build: .
profiles: [ "remote" ]
ports:
- "3000:3000"
environment:
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/riddle_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def get_riddles(self):
def get_riddles_group(self):
"""Get a random group of riddles (1 per index) and their solution."""
amount = request.args.get('amount', 5, type=int)
res = self.riddle_service.get_random_riddles_group(amount)
tag = request.args.get('tag')
res = self.riddle_service.get_random_riddles_group(amount, tag=tag)
if res['status'] == "error":
return jsonify(res), 400
return jsonify(res), 200
Expand Down
9 changes: 7 additions & 2 deletions src/repositories/riddle_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ def find_all(self, offset: int = 0, limit: int = 10):

return riddles, total_count

def find_random_per_index(self, amount: int):
def find_random_per_index(self, amount: int, tag_name: str = None):
"""Pick one random riddle for each refer_index from 1 to amount."""
riddles_group = []
with self._get_session() as session:
for i in range(1, amount + 1):
statement = select(Riddle).where(Riddle.refer_index == i)
if tag_name:
statement = statement.join(Riddle.tags).where(Tag.name.ilike(tag_name))
results = session.exec(statement).all()
if results:
riddles_group.append(random.choice(results))
else:
raise ValueError(f"No riddles found for specific index {i}")
error_msg = f"No riddles found for specific index {i}"
if tag_name:
error_msg += f" with tag '{tag_name}'"
raise ValueError(error_msg)
return riddles_group
4 changes: 2 additions & 2 deletions src/services/riddle_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def update_riddle(self, riddle_id: str, update_data: dict):

return self.riddle_repo.update(riddle).model_dump()

def get_random_riddles_group(self, amount: int):
def get_random_riddles_group(self, amount: int, tag: str = None):
"""Pick N random riddles (1 per refer_index) and build solution string."""
try:
riddles = self.riddle_repo.find_random_per_index(amount)
riddles = self.riddle_repo.find_random_per_index(amount, tag_name=tag)

# Build solution string by joining refer_char in order of refer_index
solution = "".join([r.refer_char for r in riddles])
Expand Down