
- Setup project structure with FastAPI - Create database models for users, gifts, preferences, and recommendations - Configure SQLite database with SQLAlchemy ORM - Setup Alembic for database migrations - Implement user authentication with JWT - Create API endpoints for users, gifts, preferences, and recommendations - Integrate OpenAI API for gift recommendations - Add comprehensive documentation
170 lines
5.6 KiB
Python
170 lines
5.6 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.deps import get_current_active_user, get_db
|
|
from app.models.preference import Preference
|
|
from app.models.recommendation import Recommendation
|
|
from app.models.user import User
|
|
from app.schemas.recommendation import Recommendation as RecommendationSchema
|
|
from app.schemas.recommendation import RecommendationCreate, RecommendationUpdate
|
|
from app.services.ai_recommendation import AIRecommendationService
|
|
|
|
router = APIRouter()
|
|
recommendation_service = AIRecommendationService()
|
|
|
|
|
|
@router.get("", response_model=List[RecommendationSchema])
|
|
def read_recommendations(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
recipient_name: str = Query(None, description="Filter by recipient name"),
|
|
occasion: str = Query(None, description="Filter by occasion"),
|
|
saved: bool = Query(None, description="Filter by saved status"),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve recommendations.
|
|
"""
|
|
query = db.query(Recommendation).filter(Recommendation.user_id == current_user.id)
|
|
|
|
# Apply filters if provided
|
|
if recipient_name:
|
|
query = query.filter(Recommendation.recipient_name == recipient_name)
|
|
if occasion:
|
|
query = query.filter(Recommendation.occasion == occasion)
|
|
if saved is not None:
|
|
query = query.filter(Recommendation.saved == saved)
|
|
|
|
# Apply pagination
|
|
recommendations = query.offset(skip).limit(limit).all()
|
|
return recommendations
|
|
|
|
|
|
@router.post("", response_model=RecommendationSchema)
|
|
async def create_recommendation(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
recommendation_in: RecommendationCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new recommendation using AI.
|
|
"""
|
|
# Get preferences if available
|
|
preferences = None
|
|
if recommendation_in.preferences_id:
|
|
preferences = db.query(Preference).filter(
|
|
Preference.id == recommendation_in.preferences_id,
|
|
Preference.user_id == current_user.id
|
|
).first()
|
|
if not preferences:
|
|
raise HTTPException(status_code=404, detail="Preference not found")
|
|
else:
|
|
# Try to find preferences by recipient name
|
|
preferences = db.query(Preference).filter(
|
|
Preference.recipient_name == recommendation_in.recipient_name,
|
|
Preference.user_id == current_user.id
|
|
).first()
|
|
|
|
# Generate recommendation using AI
|
|
ai_recommendation = await recommendation_service.generate_recommendations(
|
|
recipient_name=recommendation_in.recipient_name,
|
|
occasion=recommendation_in.occasion,
|
|
preferences=preferences,
|
|
budget_min=recommendation_in.budget_min,
|
|
budget_max=recommendation_in.budget_max,
|
|
additional_info=recommendation_in.additional_info,
|
|
)
|
|
|
|
# Create recommendation in database
|
|
recommendation = Recommendation(
|
|
user_id=current_user.id,
|
|
recipient_name=recommendation_in.recipient_name,
|
|
occasion=recommendation_in.occasion,
|
|
recommendation_text=ai_recommendation["recommendation_text"],
|
|
item_name=ai_recommendation["item_name"],
|
|
description=ai_recommendation["description"],
|
|
price_estimate=ai_recommendation["price_estimate"],
|
|
purchase_url=ai_recommendation["purchase_url"],
|
|
saved=False,
|
|
)
|
|
|
|
db.add(recommendation)
|
|
db.commit()
|
|
db.refresh(recommendation)
|
|
return recommendation
|
|
|
|
|
|
@router.put("/{id}", response_model=RecommendationSchema)
|
|
def update_recommendation(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int = Path(..., description="The ID of the recommendation to update"),
|
|
recommendation_in: RecommendationUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a recommendation.
|
|
"""
|
|
recommendation = db.query(Recommendation).filter(
|
|
Recommendation.id == id,
|
|
Recommendation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not recommendation:
|
|
raise HTTPException(status_code=404, detail="Recommendation not found")
|
|
|
|
update_data = recommendation_in.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(recommendation, field, value)
|
|
|
|
db.add(recommendation)
|
|
db.commit()
|
|
db.refresh(recommendation)
|
|
return recommendation
|
|
|
|
|
|
@router.get("/{id}", response_model=RecommendationSchema)
|
|
def read_recommendation(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int = Path(..., description="The ID of the recommendation to get"),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get recommendation by ID.
|
|
"""
|
|
recommendation = db.query(Recommendation).filter(
|
|
Recommendation.id == id,
|
|
Recommendation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not recommendation:
|
|
raise HTTPException(status_code=404, detail="Recommendation not found")
|
|
return recommendation
|
|
|
|
|
|
@router.delete("/{id}", status_code=204, response_model=None)
|
|
def delete_recommendation(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int = Path(..., description="The ID of the recommendation to delete"),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a recommendation.
|
|
"""
|
|
recommendation = db.query(Recommendation).filter(
|
|
Recommendation.id == id,
|
|
Recommendation.user_id == current_user.id
|
|
).first()
|
|
|
|
if not recommendation:
|
|
raise HTTPException(status_code=404, detail="Recommendation not found")
|
|
|
|
db.delete(recommendation)
|
|
db.commit()
|
|
return None |