File size: 4,904 Bytes
f3997d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
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")