feat: 统一前后端日志和错误处理
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use actix_web::{web, delete, get, post, HttpResponse, Responder};
|
||||
use actix_web::{web, delete, get, post, HttpResponse};
|
||||
use sqlx::postgres::PgPool;
|
||||
use tracing::{debug, error, info};
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::db;
|
||||
use crate::error::AppError;
|
||||
use crate::models::Claims;
|
||||
|
||||
// GET /api/favorites - 获取收藏列表
|
||||
@@ -11,31 +12,21 @@ pub async fn get_favorites(
|
||||
query: web::Query<serde_json::Value>,
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let page = query.get("page").and_then(|v| v.as_i64()).unwrap_or(1).max(1) as i32;
|
||||
let limit = query.get("limit").and_then(|v| v.as_i64()).unwrap_or(10).clamp(1, 100) as i32;
|
||||
|
||||
debug!("获取收藏列表, 页码: {}, 每页条数: {}", page, limit);
|
||||
|
||||
match db::get_favorites_list(pool.get_ref(), claims.user_id, page, limit).await {
|
||||
Ok(response) => {
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": response.list,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": response.total
|
||||
}))
|
||||
}
|
||||
Err(error_msg) => {
|
||||
error!("获取收藏列表失败: {}", error_msg);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 500,
|
||||
"errmsg": error_msg
|
||||
}))
|
||||
}
|
||||
}
|
||||
let response = db::get_favorites_list(pool.get_ref(), claims.user_id, page, limit).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": response.list,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": response.total
|
||||
})))
|
||||
}
|
||||
|
||||
// POST /api/favorites/{id} - 添加收藏
|
||||
@@ -44,25 +35,16 @@ pub async fn add_favorite(
|
||||
path: web::Path<i32>,
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let weather_id = path.into_inner();
|
||||
info!("添加收藏, weather_id: {}", weather_id);
|
||||
|
||||
match db::set_weather_favorite(pool.get_ref(), weather_id, claims.user_id, true).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": format!("已添加收藏")
|
||||
})),
|
||||
Err(error_msg) => {
|
||||
error!("添加收藏失败: {}", error_msg);
|
||||
let errcode = if error_msg.starts_with("未找到") { 404 } else { 500 };
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": errcode,
|
||||
"errmsg": error_msg
|
||||
}))
|
||||
}
|
||||
}
|
||||
db::set_weather_favorite(pool.get_ref(), weather_id, claims.user_id, true).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "已添加收藏"
|
||||
})))
|
||||
}
|
||||
|
||||
// DELETE /api/favorites/{id} - 取消收藏
|
||||
@@ -71,23 +53,14 @@ pub async fn remove_favorite(
|
||||
path: web::Path<i32>,
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let weather_id = path.into_inner();
|
||||
info!("取消收藏, weather_id: {}", weather_id);
|
||||
|
||||
match db::set_weather_favorite(pool.get_ref(), weather_id, claims.user_id, false).await {
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": format!("已取消收藏")
|
||||
})),
|
||||
Err(error_msg) => {
|
||||
error!("取消收藏失败: {}", error_msg);
|
||||
let errcode = if error_msg.starts_with("未找到") { 404 } else { 500 };
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": errcode,
|
||||
"errmsg": error_msg
|
||||
}))
|
||||
}
|
||||
}
|
||||
db::set_weather_favorite(pool.get_ref(), weather_id, claims.user_id, false).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "已取消收藏"
|
||||
})))
|
||||
}
|
||||
Reference in New Issue
Block a user