summaryrefslogtreecommitdiff
path: root/makima/src/server/handlers/settings.rs
blob: ae52d5a45b97dc64db9daecadacf2bb5c2409689 (plain) (blame)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! HTTP handlers for user settings (feature flags / preferences).

use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::IntoResponse,
    Json,
};

use crate::db::models::{UpsertUserSettingRequest, UserSettingsResponse};
use crate::db::repository;
use crate::server::auth::Authenticated;
use crate::server::messages::ApiError;
use crate::server::state::SharedState;

/// List all settings for the authenticated user.
#[utoipa::path(
    get,
    path = "/api/v1/settings",
    responses(
        (status = 200, description = "User settings", body = UserSettingsResponse),
        (status = 401, description = "Not authenticated", body = ApiError),
        (status = 503, description = "Database not configured", body = ApiError),
        (status = 500, description = "Internal server error", body = ApiError),
    ),
    security(
        ("bearer_auth" = [])
    ),
    tag = "Settings"
)]
pub async fn list_settings(
    State(state): State<SharedState>,
    Authenticated(user): Authenticated,
) -> impl IntoResponse {
    let Some(ref pool) = state.db_pool else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")),
        )
            .into_response();
    };

    match repository::get_user_settings(pool, user.owner_id).await {
        Ok(settings) => Json(UserSettingsResponse { settings }).into_response(),
        Err(e) => {
            tracing::error!("Failed to list user settings: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ApiError::new("DB_ERROR", e.to_string())),
            )
                .into_response()
        }
    }
}

/// Get a specific setting by key.
#[utoipa::path(
    get,
    path = "/api/v1/settings/{key}",
    params(
        ("key" = String, Path, description = "Setting key")
    ),
    responses(
        (status = 200, description = "User setting", body = crate::db::models::UserSetting),
        (status = 401, description = "Not authenticated", body = ApiError),
        (status = 404, description = "Setting not found", body = ApiError),
        (status = 503, description = "Database not configured", body = ApiError),
        (status = 500, description = "Internal server error", body = ApiError),
    ),
    security(
        ("bearer_auth" = [])
    ),
    tag = "Settings"
)]
pub async fn get_setting(
    State(state): State<SharedState>,
    Authenticated(user): Authenticated,
    Path(key): Path<String>,
) -> impl IntoResponse {
    let Some(ref pool) = state.db_pool else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")),
        )
            .into_response();
    };

    match repository::get_user_setting(pool, user.owner_id, &key).await {
        Ok(Some(setting)) => Json(setting).into_response(),
        Ok(None) => (
            StatusCode::NOT_FOUND,
            Json(ApiError::new("NOT_FOUND", format!("Setting '{}' not found", key))),
        )
            .into_response(),
        Err(e) => {
            tracing::error!("Failed to get user setting: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ApiError::new("DB_ERROR", e.to_string())),
            )
                .into_response()
        }
    }
}

/// Upsert a user setting (create or update).
#[utoipa::path(
    put,
    path = "/api/v1/settings",
    request_body = UpsertUserSettingRequest,
    responses(
        (status = 200, description = "Setting upserted", body = crate::db::models::UserSetting),
        (status = 401, description = "Not authenticated", body = ApiError),
        (status = 503, description = "Database not configured", body = ApiError),
        (status = 500, description = "Internal server error", body = ApiError),
    ),
    security(
        ("bearer_auth" = [])
    ),
    tag = "Settings"
)]
pub async fn upsert_setting(
    State(state): State<SharedState>,
    Authenticated(user): Authenticated,
    Json(req): Json<UpsertUserSettingRequest>,
) -> impl IntoResponse {
    let Some(ref pool) = state.db_pool else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")),
        )
            .into_response();
    };

    match repository::upsert_user_setting(pool, user.owner_id, &req.key, &req.value).await {
        Ok(setting) => Json(setting).into_response(),
        Err(e) => {
            tracing::error!("Failed to upsert user setting: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ApiError::new("DB_ERROR", e.to_string())),
            )
                .into_response()
        }
    }
}

/// Delete a user setting by key.
#[utoipa::path(
    delete,
    path = "/api/v1/settings/{key}",
    params(
        ("key" = String, Path, description = "Setting key")
    ),
    responses(
        (status = 200, description = "Setting deleted"),
        (status = 401, description = "Not authenticated", body = ApiError),
        (status = 404, description = "Setting not found", body = ApiError),
        (status = 503, description = "Database not configured", body = ApiError),
        (status = 500, description = "Internal server error", body = ApiError),
    ),
    security(
        ("bearer_auth" = [])
    ),
    tag = "Settings"
)]
pub async fn delete_setting(
    State(state): State<SharedState>,
    Authenticated(user): Authenticated,
    Path(key): Path<String>,
) -> impl IntoResponse {
    let Some(ref pool) = state.db_pool else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")),
        )
            .into_response();
    };

    match repository::delete_user_setting(pool, user.owner_id, &key).await {
        Ok(true) => StatusCode::OK.into_response(),
        Ok(false) => (
            StatusCode::NOT_FOUND,
            Json(ApiError::new("NOT_FOUND", format!("Setting '{}' not found", key))),
        )
            .into_response(),
        Err(e) => {
            tracing::error!("Failed to delete user setting: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ApiError::new("DB_ERROR", e.to_string())),
            )
                .into_response()
        }
    }
}