Add more logging
This commit is contained in:
@@ -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,
|
||||
|
||||
+50
-1
@@ -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,9 +29,18 @@ 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):
|
||||
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 (?, ?, ?, ?, ?, ?)",
|
||||
@@ -45,19 +55,44 @@ async def create_review(review: ReviewIn):
|
||||
)
|
||||
|
||||
# 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}"
|
||||
)
|
||||
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):
|
||||
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 = ?",
|
||||
@@ -72,11 +107,25 @@ async def update_review(review: ReviewIn):
|
||||
)
|
||||
|
||||
# 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}"
|
||||
)
|
||||
return Message(type="success", message="Review updated successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating review: {str(e)}")
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user