根据前端上传数据的修改,修正了相关代码
This commit is contained in:
20
src/db.rs
20
src/db.rs
@@ -7,7 +7,7 @@ use crate::models::{WeatherData, WeatherDataBrief, WeatherListResponse};
|
||||
|
||||
// 用于插入weather_data的数据
|
||||
pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> Result<i32, String> {
|
||||
// 1. 根据 openid 查询 user_id
|
||||
// 1. 根据 openid 查询 user_id (这部分逻辑不变)
|
||||
let user_id = match sqlx::query_as::<_, (i32,)>("SELECT id FROM users WHERE openid = $1")
|
||||
.bind(&weather_data.openid)
|
||||
.fetch_optional(pool)
|
||||
@@ -22,7 +22,7 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
}
|
||||
};
|
||||
|
||||
// 2. 准备插入数据的 SQL 语句
|
||||
// 2. 准备插入数据的 SQL 语句 (SQL本身不变)
|
||||
let insert_query = r#"
|
||||
INSERT INTO weather_data (
|
||||
user_id, title, date, hour, min, longitude, latitude, daysincejanfirst,
|
||||
@@ -39,15 +39,11 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
) RETURNING id
|
||||
"#;
|
||||
|
||||
// 转换经纬度为字符串
|
||||
//let longitude_str = weather_data.longitude.to_string();
|
||||
//let latitude_str = weather_data.latitude.to_string();
|
||||
|
||||
// 3. 执行插入操作
|
||||
let inserted_id = match sqlx::query_as::<_, (i32,)>(insert_query)
|
||||
.bind(user_id)
|
||||
.bind(&weather_data.title)
|
||||
.bind(weather_data.date) // NaiveDate 类型,sqlx 会自动处理
|
||||
.bind(weather_data.date)
|
||||
.bind(weather_data.hours)
|
||||
.bind(weather_data.min)
|
||||
.bind(&weather_data.longitude)
|
||||
@@ -61,11 +57,11 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
.bind(&weather_data.cloud_index)
|
||||
.bind(weather_data.solar_radiation_level as i32)
|
||||
.bind(weather_data.has_measured_wind_speed)
|
||||
.bind(weather_data.measured_wind_speed)
|
||||
.bind(weather_data.converted_wind_speed as i32)
|
||||
.bind(weather_data.measurement_height)
|
||||
.bind(weather_data.measured_wind_speed) // <-- 修改: 直接绑定 Option<f64>
|
||||
.bind(weather_data.converted_wind_speed) // <-- 修改: 直接绑定 Option<i32>,并移除 `as i32`
|
||||
.bind(weather_data.measurement_height) // <-- 修改: 直接绑定 Option<f64>
|
||||
.bind(&weather_data.area_type)
|
||||
.bind(weather_data.point_wind_speed)
|
||||
.bind(weather_data.point_wind_speed) // <-- 修改: 直接绑定 Option<f64>
|
||||
.bind(&weather_data.atmospheric_stability)
|
||||
.bind(&weather_data.suitability_degree)
|
||||
.bind(
|
||||
@@ -84,7 +80,7 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
.bind(&weather_data.overall_suitability)
|
||||
.bind(&weather_data.inspection_type)
|
||||
.bind(&weather_data.assignment_number)
|
||||
.bind(weather_data.calculated_wind_speed)
|
||||
.bind(weather_data.calculated_wind_speed) // 这个本来就是 Option,无需修改
|
||||
.bind(weather_data.has_spot_check_wind_speed)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user