update code level
This commit is contained in:
@@ -104,9 +104,7 @@ impl Database {
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
// 相册操作
|
||||
pub async fn get_all_albums(&self, page: Option<usize>, limit: Option<usize>) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
pub async fn get_albums(&self, page: Option<usize>, limit: Option<usize>) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
let page = page.unwrap_or(1);
|
||||
let limit = limit.unwrap_or(200);
|
||||
let offset = (page - 1) * limit;
|
||||
@@ -157,6 +155,50 @@ impl Database {
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
// 相册操作
|
||||
pub async fn get_all_albums(&self) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
|
||||
let albums = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
document,
|
||||
description,
|
||||
cover_image_id,
|
||||
0 as image_count,
|
||||
created_at
|
||||
FROM albums
|
||||
ORDER BY document DESC
|
||||
"#
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for row in albums {
|
||||
let id_str = row.get::<String, _>("id");
|
||||
let id = match Uuid::parse_str(&id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的UUID格式: {} - 错误: {}", id_str, e);
|
||||
continue; // 跳过无效的记录
|
||||
}
|
||||
};
|
||||
|
||||
result.push(AlbumInfo {
|
||||
id,
|
||||
title: row.get("title"),
|
||||
document: row.get("document"),
|
||||
description: row.get("description"),
|
||||
cover_image_id: row.get("cover_image_id"),
|
||||
image_count: row.get("image_count"),
|
||||
created_at: row.get::<String, _>("created_at"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn search_albums(&self, query: &str, page: Option<usize>, limit: Option<usize>) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
let search_pattern = format!("%{}%", query);
|
||||
|
||||
@@ -26,7 +26,7 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>) -> Result<
|
||||
}
|
||||
|
||||
// 获取数据库中现有的相册列表
|
||||
let existing_albums = db.get_all_albums(None, None).await?;
|
||||
let existing_albums = db.get_all_albums().await?;
|
||||
let existing_titles: Vec<String> = existing_albums.iter()
|
||||
.map(|album| album.document.to_lowercase())
|
||||
.collect();
|
||||
|
||||
@@ -13,7 +13,7 @@ pub async fn get_albums(
|
||||
let page = query.get("page").and_then(|s| s.parse::<usize>().ok());
|
||||
let limit = query.get("limit").and_then(|s| s.parse::<usize>().ok());
|
||||
|
||||
match db.get_all_albums(page, limit).await {
|
||||
match db.get_albums(page, limit).await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册列表失败: {}", e);
|
||||
@@ -206,7 +206,7 @@ pub async fn search_albums(
|
||||
let limit = query.get("limit").and_then(|s| s.parse::<usize>().ok());
|
||||
|
||||
if q.is_empty() {
|
||||
return match db.get_all_albums(page, limit).await {
|
||||
return match db.get_albums(page, limit).await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册列表失败: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user