根据前端上传数据的修改,修正了相关代码
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
|
||||
|
||||
@@ -133,15 +133,14 @@ pub struct WeatherListResponse {
|
||||
}
|
||||
|
||||
// 天气数据结构体(修改后)
|
||||
#[derive(Debug, Deserialize, Serialize, FromRow)] // 添加 FromRow 派生
|
||||
#[derive(Debug, Deserialize, Serialize, FromRow)]
|
||||
pub struct WeatherData {
|
||||
// 添加 id 字段(数据库中应该有这个主键)
|
||||
#[serde(skip_deserializing)] // 1. 反序列化时忽略该字段;
|
||||
#[sqlx(rename = "id")] // 数据库表的 id 列(自增主键),查询映射时使用
|
||||
#[serde(skip_deserializing)]
|
||||
#[sqlx(rename = "id")]
|
||||
pub id: i32,
|
||||
|
||||
#[serde(rename = "areaType")]
|
||||
#[sqlx(rename = "areatype")] // 数据库列名是小写下划线式
|
||||
#[sqlx(rename = "areatype")]
|
||||
pub area_type: String,
|
||||
|
||||
#[serde(rename = "assignmentnumber")]
|
||||
@@ -162,15 +161,18 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "calculatedwindspeed")]
|
||||
#[sqlx(rename = "calculatedwindspeed")]
|
||||
pub calculated_wind_speed: Option<f64>,
|
||||
pub calculated_wind_speed: Option<f64>, // 这个已经是 Option,很好
|
||||
|
||||
#[serde(rename = "cloudIndex")]
|
||||
#[sqlx(rename = "cloudindex")]
|
||||
pub cloud_index: String,
|
||||
|
||||
// --- 以下是修改的重点 ---
|
||||
// 这些字段在前端是条件性发送的,因此在 Rust 中也必须是可选的
|
||||
|
||||
#[serde(rename = "convertedWindSpeed")]
|
||||
#[sqlx(rename = "convertedwindspeed")]
|
||||
pub converted_wind_speed: i32,
|
||||
pub converted_wind_speed: Option<i32>, // <-- 修改: i32 -> Option<i32>
|
||||
|
||||
pub date: NaiveDate,
|
||||
|
||||
@@ -187,7 +189,7 @@ pub struct WeatherData {
|
||||
pub has_spot_check_wind_speed: bool,
|
||||
|
||||
#[serde(rename = "hours")]
|
||||
#[sqlx(rename = "hour")] // 数据库中是 hour 字段
|
||||
#[sqlx(rename = "hour")]
|
||||
pub hours: i32,
|
||||
|
||||
#[serde(rename = "inspectiontype")]
|
||||
@@ -203,11 +205,11 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "measuredWindSpeed")]
|
||||
#[sqlx(rename = "measuredwindspeed")]
|
||||
pub measured_wind_speed: f64,
|
||||
pub measured_wind_speed: Option<f64>, // <-- 修改: f64 -> Option<f64>
|
||||
|
||||
#[serde(rename = "measurementHeight")]
|
||||
#[sqlx(rename = "measurementheight")]
|
||||
pub measurement_height: f64,
|
||||
pub measurement_height: Option<f64>, // <-- 修改: f64 -> Option<f64>
|
||||
|
||||
#[serde(rename = "min")]
|
||||
#[sqlx(rename = "min")]
|
||||
@@ -225,7 +227,7 @@ pub struct WeatherData {
|
||||
|
||||
#[serde(rename = "pointWindSpeed")]
|
||||
#[sqlx(rename = "pointwindspeed")]
|
||||
pub point_wind_speed: f64,
|
||||
pub point_wind_speed: Option<f64>, // <-- 修改: f64 -> Option<f64>
|
||||
|
||||
#[serde(rename = "solarDeclination")]
|
||||
#[sqlx(rename = "solardeclination")]
|
||||
|
||||
Reference in New Issue
Block a user