152 lines
5.8 KiB
Python
152 lines
5.8 KiB
Python
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"])
|
|
|
|
|
|
@router.get("/all", response_model=list[ReviewOut])
|
|
async def list_reviews():
|
|
with get_connection() as conn:
|
|
reviews = conn.execute("SELECT * FROM Review").fetchdf()
|
|
if reviews.empty:
|
|
return []
|
|
return reviews.to_dict(orient="records")
|
|
|
|
|
|
@router.get("/", response_model=ReviewOut)
|
|
async def get_review(song_id: int, user_id: int):
|
|
with get_connection() as conn:
|
|
review = conn.execute(
|
|
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
|
(song_id, user_id),
|
|
).fetchdf()
|
|
if review.empty:
|
|
return []
|
|
return review.to_dict(orient="records")[0]
|
|
|
|
|
|
@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 (?, ?, ?, ?, ?, ?)",
|
|
(
|
|
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}"
|
|
)
|
|
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}"
|
|
)
|
|
|
|
# Check if the review exists
|
|
review_df = conn.execute(
|
|
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
|
(review.song_id, review.user_id),
|
|
).fetchdf()
|
|
|
|
if not review_df.empty:
|
|
# 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,
|
|
),
|
|
)
|
|
else:
|
|
# 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 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
|