add book_count feature, migrate backoffice to server actions, fix healthcheck

This commit is contained in:
2026-03-05 21:29:48 +01:00
parent 3a96f6ba36
commit ef8a755a83
13 changed files with 222 additions and 79 deletions

View File

@@ -59,6 +59,57 @@ pub async fn list_index_jobs(State(state): State<AppState>) -> Result<Json<Vec<I
Ok(Json(rows.into_iter().map(map_row).collect()))
}
pub async fn cancel_job(
State(state): State<AppState>,
id: axum::extract::Path<Uuid>,
) -> Result<Json<IndexJobItem>, ApiError> {
let rows_affected = sqlx::query(
"UPDATE index_jobs SET status = 'cancelled' WHERE id = $1 AND status IN ('pending', 'running')",
)
.bind(id.0)
.execute(&state.pool)
.await?;
if rows_affected.rows_affected() == 0 {
return Err(ApiError::not_found("job not found or already finished"));
}
let row = sqlx::query(
"SELECT id, library_id, type, status, started_at, finished_at, stats_json, error_opt, created_at FROM index_jobs WHERE id = $1",
)
.bind(id.0)
.fetch_one(&state.pool)
.await?;
Ok(Json(map_row(row)))
}
#[derive(Serialize)]
pub struct FolderItem {
pub name: String,
pub path: String,
}
pub async fn list_folders(State(_state): State<AppState>) -> Result<Json<Vec<FolderItem>>, ApiError> {
let libraries_path = std::path::Path::new("/libraries");
let mut folders = Vec::new();
if let Ok(entries) = std::fs::read_dir(libraries_path) {
for entry in entries.flatten() {
if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
let name = entry.file_name().to_string_lossy().to_string();
folders.push(FolderItem {
name: name.clone(),
path: format!("/libraries/{}", name),
});
}
}
}
folders.sort_by(|a, b| a.name.cmp(&b.name));
Ok(Json(folders))
}
fn map_row(row: sqlx::postgres::PgRow) -> IndexJobItem {
IndexJobItem {
id: row.get("id"),

View File

@@ -13,6 +13,7 @@ pub struct LibraryDto {
pub name: String,
pub root_path: String,
pub enabled: bool,
pub book_count: i64,
}
#[derive(Deserialize)]
@@ -22,7 +23,11 @@ pub struct CreateLibraryInput {
}
pub async fn list_libraries(State(state): State<AppState>) -> Result<Json<Vec<LibraryDto>>, ApiError> {
let rows = sqlx::query("SELECT id, name, root_path, enabled FROM libraries ORDER BY created_at DESC")
let rows = sqlx::query(
"SELECT l.id, l.name, l.root_path, l.enabled,
(SELECT COUNT(*) FROM books b WHERE b.library_id = l.id) as book_count
FROM libraries l ORDER BY l.created_at DESC"
)
.fetch_all(&state.pool)
.await?;
@@ -33,6 +38,7 @@ pub async fn list_libraries(State(state): State<AppState>) -> Result<Json<Vec<Li
name: row.get("name"),
root_path: row.get("root_path"),
enabled: row.get("enabled"),
book_count: row.get("book_count"),
})
.collect();
@@ -65,6 +71,7 @@ pub async fn create_library(
name: input.name.trim().to_string(),
root_path,
enabled: true,
book_count: 0,
}))
}

View File

@@ -94,6 +94,8 @@ async fn main() -> anyhow::Result<()> {
.route("/libraries/:id", delete(libraries::delete_library))
.route("/index/rebuild", axum::routing::post(index_jobs::enqueue_rebuild))
.route("/index/status", get(index_jobs::list_index_jobs))
.route("/index/cancel/:id", axum::routing::post(index_jobs::cancel_job))
.route("/folders", get(index_jobs::list_folders))
.route("/admin/tokens", get(tokens::list_tokens).post(tokens::create_token))
.route("/admin/tokens/:id", delete(tokens::revoke_token))
.route_layer(middleware::from_fn_with_state(