Changed nutritional to nutrition.

This commit is contained in:
matthias committed 2026-09-28 01:55:09 +02:00
1 parent 7986632dab
commit 3ae697d852
15 files changed
+93 -10234

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
use log::{error, info};
use serde::{Deserialize, Serialize};
use sqlx::{Arguments, Pool, Row, Postgres, Error, AssertSqlSafe, FromRow, PgPool};
use sqlx::postgres::{PgArguments, PgQueryResult, PgRow};
use crate::models::article::Article;
use crate::models::variant::load_article_variants;
#[derive(Debug, Deserialize, Serialize, FromRow, Clone)]
pub struct Nutrition {
pub id: i64,
pub calorific_value_kj: f64,
pub calorific_value_kcal: f64,
pub fat: f64,
pub saturated_fat : f64,
pub carbohydrates: f64,
pub sugar: f64,
pub fibers: f64,
pub protein: f64,
pub salt: f64,
}
impl Default for Nutrition {
fn default() -> Self {
Self {
id: 0,
calorific_value_kj: 0.0,
calorific_value_kcal: 0.0,
fat: 0.0,
saturated_fat: 0.0,
carbohydrates: 0.0,
sugar: 0.0,
fibers: 0.0,
protein: 0.0,
salt: 0.0
}
}
}
async fn from_postgres_row(row: PgRow, pool: &PgPool) -> Nutrition {
Nutrition {
id: row.get(0),
calorific_value_kj: row.get(1),
calorific_value_kcal: row.get(2),
fat: row.get(3),
saturated_fat: row.get(4),
carbohydrates: row.get(5),
sugar: row.get(6),
fibers: row.get(7),
protein: row.get(8),
salt: row.get(9),
}
}
pub async fn load_nutrition(nutrition_id: i64, pool: &PgPool) -> Option<Nutrition> {
let statement = format!(
"SELECT * FROM {} WHERE id = $1",
"nutrition"
);
let mut args = PgArguments::default();
args.add(nutrition_id);
let res = sqlx::query_with(
AssertSqlSafe(statement.as_str()), args)
.fetch_one(pool).await;
match res {
Ok(record) => {
let article = from_postgres_row(record, pool).await;
Some(article)
}
Err(err) => {
error!("Error while loading nutrition value: ID: {}", nutrition_id);
None
}
}
}
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);
args.add(&nutrition.fat);
args.add(&nutrition.saturated_fat);
args.add(&nutrition.carbohydrates);
args.add(&nutrition.sugar);
args.add(&nutrition.fibers);
args.add(&nutrition.protein);
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 *",
"nutrition",
);
let res = sqlx::query_with(
AssertSqlSafe(statement.as_str()),
args)
.fetch_one(&pool).await;
info!("{:#?}", res);
match res {
Ok(record) => {
let nutrition = from_postgres_row(record, &pool).await;
Some(nutrition)
}
Err(err) => {
error!("Error while loading newly created nutrition values. ID: {}", nutrition.id);
None
}
}
}
pub async fn update_nutrition(nutrition: &Nutrition, pool: PgPool) -> Option<Nutrition> {
let mut args = PgArguments::default();
args.add(&nutrition.id);
args.add(&nutrition.calorific_value_kj);
args.add(&nutrition.calorific_value_kcal);
args.add(&nutrition.fat);
args.add(&nutrition.saturated_fat);
args.add(&nutrition.carbohydrates);
args.add(&nutrition.sugar);
args.add(&nutrition.fibers);
args.add(&nutrition.protein);
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 *",
"nutrition",
);
let res = sqlx::query_with(
AssertSqlSafe(statement.as_str()),
args)
.fetch_one(&pool).await;
info!("{:#?}", res);
match res {
Ok(record) => {
let updated = from_postgres_row(record, &pool).await;
Some(updated)
}
Err(err) => {
error!("Error while updating nutrition values. ID {}", nutrition.id);
None
}
}
}
pub async fn delete_nutrition(nutrition_id: i64, pool: PgPool) -> Option<()> {
let result = sqlx::query(
AssertSqlSafe(format!("DELETE FROM {} WHERE id = {}",
"nutrition",
nutrition_id
).as_str())
).execute(&pool).await;
match result {
Ok(_) => {
Some(())
}
Err(err) => {
println!("Error deleting nutrition. ID: {:#?}", err);
None
}
}
}