92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.responses import Response
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from auth_utils import get_current_user
|
|
from groups_db_utils import (
|
|
add_or_update_rating,
|
|
get_recipe_rating_stats,
|
|
get_user_recipe_rating,
|
|
add_comment,
|
|
get_recipe_comments,
|
|
update_comment,
|
|
delete_comment,
|
|
)
|
|
|
|
router = APIRouter(tags=["ratings-comments"])
|
|
|
|
|
|
class RatingCreate(BaseModel):
|
|
rating: int
|
|
|
|
|
|
class CommentCreate(BaseModel):
|
|
content: str
|
|
parent_comment_id: Optional[int] = None
|
|
|
|
|
|
@router.post("/recipes/{recipe_id}/rating")
|
|
def rate_recipe_endpoint(
|
|
recipe_id: int,
|
|
data: RatingCreate,
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""Add or update rating for a recipe"""
|
|
if data.rating < 1 or data.rating > 5:
|
|
raise HTTPException(status_code=400, detail="Rating must be between 1 and 5")
|
|
return add_or_update_rating(recipe_id, current_user["user_id"], data.rating)
|
|
|
|
|
|
@router.get("/recipes/{recipe_id}/rating/stats")
|
|
def get_recipe_rating_stats_endpoint(recipe_id: int):
|
|
"""Get rating statistics for a recipe"""
|
|
return get_recipe_rating_stats(recipe_id)
|
|
|
|
|
|
@router.get("/recipes/{recipe_id}/rating/mine")
|
|
def get_my_recipe_rating_endpoint(
|
|
recipe_id: int,
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""Get current user's rating for a recipe"""
|
|
rating = get_user_recipe_rating(recipe_id, current_user["user_id"])
|
|
if not rating:
|
|
return {"rating": None}
|
|
return rating
|
|
|
|
|
|
@router.post("/recipes/{recipe_id}/comments")
|
|
def add_comment_endpoint(
|
|
recipe_id: int,
|
|
data: CommentCreate,
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""Add a comment to a recipe"""
|
|
return add_comment(recipe_id, current_user["user_id"], data.content, data.parent_comment_id)
|
|
|
|
|
|
@router.get("/recipes/{recipe_id}/comments")
|
|
def get_comments_endpoint(recipe_id: int):
|
|
"""Get all comments for a recipe"""
|
|
return get_recipe_comments(recipe_id)
|
|
|
|
|
|
@router.patch("/comments/{comment_id}")
|
|
def update_comment_endpoint(
|
|
comment_id: int,
|
|
data: CommentCreate,
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""Update a comment"""
|
|
return update_comment(comment_id, current_user["user_id"], data.content)
|
|
|
|
|
|
@router.delete("/comments/{comment_id}")
|
|
def delete_comment_endpoint(
|
|
comment_id: int,
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""Delete a comment"""
|
|
delete_comment(comment_id, current_user["user_id"])
|
|
return Response(status_code=204)
|