You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB
Rust

//! Settings handlers.
//!
//! - `GET /api/v1/settings` — returns the current user's settings
//! - `PUT /api/v1/settings` — updates the current user's settings
use axum::extract::State;
use axum::response::IntoResponse;
use axum::Json;
use crate::app_state::AppState;
use crate::db;
use crate::errors::AppError;
use crate::middleware::auth::AuthUser;
use crate::models::settings::UpdateSettingsRequest;
/// `GET /api/v1/settings`
///
/// Returns the authenticated user's settings. Creates default settings
/// on first access if none exist.
pub async fn get_settings(
auth_user: AuthUser,
State(state): State<AppState>,
) -> Result<impl IntoResponse, AppError> {
let settings = db::settings::get_or_create_default(&state.pool, auth_user.id).await?;
Ok(Json(settings))
}
/// `PUT /api/v1/settings`
///
/// Updates the authenticated user's settings. Validates all fields
/// before persisting.
pub async fn update_settings(
auth_user: AuthUser,
State(state): State<AppState>,
Json(body): Json<UpdateSettingsRequest>,
) -> Result<impl IntoResponse, AppError> {
body.validate().map_err(AppError::Validation)?;
let settings = db::settings::upsert(&state.pool, auth_user.id, &body).await?;
tracing::info!(user_id = %auth_user.id, "Settings updated");
Ok(Json(settings))
}