Started adding nutrition editing logic.

This commit is contained in:
matthias committed 2026-09-28 03:46:36 +02:00
1 parent 3ae697d852
commit d613f8c1d7
6 files changed
+306 -15

No files matched your search

+62 -1
View File
@@ -5,7 +5,7 @@ use crate::models::variant::{load_article_variants, Variant};
use sqlx::{Arguments, Pool, Row, Postgres, Error, AssertSqlSafe};
use sqlx::postgres::{PgArguments, PgQueryResult, PgRow};
use log::{debug, error, info};
use crate::models::nutrition::{load_nutrition, Nutrition};
use crate::models::nutrition::{load_nutrition, Nutrition, insert_nutrition};
use crate::models::unit::Unit;
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
@@ -27,6 +27,28 @@ pub struct Article {
pub nutrition: Option<Nutrition>,
}
impl Default for Article {
fn default() -> Self {
Self {
id: 0,
name: "".to_string(),
bio: false,
origin: Origin::EU,
description: None,
ingredients: None,
default_unit: Unit::G,
variants: vec![],
mhd: None,
bio_origin: None,
allergens_text: None,
bio_info: None,
topm_id: None,
additional_name: None,
nutrition: None,
}
}
}
fn from_postgres_row_small(row: PgRow) -> Article {
Article {
id: row.get(0),
@@ -244,3 +266,42 @@ pub async fn delete_article(article_id: i64, pool: PgPool) -> Option<()> {
}
}
}
pub async fn article_add_nutrition(article_id: i64, pool: PgPool) -> Option<Article> {
let nutrition = insert_nutrition(&Nutrition::default(), &pool).await;
match nutrition {
Some(nutrition) => {
let mut args = PgArguments::default();
args.add(&article_id);
args.add(&nutrition.id);
let statement = format!(
"UPDATE {} SET nutrition_id = $2 WHERE id = $1 RETURNING *",
"article",
);
let res = sqlx::query_with(
AssertSqlSafe(statement.as_str()),
args)
.fetch_one(&pool).await;
info!("{:#?}", res);
match res {
Ok(record) => {
let article = from_postgres_row_small(record);
Some(article)
}
Err(err) => {
error!("Error while adding nutrition to article: {}", article_id);
None
}
}
},
None => {
error!("Error while creating nutrition");
None
}
}
}
+9 -6
View File
@@ -77,7 +77,7 @@ pub async fn load_nutrition(nutrition_id: i64, pool: &PgPool) -> Option<Nutritio
}
}
pub async fn insert_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nutrition> {
pub async fn insert_nutrition(nutrition: &Nutrition, pool: &PgPool) -> Option<Nutrition> {
let mut args = PgArguments::default();
args.add(&nutrition.calorific_value_kj);
args.add(&nutrition.calorific_value_kcal);
@@ -90,14 +90,16 @@ pub async fn insert_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nut
args.add(&nutrition.salt);
let statement = format!(
"INSERT INTO {} (calorific_value_kj, calorific_value_kcal, fat, carbohydrates, sugar, default_unit_id, fibers, protein, salt) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *",
"INSERT INTO {} (calorific_value_kj, calorific_value_kcal, fat, saturated_fat,\
carbohydrates, sugar, fibers, protein, salt)\
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *",
"nutrition",
);
let res = sqlx::query_with(
AssertSqlSafe(statement.as_str()),
args)
.fetch_one(&pool).await;
.fetch_one(pool).await;
info!("{:#?}", res);
@@ -114,9 +116,9 @@ pub async fn insert_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nut
}
}
pub async fn update_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nutrition> {
pub async fn update_nutrition(nutrition_id: i64, nutrition: &Nutrition, pool: PgPool) -> Option<Nutrition> {
let mut args = PgArguments::default();
args.add(&nutrition.id);
args.add(nutrition_id);
args.add(&nutrition.calorific_value_kj);
args.add(&nutrition.calorific_value_kcal);
args.add(&nutrition.fat);
@@ -128,7 +130,8 @@ pub async fn update_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nut
args.add(&nutrition.salt);
let statement = format!(
"UPDATE {} SET calorific_value_kj = $3, calorific_value_kcal = $4, fat = $5,\
saturated_fat = $6, carbohydrates = $7, sugar = $8, fibers = $9, protein = $10, salt = $11 WHERE id = $1 RETURNING *",
saturated_fat = $6, carbohydrates = $7, sugar = $8, fibers = $9, protein = $10,\
salt = $11 WHERE id = $1 RETURNING *",
"nutrition",
);