update
Some checks failed
Rust CI / Build Release (push) Has been cancelled
Rust CI / Cargo Check (push) Has been cancelled

This commit is contained in:
lychang
2026-07-19 15:04:22 +08:00
parent b915e69222
commit 82653c1a6c
40 changed files with 7778 additions and 1 deletions

132
src/db/album.rs Normal file
View File

@@ -0,0 +1,132 @@
use super::Database;
use crate::models::*;
use sqlx::Row;
use uuid::Uuid;
impl Database {
// ── 查询 ──
pub async fn get_all_albums(&self) -> Result<Vec<AlbumInfo>, sqlx::Error> {
let rows = sqlx::query(
r#"SELECT id, title, document, description, cover_image_id, image_count, created_at
FROM albums ORDER BY created_at DESC"#
).fetch_all(&self.pool).await?;
Ok(rows.iter().map(|r| {
let id_str = r.get::<String, _>("id");
AlbumInfo {
id: Uuid::parse_str(&id_str).unwrap_or_else(|_| Uuid::nil()),
title: r.get("title"),
document: r.get("document"),
description: r.get("description"),
cover_image_id: r.get("cover_image_id"),
image_count: r.get("image_count"),
created_at: r.get("created_at"),
}
}).collect())
}
pub async fn get_album_with_images(&self, album_id: Uuid) -> Result<Option<AlbumWithImages>, sqlx::Error> {
let row = sqlx::query("SELECT * FROM albums WHERE id = ?")
.bind(album_id.to_string())
.fetch_optional(&self.pool).await?;
let Some(row) = row else { return Ok(None) };
let id_str = row.get::<String, _>("id");
let id = Uuid::parse_str(&id_str).unwrap_or_else(|_| Uuid::nil());
let cover = row.get::<Option<String>, _>("cover_image_id")
.and_then(|s| Uuid::parse_str(&s).ok());
let album = Album {
id,
title: row.get("title"),
description: row.get("description"),
cover_image_id: cover,
image_count: row.get("image_count"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
person_name_cleaned: row.get("person_name_cleaned"),
tags: row.get("tags"),
};
let img_rows = sqlx::query("SELECT * FROM images WHERE album_id = ?")
.bind(album_id.to_string()).fetch_all(&self.pool).await?;
let mut images = Vec::new();
for r in &img_rows {
let img_id = Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil());
let aid = Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil());
images.push(super::image::Image {
id: img_id, album_id: aid,
filename: r.get("filename"),
original_url: r.get("original_url"),
title: r.get("title"),
description: r.get("description"),
file_size: r.get("file_size"),
width: r.get("width"),
height: r.get("height"),
mime_type: r.get("mime_type"),
created_at: r.get("created_at"),
});
}
Ok(Some(AlbumWithImages { album, images: images.clone(), image_count: images.len() as i64 }))
}
pub async fn get_all_albums_full(&self) -> Vec<AlbumMeta> {
let has_col = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM pragma_table_info('albums') WHERE name = 'person_name_original'"
).fetch_one(&self.pool).await.unwrap_or(0);
if has_col == 0 { return Vec::new(); }
let rows = sqlx::query(
r#"SELECT id, title, person_name_original, person_name_cleaned, tags, all_words, raw_title, processed_at, image_count
FROM albums ORDER BY created_at DESC"#
).fetch_all(&self.pool).await.unwrap_or_default();
rows.iter().map(|r| AlbumMeta {
id: r.get("id"), title: r.get("title"),
person_name_original: r.get("person_name_original"),
person_name_cleaned: r.get("person_name_cleaned"),
tags: r.get("tags"), all_words: r.get("all_words"),
raw_title: r.get("raw_title"), processed_at: r.get("processed_at"),
image_count: r.get("image_count"),
}).collect()
}
// ── 写入 ──
pub async fn create_album(&self, album: CreateAlbum) -> Result<Uuid, sqlx::Error> {
let id = Uuid::new_v4();
let now = chrono::Utc::now().to_rfc3339();
sqlx::query(
"INSERT INTO albums (id, title, document, description, image_count, created_at, updated_at) VALUES (?, ?, ?, ?, 0, ?, ?)"
).bind(id.to_string()).bind(album.title).bind(album.document).bind(album.description).bind(&now).bind(&now)
.execute(&self.pool).await?;
Ok(id)
}
pub async fn delete_album(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM albums WHERE id = ?").bind(album_id.to_string()).execute(&self.pool).await?;
Ok(())
}
pub async fn update_album_cover(&self, album_id: Uuid, image_id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query("UPDATE albums SET cover_image_id = ? WHERE id = ?")
.bind(image_id.to_string()).bind(album_id.to_string()).execute(&self.pool).await?;
Ok(())
}
pub async fn update_album_image_count(&self, album_id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query(
r#"UPDATE albums SET image_count = (SELECT COUNT(*) FROM images WHERE album_id = ?), updated_at = datetime('now') WHERE id = ?"#
).bind(album_id.to_string()).bind(album_id.to_string()).execute(&self.pool).await?;
Ok(())
}
pub async fn get_album_image_count(&self, album_id: Uuid) -> Result<i64, sqlx::Error> {
sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM images WHERE album_id = ?")
.bind(album_id.to_string()).fetch_one(&self.pool).await
}
}