Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.4 KB
Raw Blame
use std::sync::Arc;

use axum::extract::{Path, Query, State};
use axum::Json;
use dbx_core::history::{HistoryConnectionOption, HistoryEntry, HistorySearchRequest, HistorySearchResult};
use serde::Deserialize;

use crate::error::AppError;
use crate::state::WebState;

#[derive(Deserialize)]
pub struct HistoryQuery {
    pub limit: Option<usize>,
    pub offset: Option<usize>,
    pub activity_kind: Option<String>,
}

#[derive(Deserialize)]
pub struct HistoryClearQuery {
    pub source: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SaveHistoryRequest {
    pub entry: HistoryEntry,
}

pub async fn save_history(
    State(state): State<Arc<WebState>>,
    Json(body): Json<SaveHistoryRequest>,
) -> Result<Json<()>, AppError> {
    state.app.storage.save_history_entry(&body.entry).await.map_err(AppError::from)?;
    Ok(Json(()))
}

pub async fn load_history(
    State(state): State<Arc<WebState>>,
    Query(q): Query<HistoryQuery>,
) -> Result<Json<Vec<HistoryEntry>>, AppError> {
    let limit = q.limit.unwrap_or(100);
    let offset = q.offset.unwrap_or(0);
    let entries =
        state.app.storage.load_history_entries(limit, offset, q.activity_kind).await.map_err(AppError::from)?;
    Ok(Json(entries))
}

pub async fn search_history(
    State(state): State<Arc<WebState>>,
    Json(request): Json<HistorySearchRequest>,
) -> Result<Json<HistorySearchResult>, AppError> {
    state.app.storage.search_history_entries(request).await.map(Json).map_err(AppError::from)
}

pub async fn load_history_connection_options(
    State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<HistoryConnectionOption>>, AppError> {
    state.app.storage.load_history_connection_options().await.map(Json).map_err(AppError::from)
}

pub async fn clear_history(
    State(state): State<Arc<WebState>>,
    Query(query): Query<HistoryClearQuery>,
) -> Result<Json<()>, AppError> {
    if let Some(source) = query.source.as_deref().filter(|source| !source.trim().is_empty()) {
        state.app.storage.clear_history_by_source(source).await.map_err(AppError::from)?;
    } else {
        state.app.storage.clear_history().await.map_err(AppError::from)?;
    }
    Ok(Json(()))
}

pub async fn delete_history_entry(
    State(state): State<Arc<WebState>>,
    Path(id): Path<String>,
) -> Result<Json<()>, AppError> {
    state.app.storage.delete_history_entry(&id).await.map_err(AppError::from)?;
    Ok(Json(()))
}