buildersai / app /routes /settings.py
Kushal
Initial deployment: FastAPI backend with Docker
f3997d4
Raw
History Blame Contribute Delete
4.9 kB
"""
API routes for user settings.
"""
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from pydantic import BaseModel
from typing import Optional
from app.database.connection import get_db
from app.database.models import User
from app.middleware.auth import get_current_user
from app.services.settings_service import settings_service
router = APIRouter(prefix="/api/settings", tags=["settings"])
class ProfileUpdateRequest(BaseModel):
"""Request schema for profile updates."""
bio: Optional[str] = None
phone: Optional[str] = None
company: Optional[str] = None
class AppearanceUpdateRequest(BaseModel):
"""Request schema for appearance updates."""
theme: str # light, dark, system
class NotificationUpdateRequest(BaseModel):
"""Request schema for notification updates."""
email_notifications: Optional[bool] = None
update_notifications: Optional[bool] = None
@router.get("")
async def get_settings(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Get current user settings.
Returns:
User settings including profile, appearance, and notifications
"""
try:
settings = settings_service.get_or_create_settings(db, current_user.id)
return {
"profile": {
"name": current_user.name,
"email": current_user.email,
"bio": settings.bio,
"phone": settings.phone,
"company": settings.company
},
"appearance": {
"theme": settings.theme
},
"notifications": {
"email_notifications": bool(settings.email_notifications),
"update_notifications": bool(settings.update_notifications)
}
}
except Exception as e:
print(f"Error getting settings: {e}")
raise HTTPException(status_code=500, detail="Error retrieving settings")
@router.patch("/profile")
async def update_profile(
request: ProfileUpdateRequest,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Update user profile settings.
Args:
request: Profile update data
Returns:
Updated profile settings
"""
try:
settings = settings_service.update_profile(
db,
current_user.id,
bio=request.bio,
phone=request.phone,
company=request.company
)
return {
"success": True,
"profile": {
"bio": settings.bio,
"phone": settings.phone,
"company": settings.company
}
}
except Exception as e:
print(f"Error updating profile: {e}")
raise HTTPException(status_code=500, detail="Error updating profile")
@router.patch("/appearance")
async def update_appearance(
request: AppearanceUpdateRequest,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Update appearance settings.
Args:
request: Appearance update data
Returns:
Updated appearance settings
"""
try:
settings = settings_service.update_appearance(
db,
current_user.id,
theme=request.theme
)
return {
"success": True,
"appearance": {
"theme": settings.theme
}
}
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
print(f"Error updating appearance: {e}")
raise HTTPException(status_code=500, detail="Error updating appearance")
@router.patch("/notifications")
async def update_notifications(
request: NotificationUpdateRequest,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Update notification settings.
Args:
request: Notification update data
Returns:
Updated notification settings
"""
try:
settings = settings_service.update_notifications(
db,
current_user.id,
email_notifications=request.email_notifications,
update_notifications=request.update_notifications
)
return {
"success": True,
"notifications": {
"email_notifications": bool(settings.email_notifications),
"update_notifications": bool(settings.update_notifications)
}
}
except Exception as e:
print(f"Error updating notifications: {e}")
raise HTTPException(status_code=500, detail="Error updating notifications")