Spaces:
Running
Running
# app/routers/preferences.py | |
from fastapi import APIRouter, Depends, HTTPException | |
from pydantic import BaseModel | |
from typing import Dict, Any | |
from app.database.database_query import DatabaseQuery | |
from app.middleware.auth import get_current_user | |
router = APIRouter() | |
query = DatabaseQuery() | |
class ThemeSettings(BaseModel): | |
theme: bool | |
async def get_preferences(username: str = Depends(get_current_user)): | |
try: | |
user_preferences = query.get_user_preferences(username) | |
return {'preferences': user_preferences} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def set_preferences(preferences: Dict[str, Any], username: str = Depends(get_current_user)): | |
try: | |
preferences_result = query.set_user_preferences(username, preferences) | |
return { | |
'message': 'Preferences updated successfully', | |
'preferences': preferences_result | |
} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def get_theme(username: str = Depends(get_current_user)): | |
try: | |
user_theme = query.get_user_theme(username) | |
return {'theme': user_theme} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def set_theme(theme_data: ThemeSettings, username: str = Depends(get_current_user)): | |
try: | |
theme = theme_data.theme | |
theme_data = query.set_user_theme(username, theme) | |
return { | |
'message': 'Theme updated successfully', | |
'theme': theme_data['theme'] | |
} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) |