Add review model and CRUD endpoints for managing song reviews
This commit is contained in:
+66
-2
@@ -1,3 +1,67 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
router = APIRouter(prefix="/songs", tags=["songs"])
|
||||
from models.review import ReviewOut, ReviewSearch, ReviewIn
|
||||
from lib.db import get_connection
|
||||
|
||||
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:
|
||||
raise HTTPException(status_code=404, detail="Reviews not found")
|
||||
return reviews.to_dict(orient="records")
|
||||
|
||||
|
||||
@router.get("/", response_model=ReviewOut)
|
||||
async def get_review(review_id: ReviewSearch):
|
||||
with get_connection() as conn:
|
||||
review = conn.execute(
|
||||
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
||||
(review_id.song_id, review_id.user_id),
|
||||
).fetchdf()
|
||||
if review.empty:
|
||||
raise HTTPException(status_code=404, detail="Review not found")
|
||||
return review.to_dict(orient="records")[0]
|
||||
|
||||
|
||||
@router.post("/", response_model=ReviewOut)
|
||||
async def create_review(review: ReviewIn):
|
||||
with get_connection() as conn:
|
||||
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,
|
||||
),
|
||||
).fetchdf()
|
||||
if review.empty:
|
||||
raise HTTPException(status_code=404, detail="Review not found")
|
||||
return review.to_dict(orient="records")[0]
|
||||
|
||||
|
||||
@router.put("/", response_model=ReviewOut)
|
||||
async def update_review(review: ReviewIn):
|
||||
with get_connection() as conn:
|
||||
review = conn.execute(
|
||||
"UPDATE Review SET user_id = ?, song_id = ?, score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
|
||||
(
|
||||
review.user_id,
|
||||
review.song_id,
|
||||
review.score_song,
|
||||
review.score_show,
|
||||
review.score_costume,
|
||||
review.text_review,
|
||||
review.song_id,
|
||||
review.user_id,
|
||||
),
|
||||
).fetchdf()
|
||||
if review.empty:
|
||||
raise HTTPException(status_code=404, detail="Review not found")
|
||||
return review.to_dict(orient="records")[0]
|
||||
|
||||
Reference in New Issue
Block a user