Added unit to db and structs/types.

This commit is contained in:
matthias committed 2026-01-26 22:09:32 +01:00
1 parent 7879f69255
commit 8a27639aad
14 files changed
+243 -60

No files matched your search

+9 -3
View File
@@ -5,6 +5,7 @@ use crate::models::variant::{load_article_variants, Variant};
use sqlx::{Arguments, Pool, Row, Postgres, Error};
use sqlx::postgres::{PgArguments, PgQueryResult, PgRow};
use log::{debug, error, info};
use crate::models::unit::Unit;
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
pub struct Article {
@@ -14,6 +15,7 @@ pub struct Article {
pub origin: Origin,
pub description: Option<String>,
pub ingredients: Option<String>,
pub default_unit: Unit,
pub variants: Vec<Variant>,
}
@@ -25,6 +27,7 @@ fn from_postgres_row_small(row: PgRow) -> Article {
origin: row.get(3),
description: row.get(4),
ingredients: row.get(5),
default_unit: row.get(6),
variants: vec![],
}
}
@@ -50,6 +53,7 @@ async fn from_postgres_row(row: PgRow, pool: PgPool) -> Article {
origin: row.get(3),
description: row.get(4),
ingredients: row.get(5),
default_unit: row.get(6),
variants: variants,
}
}
@@ -116,9 +120,10 @@ pub async fn insert_article(article: &Article, pool: PgPool) -> Option<Article>
args.add(&article.origin);
args.add(&article.description);
args.add(&article.ingredients);
args.add(&article.default_unit);
let statement = format!(
"INSERT INTO {} (name, bio, origin, description, ingredients) VALUES ($1, $2, $3, $4, $5) RETURNING *",
"INSERT INTO {} (name, bio, origin, description, ingredients, default_unit_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
"article",
);
@@ -150,9 +155,10 @@ pub async fn update_article(article_id: i64, article: &Article, pool: PgPool) ->
args.add(&article.origin);
args.add(&article.description);
args.add(&article.ingredients);
args.add(&article.default_unit);
let statement = format!(
"UPDATE {} SET name = $2, bio = $3, origin = $4, description = $5, ingredients = $6 WHERE id = $1 RETURNING *",
"UPDATE {} SET name = $2, bio = $3, origin = $4, description = $5, ingredients = $6, default_unit_id = $7 WHERE id = $1 RETURNING *",
"article",
);