This commit is contained in:
lychang
2026-03-22 04:02:47 +08:00
parent e4bf7cf1a4
commit f3ae54a26b
22 changed files with 10607 additions and 347 deletions

View File

@@ -18,6 +18,133 @@ pub async fn get_albums(db: web::Data<Arc<Database>>) -> impl Responder {
}
}
// 收藏功能API路由
// 获取用户的收藏列表
pub async fn get_favorites(
db: web::Data<Arc<Database>>,
) -> impl Responder {
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
let user_id = "default_user".to_string();
match db.get_user_favorites(&user_id).await {
Ok(favorites) => HttpResponse::Ok().json(favorites),
Err(e) => {
eprintln!("获取收藏列表失败: {}", e);
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "获取收藏列表失败"
}))
}
}
}
// 检查图片是否被收藏
pub async fn check_favorite_status(
db: web::Data<Arc<Database>>,
path: web::Path<String>,
) -> impl Responder {
let image_id = match Uuid::parse_str(&path.into_inner()) {
Ok(id) => id,
Err(_) => {
return HttpResponse::BadRequest().json(serde_json::json!({
"error": "无效的图片ID"
}));
}
};
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
let user_id = "default_user".to_string();
match db.get_favorite_status(&user_id, image_id).await {
Ok(status) => HttpResponse::Ok().json(status),
Err(e) => {
eprintln!("检查收藏状态失败: {}", e);
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "检查收藏状态失败"
}))
}
}
}
// 添加收藏
pub async fn add_favorite(
db: web::Data<Arc<Database>>,
favorite_data: web::Json<CreateFavorite>,
) -> impl Responder {
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
let user_id = "default_user".to_string();
match db.add_favorite(&user_id, favorite_data.into_inner()).await {
Ok(favorite_id) => HttpResponse::Created().json(serde_json::json!({
"id": favorite_id,
"message": "收藏成功"
})),
Err(e) => {
eprintln!("添加收藏失败: {}", e);
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "添加收藏失败"
}))
}
}
}
// 取消收藏
pub async fn remove_favorite(
db: web::Data<Arc<Database>>,
path: web::Path<String>,
) -> impl Responder {
let favorite_id = match Uuid::parse_str(&path.into_inner()) {
Ok(id) => id,
Err(_) => {
return HttpResponse::BadRequest().json(serde_json::json!({
"error": "无效的收藏ID"
}));
}
};
match db.remove_favorite(favorite_id).await {
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
"message": "取消收藏成功"
})),
Err(e) => {
eprintln!("取消收藏失败: {}", e);
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "取消收藏失败"
}))
}
}
}
// 通过图片ID取消收藏
pub async fn remove_favorite_by_image(
db: web::Data<Arc<Database>>,
path: web::Path<String>,
) -> impl Responder {
let image_id = match Uuid::parse_str(&path.into_inner()) {
Ok(id) => id,
Err(_) => {
return HttpResponse::BadRequest().json(serde_json::json!({
"error": "无效的图片ID"
}));
}
};
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
let user_id = "default_user".to_string();
match db.remove_favorite_by_image(&user_id, image_id).await {
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
"message": "取消收藏成功"
})),
Err(e) => {
eprintln!("取消收藏失败: {}", e);
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "取消收藏失败"
}))
}
}
}
pub async fn get_original_image(
db: web::Data<Arc<Database>>,
image_processor: web::Data<Arc<ImageProcessor>>,
@@ -263,4 +390,4 @@ pub async fn get_thumbnail(
}))
}
}
}
}