Add message model
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class Message(BaseModel):
|
||||||
|
type: str | None = None
|
||||||
|
message: str
|
||||||
|
timestamp: datetime = Field(default_factory=datetime.now)
|
||||||
+28
-13
@@ -1,6 +1,7 @@
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
from models.review import ReviewOut, ReviewSearch, ReviewIn
|
from models.review import ReviewOut, ReviewIn
|
||||||
|
from models.msg import Message
|
||||||
from lib.db import get_connection
|
from lib.db import get_connection
|
||||||
|
|
||||||
router = APIRouter(prefix="/reviews", tags=["reviews"])
|
router = APIRouter(prefix="/reviews", tags=["reviews"])
|
||||||
@@ -16,11 +17,11 @@ async def list_reviews():
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/", response_model=ReviewOut)
|
@router.get("/", response_model=ReviewOut)
|
||||||
async def get_review(review_id: ReviewSearch):
|
async def get_review(song_id: int, user_id: int):
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
review = conn.execute(
|
review = conn.execute(
|
||||||
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
||||||
(review_id.song_id, review_id.user_id),
|
(song_id, user_id),
|
||||||
).fetchdf()
|
).fetchdf()
|
||||||
if review.empty:
|
if review.empty:
|
||||||
raise HTTPException(status_code=404, detail="Review not found")
|
raise HTTPException(status_code=404, detail="Review not found")
|
||||||
@@ -30,7 +31,8 @@ async def get_review(review_id: ReviewSearch):
|
|||||||
@router.post("/", response_model=ReviewOut)
|
@router.post("/", response_model=ReviewOut)
|
||||||
async def create_review(review: ReviewIn):
|
async def create_review(review: ReviewIn):
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
review = conn.execute(
|
# Insert the review
|
||||||
|
conn.execute(
|
||||||
"INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) VALUES (?, ?, ?, ?, ?, ?)",
|
"INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
(
|
(
|
||||||
review.user_id,
|
review.user_id,
|
||||||
@@ -40,20 +42,26 @@ async def create_review(review: ReviewIn):
|
|||||||
review.score_costume,
|
review.score_costume,
|
||||||
review.text_review,
|
review.text_review,
|
||||||
),
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fetch the newly created review
|
||||||
|
result = conn.execute(
|
||||||
|
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
||||||
|
(review.song_id, review.user_id),
|
||||||
).fetchdf()
|
).fetchdf()
|
||||||
if review.empty:
|
|
||||||
|
if result.empty:
|
||||||
raise HTTPException(status_code=404, detail="Review not found")
|
raise HTTPException(status_code=404, detail="Review not found")
|
||||||
return review.to_dict(orient="records")[0]
|
return Message(type="success", message="Review created successfully")
|
||||||
|
|
||||||
|
|
||||||
@router.put("/", response_model=ReviewOut)
|
@router.put("/", response_model=Message)
|
||||||
async def update_review(review: ReviewIn):
|
async def update_review(review: ReviewIn):
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
review = conn.execute(
|
# Update the review
|
||||||
"UPDATE Review SET user_id = ?, song_id = ?, score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
|
conn.execute(
|
||||||
|
"UPDATE Review SET 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_song,
|
||||||
review.score_show,
|
review.score_show,
|
||||||
review.score_costume,
|
review.score_costume,
|
||||||
@@ -61,7 +69,14 @@ async def update_review(review: ReviewIn):
|
|||||||
review.song_id,
|
review.song_id,
|
||||||
review.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()
|
).fetchdf()
|
||||||
if review.empty:
|
|
||||||
|
if result.empty:
|
||||||
raise HTTPException(status_code=404, detail="Review not found")
|
raise HTTPException(status_code=404, detail="Review not found")
|
||||||
return review.to_dict(orient="records")[0]
|
return Message(type="success", message="Review updated successfully")
|
||||||
|
|||||||
Reference in New Issue
Block a user