use axum::{
extract::{Path, Query, State},
http::StatusCode,
Json,
};
use serde::Deserialize;
use crate::db::models::{CredentialCreateRequest, CredentialListResponse, CredentialResponse};
use crate::error::AppResult;
use crate::services::CredentialService;
#[derive(Debug, Deserialize, Default)]
pub struct ListCredentialsQuery {
#[serde(rename = "type")]
pub credential_type: Option<String>,
pub q: Option<String>,
}
#[derive(Debug, Deserialize, Default)]
pub struct GetCredentialQuery {
#[serde(default)]
pub include_data: bool,
pub execution_id: Option<i64>,
pub parent_execution_id: Option<i64>,
}
pub async fn create_or_update(
State(service): State<CredentialService>,
Json(request): Json<CredentialCreateRequest>,
) -> AppResult<(StatusCode, Json<CredentialResponse>)> {
let response = service.create_or_update(request).await?;
Ok((StatusCode::OK, Json(response)))
}
pub async fn list(
State(service): State<CredentialService>,
Query(query): Query<ListCredentialsQuery>,
) -> AppResult<Json<CredentialListResponse>> {
let response = service
.list(query.credential_type.as_deref(), query.q.as_deref())
.await?;
Ok(Json(response))
}
pub async fn get(
State(service): State<CredentialService>,
Path(identifier): Path<String>,
Query(query): Query<GetCredentialQuery>,
) -> AppResult<Json<CredentialResponse>> {
let response = service.get(&identifier, query.include_data).await?;
Ok(Json(response))
}
pub async fn delete(
State(service): State<CredentialService>,
Path(identifier): Path<String>,
) -> AppResult<Json<serde_json::Value>> {
let id = service.delete(&identifier).await?;
Ok(Json(serde_json::json!({
"message": "Credential deleted successfully",
"id": id
})))
}