update
This commit is contained in:
@@ -26,6 +26,98 @@ impl Database {
|
||||
}).collect())
|
||||
}
|
||||
|
||||
/// 组合过滤搜索(懒加载):仅返回符合条件的相册,避免全量加载
|
||||
///
|
||||
/// 支持按 tag / person / 标题关键词 任意组合过滤(AND 关系)。
|
||||
/// 参数传空字符串或 None 表示不过滤该维度。
|
||||
/// 全参数为空时等价于 get_all_albums。
|
||||
pub async fn search_albums_with_filters(
|
||||
&self,
|
||||
tag: Option<&str>,
|
||||
person: Option<&str>,
|
||||
title_q: Option<&str>,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<AlbumInfo>, sqlx::Error> {
|
||||
let has_tag = tag.map(|s| !s.is_empty()).unwrap_or(false);
|
||||
let has_person = person.map(|s| !s.is_empty()).unwrap_or(false);
|
||||
let has_q = title_q.map(|s| !s.is_empty()).unwrap_or(false);
|
||||
|
||||
if !has_tag && !has_person && !has_q {
|
||||
// 无过滤条件 → 走缓存路径,这里回退到全量查询
|
||||
return self.get_all_albums().await;
|
||||
}
|
||||
|
||||
// 动态构建 SQL
|
||||
let sql = {
|
||||
let mut s = String::from(
|
||||
"SELECT DISTINCT a.id, a.title, a.document, a.description, \
|
||||
a.cover_image_id, a.image_count, a.created_at \
|
||||
FROM albums a"
|
||||
);
|
||||
let mut joins = Vec::new();
|
||||
let mut wheres = Vec::new();
|
||||
|
||||
if has_tag {
|
||||
joins.push("JOIN album_tags t ON a.id = t.album_id");
|
||||
wheres.push("t.tag = ?");
|
||||
}
|
||||
if has_person {
|
||||
joins.push("JOIN person_albums pa ON a.id = pa.album_id");
|
||||
joins.push("JOIN persons p ON pa.person_id = p.id");
|
||||
wheres.push("(p.name = ? OR p.name LIKE ?)");
|
||||
}
|
||||
if has_q {
|
||||
wheres.push("a.title LIKE ?");
|
||||
}
|
||||
|
||||
for j in &joins {
|
||||
s.push(' ');
|
||||
s.push_str(j);
|
||||
}
|
||||
if !wheres.is_empty() {
|
||||
s.push_str(" WHERE ");
|
||||
s.push_str(&wheres.join(" AND "));
|
||||
}
|
||||
s.push_str(" ORDER BY a.created_at DESC");
|
||||
|
||||
if limit > 0 {
|
||||
s.push_str(" LIMIT ? OFFSET ?");
|
||||
}
|
||||
|
||||
s
|
||||
};
|
||||
|
||||
let mut q = sqlx::query(&sql);
|
||||
|
||||
if has_tag {
|
||||
q = q.bind(tag.unwrap());
|
||||
}
|
||||
if has_person {
|
||||
let p = person.unwrap();
|
||||
q = q.bind(p).bind(format!("%{}%", p));
|
||||
}
|
||||
if has_q {
|
||||
q = q.bind(format!("%{}%", title_q.unwrap()));
|
||||
}
|
||||
if limit > 0 {
|
||||
q = q.bind(limit).bind(offset);
|
||||
}
|
||||
|
||||
let rows = q.fetch_all(&self.pool).await?;
|
||||
Ok(rows.iter().map(|r| {
|
||||
AlbumInfo {
|
||||
id: Uuid::parse_str(&r.get::<String, _>("id")).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())
|
||||
|
||||
Reference in New Issue
Block a user