修改了get_weather方法,现在可以实现前端的需求了
This commit is contained in:
@@ -155,7 +155,7 @@ pub async fn get_weather_details(pool: &PgPool, weather_id: i32) -> Result<Weath
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换db.rs中的get_weather_list函数
|
// get_weather_list函数
|
||||||
pub async fn get_weather_list(
|
pub async fn get_weather_list(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
user_id: i32,
|
user_id: i32,
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@@ -251,8 +251,38 @@ async fn get_weather(
|
|||||||
pool: web::Data<PgPool>,
|
pool: web::Data<PgPool>,
|
||||||
claims: web::ReqData<Claims>,
|
claims: web::ReqData<Claims>,
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let page = query.get("page").and_then(|v| v.as_i64()).unwrap_or(1) as i32;
|
// 同时处理数字和字符串类型的page参数
|
||||||
let limit = query.get("limit").and_then(|v| v.as_i64()).unwrap_or(10) as i32;
|
let page = match query.get("page") {
|
||||||
|
Some(v) => {
|
||||||
|
// 尝试直接解析为数字
|
||||||
|
if let Some(num) = v.as_i64() {
|
||||||
|
num as i32
|
||||||
|
}
|
||||||
|
// 尝试将字符串解析为数字
|
||||||
|
else if let Some(s) = v.as_str() {
|
||||||
|
s.parse().unwrap_or(1)
|
||||||
|
}
|
||||||
|
// 解析失败用默认值
|
||||||
|
else {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => 1, // 无参数时用默认值
|
||||||
|
};
|
||||||
|
|
||||||
|
// 同理优化limit参数解析
|
||||||
|
let limit = match query.get("limit") {
|
||||||
|
Some(v) => {
|
||||||
|
if let Some(num) = v.as_i64() {
|
||||||
|
num as i32
|
||||||
|
} else if let Some(s) = v.as_str() {
|
||||||
|
s.parse().unwrap_or(10)
|
||||||
|
} else {
|
||||||
|
10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => 10,
|
||||||
|
};
|
||||||
|
|
||||||
println!("获取天气数据列表, 页码: {}, 每页条数: {}", page, limit);
|
println!("获取天气数据列表, 页码: {}, 每页条数: {}", page, limit);
|
||||||
|
|
||||||
|
|||||||
@@ -82,8 +82,8 @@ pub struct WeatherDataBrief {
|
|||||||
#[sqlx(rename = "min")]
|
#[sqlx(rename = "min")]
|
||||||
pub min: i32,
|
pub min: i32,
|
||||||
|
|
||||||
pub longitude: f64,
|
pub longitude: String,
|
||||||
pub latidute: f64,
|
pub latitude: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在models.rs中添加(可放在WeatherDataBrief下方)
|
// 在models.rs中添加(可放在WeatherDataBrief下方)
|
||||||
@@ -151,8 +151,8 @@ pub struct WeatherData {
|
|||||||
#[sqlx(rename = "inspectiontype")]
|
#[sqlx(rename = "inspectiontype")]
|
||||||
pub inspection_type: Option<String>,
|
pub inspection_type: Option<String>,
|
||||||
|
|
||||||
pub latitude: f64,
|
pub latitude: String,
|
||||||
pub longitude: f64,
|
pub longitude: String,
|
||||||
|
|
||||||
#[serde(rename = "lowCloudiness")]
|
#[serde(rename = "lowCloudiness")]
|
||||||
#[sqlx(rename = "lowcloudiness")]
|
#[sqlx(rename = "lowcloudiness")]
|
||||||
|
|||||||
Reference in New Issue
Block a user