use std::sync::Arc;
use axum::Json;
use axum::extract::State;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use crate::AppState;
use crate::routes::internal_error;
async fn run<T, F>(state: Arc<AppState>, repair: F) -> Response
where
T: Serialize + Send + 'static,
F: FnOnce(&second_brain_core::sqlite_store::SqliteStore) -> anyhow::Result<T> + Send + 'static,
{
let result = tokio::task::spawn_blocking(move || repair(&state.sqlite)).await;
match result {
Ok(Ok(outcome)) => Json(outcome).into_response(),
Ok(Err(e)) => internal_error(e),
Err(e) => internal_error(e),
}
}
pub async fn refile_nested_projects(State(state): State<Arc<AppState>>) -> Response {
run(state, |store| {
Ok(serde_json::json!({
"memories_refiled": store.refile_nested_transcript_projects()?
}))
})
.await
}
pub async fn restore_explicit(State(state): State<Arc<AppState>>) -> Response {
run(state, |store| store.restore_explicit_memories()).await
}
pub async fn refile_stray_projects(State(state): State<Arc<AppState>>) -> Response {
run(state, |store| store.refile_stray_project_slugs()).await
}