Add more logging

This commit is contained in:
Esa Kataja
2025-05-13 19:48:37 +03:00
parent 07d8e15925
commit 21e30b0fb2
2 changed files with 103 additions and 45 deletions
+9
View File
@@ -15,6 +15,15 @@ app = FastAPI(
version="1.0rc2",
)
@app.middleware("http")
async def log_requests(request, call_next):
body = await request.body()
logger.debug(f"Request body: {body.decode('utf-8')}")
response = await call_next(request)
return response
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
+94 -45
View File
@@ -3,6 +3,7 @@ from fastapi import APIRouter, HTTPException
from models.review import ReviewOut, ReviewIn
from models.msg import Message
from lib.db import get_connection
from lib.logger import logger
router = APIRouter(prefix="/reviews", tags=["reviews"])
@@ -28,55 +29,103 @@ async def get_review(song_id: int, user_id: int):
return review.to_dict(orient="records")[0]
@router.post("/", response_model=ReviewOut)
@router.post("/", response_model=Message)
async def create_review(review: ReviewIn):
with get_connection() as conn:
# Insert the review
conn.execute(
"INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) VALUES (?, ?, ?, ?, ?, ?)",
(
review.user_id,
review.song_id,
review.score_song,
review.score_show,
review.score_costume,
review.text_review,
),
logger.debug(f"Received create review request with data: {review.dict()}")
try:
with get_connection() as conn:
# Log the SQL query and parameters
logger.debug(
f"Executing INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) "
f"VALUES ({review.user_id}, {review.song_id}, {review.score_song}, "
f"{review.score_show}, {review.score_costume}, '{review.text_review}')"
)
# Insert the review
conn.execute(
"INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) VALUES (?, ?, ?, ?, ?, ?)",
(
review.user_id,
review.song_id,
review.score_song,
review.score_show,
review.score_costume,
review.text_review,
),
)
# Fetch the newly created review
logger.debug(
f"Checking if review was created for song_id={review.song_id}, user_id={review.user_id}"
)
result = conn.execute(
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
(review.song_id, review.user_id),
).fetchdf()
logger.debug(f"Database result for newly created review: {result}")
if result.empty:
logger.warning(
f"Review not found after insert attempt: song_id={review.song_id}, user_id={review.user_id}"
)
raise HTTPException(status_code=404, detail="Review not found")
logger.info(
f"Review created successfully for song_id={review.song_id}, user_id={review.user_id}"
)
# Fetch the newly created review
result = conn.execute(
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
(review.song_id, review.user_id),
).fetchdf()
if result.empty:
raise HTTPException(status_code=404, detail="Review not found")
return Message(type="success", message="Review created successfully")
return Message(type="success", message="Review created successfully")
except Exception as e:
logger.error(f"Error creating review: {str(e)}")
raise
@router.put("/", response_model=Message)
async def update_review(review: ReviewIn):
with get_connection() as conn:
# Update the review
conn.execute(
"UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
(
review.score_song,
review.score_show,
review.score_costume,
review.text_review,
review.song_id,
review.user_id,
),
print("update_review")
logger.debug(f"Received update review request with data: {review.dict()}")
try:
with get_connection() as conn:
# Log the SQL query and parameters
logger.debug(
f"Executing UPDATE Review SET score_song = {review.score_song}, "
f"score_show = {review.score_show}, score_costume = {review.score_costume}, "
f"text_review = '{review.text_review}' WHERE song_id = {review.song_id} "
f"AND user_id = {review.user_id}"
)
# Update the review
conn.execute(
"UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
(
review.score_song,
review.score_show,
review.score_costume,
review.text_review,
review.song_id,
review.user_id,
),
)
# Fetch the updated review
logger.debug(
f"Checking if review was updated for song_id={review.song_id}, user_id={review.user_id}"
)
result = conn.execute(
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
(review.song_id, review.user_id),
).fetchdf()
logger.debug(f"Database result: {result}")
if result.empty:
logger.warning(
f"Review not found after update attempt: song_id={review.song_id}, user_id={review.user_id}"
)
raise HTTPException(status_code=404, detail="Review not found")
logger.info(
f"Review updated successfully for song_id={review.song_id}, user_id={review.user_id}"
)
# Fetch the updated review
result = conn.execute(
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
(review.song_id, review.user_id),
).fetchdf()
if result.empty:
raise HTTPException(status_code=404, detail="Review not found")
return Message(type="success", message="Review updated successfully")
return Message(type="success", message="Review updated successfully")
except Exception as e:
logger.error(f"Error updating review: {str(e)}")
raise