在weather_data表中添加了一个字段
This commit is contained in:
17
src/db.rs
17
src/db.rs
@@ -5,7 +5,7 @@ use std::error::Error;
|
||||
// 从 models 模块引入 WeatherData 结构体
|
||||
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
|
||||
let user_id = match sqlx::query_as::<_, (i32,)>("SELECT id FROM users WHERE openid = $1")
|
||||
@@ -32,16 +32,16 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
atmosphericstability, suitabilitydegree, winddirection, averagewinddirection,
|
||||
winddirectionstandarddeviation, windspeed, averagewindspeed, windspeedsuitability,
|
||||
winddirectionsuitability, overallsuitability, inspectiontype, assignmentnumber,
|
||||
calculatedwindspeed
|
||||
calculatedwindspeed, hasspotcheckwindspeed
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19,
|
||||
$20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34
|
||||
$20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35
|
||||
) RETURNING id
|
||||
"#;
|
||||
|
||||
// 转换经纬度为字符串
|
||||
let longitude_str = weather_data.longitude.to_string();
|
||||
let latitude_str = weather_data.latitude.to_string();
|
||||
//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)
|
||||
@@ -50,8 +50,8 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
.bind(weather_data.date) // NaiveDate 类型,sqlx 会自动处理
|
||||
.bind(weather_data.hours)
|
||||
.bind(weather_data.min)
|
||||
.bind(&longitude_str)
|
||||
.bind(&latitude_str)
|
||||
.bind(&weather_data.longitude)
|
||||
.bind(&weather_data.latitude)
|
||||
.bind(weather_data.day_since_jan_first)
|
||||
.bind(weather_data.theta)
|
||||
.bind(weather_data.solar_declination)
|
||||
@@ -85,6 +85,7 @@ pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData) -> R
|
||||
.bind(&weather_data.inspection_type)
|
||||
.bind(&weather_data.assignment_number)
|
||||
.bind(weather_data.calculated_wind_speed)
|
||||
.bind(weather_data.has_spot_check_wind_speed)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
@@ -135,7 +136,7 @@ pub async fn get_weather_details(pool: &PgPool, weather_id: i32) -> Result<Weath
|
||||
wd.suitabilitydegree, wd.winddirection, wd.averagewinddirection,
|
||||
wd.winddirectionstandarddeviation, wd.windspeed, wd.averagewindspeed,
|
||||
wd.windspeedsuitability, wd.winddirectionsuitability, wd.overallsuitability,
|
||||
wd.inspectiontype, wd.assignmentnumber, wd.calculatedwindspeed,
|
||||
wd.inspectiontype, wd.assignmentnumber, wd.calculatedwindspeed, wd.hasspotcheckwindspeed,
|
||||
u.openid
|
||||
FROM weather_data wd
|
||||
JOIN users u ON wd.user_id = u.id
|
||||
|
||||
Reference in New Issue
Block a user