Added unit to db and structs/types.
This commit is contained in:
@@ -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",
|
||||
);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod origin;
|
||||
pub mod article;
|
||||
pub mod variant;
|
||||
pub mod unit;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
use std::fmt;
|
||||
use std::fmt::Formatter;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[repr(i64)]
|
||||
#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, sqlx::Type)]
|
||||
pub enum Unit {
|
||||
Kg,
|
||||
G,
|
||||
L,
|
||||
Ml,
|
||||
Stk,
|
||||
}
|
||||
|
||||
impl Unit {
|
||||
pub const VEC: &'static [Unit] = &[Unit::Kg, Unit::G, Unit::L, Unit::Ml, Unit::Stk];
|
||||
|
||||
pub fn position(&self) -> usize {
|
||||
Unit::VEC.iter().position(|r| *r == *self).unwrap()
|
||||
}
|
||||
|
||||
// TODO: Convert function for units if possible/needed
|
||||
|
||||
}
|
||||
|
||||
fn to_string(unit: Unit, f: &mut Formatter) -> fmt::Result {
|
||||
match unit {
|
||||
Unit::Kg => write!(f, "Kilogramm"),
|
||||
Unit::G => write!(f, "Gramm"),
|
||||
Unit::L => write!(f, "Liter"),
|
||||
Unit::Ml => write!(f, "Milliliter"),
|
||||
Unit::Stk => write!(f, "Stück"),
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Unit {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
to_string(*self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Unit {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
to_string(*self, f)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Arguments, FromRow, PgPool, Row};
|
||||
use sqlx::postgres::{PgArguments, PgRow};
|
||||
use crate::models::unit::Unit;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
pub struct Variant {
|
||||
@@ -11,6 +12,7 @@ pub struct Variant {
|
||||
pub name: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub ingredients: Option<String>,
|
||||
pub unit: Unit,
|
||||
}
|
||||
|
||||
fn from_postgres_row_small(row: PgRow) -> Variant {
|
||||
@@ -21,6 +23,7 @@ fn from_postgres_row_small(row: PgRow) -> Variant {
|
||||
name: row.get(4),
|
||||
description: row.get(5),
|
||||
ingredients: row.get(6),
|
||||
unit: row.get(7),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,9 +67,10 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
args.add(&variant.name);
|
||||
args.add(&variant.description);
|
||||
args.add(&variant.ingredients);
|
||||
args.add(&variant.unit);
|
||||
|
||||
let statement = format!(
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients, unit_id) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *",
|
||||
"variant",
|
||||
);
|
||||
|
||||
@@ -97,11 +101,12 @@ pub async fn update_variant(variant_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
args.add(&variant.name);
|
||||
args.add(&variant.description);
|
||||
args.add(&variant.ingredients);
|
||||
args.add(&variant.unit);
|
||||
|
||||
info!("Variant: {:#?}", variant);
|
||||
|
||||
let statement = format!(
|
||||
"UPDATE {} SET weight = $2, ean = $3, name = $4, description = $5, ingredients = $6 WHERE id = $1 RETURNING *",
|
||||
"UPDATE {} SET weight = $2, ean = $3, name = $4, description = $5, ingredients = $6, unit_id = $7 WHERE id = $1 RETURNING *",
|
||||
"variant",
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user