Files
daily-journal-prompt/backend/app/api/v1/endpoints/feedback.py
2026-01-03 11:18:56 -07:00

132 lines
3.7 KiB
Python

"""
Feedback-related API endpoints.
"""
from typing import List, Dict
from fastapi import APIRouter, HTTPException, Depends, status
from pydantic import BaseModel
from app.services.prompt_service import PromptService
from app.models.prompt import FeedbackWord, RateFeedbackWordsRequest, RateFeedbackWordsResponse
# Create router
router = APIRouter()
# Response models
class GenerateFeedbackWordsResponse(BaseModel):
"""Response model for generating feedback words."""
theme_words: List[str]
count: int = 6
# Service dependency
async def get_prompt_service() -> PromptService:
"""Dependency to get PromptService instance."""
return PromptService()
@router.get("/generate", response_model=GenerateFeedbackWordsResponse)
async def generate_feedback_words(
prompt_service: PromptService = Depends(get_prompt_service)
):
"""
Generate 6 theme feedback words using AI.
Returns:
List of 6 theme words for feedback
"""
try:
theme_words = await prompt_service.generate_theme_feedback_words()
if len(theme_words) != 6:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Expected 6 theme words, got {len(theme_words)}"
)
return GenerateFeedbackWordsResponse(
theme_words=theme_words,
count=len(theme_words)
)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error generating feedback words: {str(e)}"
)
@router.post("/rate", response_model=RateFeedbackWordsResponse)
async def rate_feedback_words(
request: RateFeedbackWordsRequest,
prompt_service: PromptService = Depends(get_prompt_service)
):
"""
Rate feedback words and update feedback system.
Args:
request: Dictionary of word to rating (0-6)
Returns:
Updated feedback words
"""
try:
feedback_words = await prompt_service.update_feedback_words(request.ratings)
return RateFeedbackWordsResponse(
feedback_words=feedback_words,
added_to_history=True
)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error rating feedback words: {str(e)}"
)
@router.get("/current", response_model=List[FeedbackWord])
async def get_current_feedback_words(
prompt_service: PromptService = Depends(get_prompt_service)
):
"""
Get current feedback words with weights.
Returns:
List of current feedback words with weights
"""
try:
# This would need to be implemented in PromptService
# For now, return empty list
return []
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error getting current feedback words: {str(e)}"
)
@router.get("/history")
async def get_feedback_history(
prompt_service: PromptService = Depends(get_prompt_service)
):
"""
Get feedback word history.
Returns:
List of historic feedback words
"""
try:
# This would need to be implemented in PromptService
# For now, return empty list
return []
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error getting feedback history: {str(e)}"
)