add to result and pub plan
This commit is contained in:
@@ -6,19 +6,19 @@ use crate::config::{ORACLE_PASS, ORACLE_USER, ORACLE_CON_STR};
|
|||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||||
pub struct PlanData{
|
pub struct PlanData{
|
||||||
net_id: String,
|
pub net_id: String,
|
||||||
week_date: String,
|
pub week_date: String,
|
||||||
total_cal: Option<f32>,
|
pub total_cal: Option<f32>,
|
||||||
total_fat: Option<f32>,
|
pub total_fat: Option<f32>,
|
||||||
total_sat_fat: Option<f32>,
|
pub total_sat_fat: Option<f32>,
|
||||||
total_trans_fat: Option<f32>,
|
pub total_trans_fat: Option<f32>,
|
||||||
total_carbs: Option<f32>,
|
pub total_carbs: Option<f32>,
|
||||||
total_fiber: Option<f32>,
|
pub total_fiber: Option<f32>,
|
||||||
total_sugar: Option<f32>,
|
pub total_sugar: Option<f32>,
|
||||||
total_protein: Option<f32>,
|
pub total_protein: Option<f32>,
|
||||||
total_sodium: Option<f32>,
|
pub total_sodium: Option<f32>,
|
||||||
total_potassium: Option<f32>,
|
pub total_potassium: Option<f32>,
|
||||||
total_cholesterol: Option<f32>
|
pub total_cholesterol: Option<f32>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn plan(body: Json<PlanData>) -> impl Responder {
|
pub async fn plan(body: Json<PlanData>) -> impl Responder {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use log::error;
|
use log::error;
|
||||||
use actix_web::{web::Json, web::Path};
|
use actix_web::{web::Json, web::Path, Responder, HttpResponse};
|
||||||
use oracle::{Connection, Result};
|
use oracle::{Connection, Result};
|
||||||
use crate::{api::plan::PlanData, config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR}};
|
use crate::{api::plan::PlanData, config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR}};
|
||||||
|
|
||||||
@@ -17,6 +17,18 @@ pub async fn all_result(net_id: Path<String>) -> Json<Vec<PlanData>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn add_result(result: Json<PlanData>) -> impl Responder {
|
||||||
|
let result = result.into_inner();
|
||||||
|
|
||||||
|
match push_result(&result){
|
||||||
|
Ok(_) => HttpResponse::Ok(),
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to add result for user {}: {}", result.net_id, e);
|
||||||
|
HttpResponse::InternalServerError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_result(net_id: &str) -> Result<Vec<PlanData>> {
|
fn get_result(net_id: &str) -> Result<Vec<PlanData>> {
|
||||||
|
|
||||||
@@ -46,3 +58,27 @@ fn get_result(net_id: &str) -> Result<Vec<PlanData>> {
|
|||||||
|
|
||||||
Ok(row_vec)
|
Ok(row_vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn push_result(plan: &PlanData) -> Result<()> {
|
||||||
|
let conn = Connection::connect(ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR)?;
|
||||||
|
let mut stmt = conn.statement("insert into result values(:net_id, :week_date, :total_cal, :total_fat, :total_sat_fat, :total_trans_fat, :total_carbs, :total_fiber, :total_sugar, :total_protein, :total_sodium, :total_potassium, :total_cholesterol)").build()?;
|
||||||
|
|
||||||
|
stmt.execute_named(&[
|
||||||
|
("net_id", &plan.net_id),
|
||||||
|
("week_date", &plan.week_date),
|
||||||
|
("total_cal", &plan.total_cal),
|
||||||
|
("total_fat", &plan.total_fat),
|
||||||
|
("total_sat_fat", &plan.total_sat_fat),
|
||||||
|
("total_trans_fat", &plan.total_trans_fat),
|
||||||
|
("total_carbs", &plan.total_carbs),
|
||||||
|
("total_fiber", &plan.total_fiber),
|
||||||
|
("total_sugar", &plan.total_sugar),
|
||||||
|
("total_protein", &plan.total_protein),
|
||||||
|
("total_sodium", &plan.total_sodium),
|
||||||
|
("total_potassium", &plan.total_potassium),
|
||||||
|
("total_cholesterol", &plan.total_cholesterol)])?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -92,6 +92,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
web::resource("result/{net_id}")
|
web::resource("result/{net_id}")
|
||||||
.route(web::get().to(api::result::all_result))
|
.route(web::get().to(api::result::all_result))
|
||||||
)
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("add_result/")
|
||||||
|
.route(web::post().to(api::result::add_result))
|
||||||
|
)
|
||||||
.route("/", web::get().to(api_index))
|
.route("/", web::get().to(api_index))
|
||||||
)
|
)
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
|
|||||||
Reference in New Issue
Block a user