feat(presentation): all handlers

This commit is contained in:
2026-05-14 04:00:04 +02:00
parent fb39ea2469
commit 38106ecdb6
5 changed files with 190 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
use axum::{extract::{Path, State}, http::StatusCode, Json};
use uuid::Uuid;
use api_types::{requests::CreateApiKeyRequest, responses::ApiKeyResponse};
use application::use_cases::api_keys::{create_api_key, delete_api_key, list_api_keys};
use domain::value_objects::ApiKeyId;
use crate::{errors::ApiError, extractors::AuthUser, state::AppState};
pub async fn get_api_keys(State(s): State<AppState>, AuthUser(uid): AuthUser) -> Result<Json<Vec<ApiKeyResponse>>, ApiError> {
let keys = list_api_keys(&*s.api_keys, &uid).await?;
Ok(Json(keys.into_iter().map(|k| ApiKeyResponse { id: k.id.as_uuid(), name: k.name, created_at: k.created_at }).collect()))
}
pub async fn post_api_key(State(s): State<AppState>, AuthUser(uid): AuthUser, Json(body): Json<CreateApiKeyRequest>) -> Result<Json<serde_json::Value>, ApiError> {
let (key, raw) = create_api_key(&*s.api_keys, &uid, body.name).await?;
Ok(Json(serde_json::json!({ "id": key.id.as_uuid(), "name": key.name, "key": raw })))
}
pub async fn delete_api_key_handler(State(s): State<AppState>, AuthUser(uid): AuthUser, Path(id): Path<Uuid>) -> Result<StatusCode, ApiError> {
delete_api_key(&*s.api_keys, &uid, &ApiKeyId::from_uuid(id)).await?;
Ok(StatusCode::NO_CONTENT)
}