Added unit to db and structs/types.

This commit is contained in:
2026-01-26 22:09:32 +01:00
parent 7879f69255
commit 8a27639aad
14 changed files with 243 additions and 60 deletions
+8 -2
View File
@@ -5,6 +5,7 @@ use crate::models::variant::{load_article_variants, Variant};
use sqlx::{Arguments, Pool, Row, Postgres, Error}; use sqlx::{Arguments, Pool, Row, Postgres, Error};
use sqlx::postgres::{PgArguments, PgQueryResult, PgRow}; use sqlx::postgres::{PgArguments, PgQueryResult, PgRow};
use log::{debug, error, info}; use log::{debug, error, info};
use crate::models::unit::Unit;
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)] #[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
pub struct Article { pub struct Article {
@@ -14,6 +15,7 @@ pub struct Article {
pub origin: Origin, pub origin: Origin,
pub description: Option<String>, pub description: Option<String>,
pub ingredients: Option<String>, pub ingredients: Option<String>,
pub default_unit: Unit,
pub variants: Vec<Variant>, pub variants: Vec<Variant>,
} }
@@ -25,6 +27,7 @@ fn from_postgres_row_small(row: PgRow) -> Article {
origin: row.get(3), origin: row.get(3),
description: row.get(4), description: row.get(4),
ingredients: row.get(5), ingredients: row.get(5),
default_unit: row.get(6),
variants: vec![], variants: vec![],
} }
} }
@@ -50,6 +53,7 @@ async fn from_postgres_row(row: PgRow, pool: PgPool) -> Article {
origin: row.get(3), origin: row.get(3),
description: row.get(4), description: row.get(4),
ingredients: row.get(5), ingredients: row.get(5),
default_unit: row.get(6),
variants: variants, variants: variants,
} }
} }
@@ -116,9 +120,10 @@ pub async fn insert_article(article: &Article, pool: PgPool) -> Option<Article>
args.add(&article.origin); args.add(&article.origin);
args.add(&article.description); args.add(&article.description);
args.add(&article.ingredients); args.add(&article.ingredients);
args.add(&article.default_unit);
let statement = format!( 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", "article",
); );
@@ -150,9 +155,10 @@ pub async fn update_article(article_id: i64, article: &Article, pool: PgPool) ->
args.add(&article.origin); args.add(&article.origin);
args.add(&article.description); args.add(&article.description);
args.add(&article.ingredients); args.add(&article.ingredients);
args.add(&article.default_unit);
let statement = format!( 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", "article",
); );
+1
View File
@@ -1,3 +1,4 @@
pub mod origin; pub mod origin;
pub mod article; pub mod article;
pub mod variant; pub mod variant;
pub mod unit;
+47
View File
@@ -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)
}
}
+7 -2
View File
@@ -2,6 +2,7 @@ use log::{error, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{Arguments, FromRow, PgPool, Row}; use sqlx::{Arguments, FromRow, PgPool, Row};
use sqlx::postgres::{PgArguments, PgRow}; use sqlx::postgres::{PgArguments, PgRow};
use crate::models::unit::Unit;
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)] #[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
pub struct Variant { pub struct Variant {
@@ -11,6 +12,7 @@ pub struct Variant {
pub name: Option<String>, pub name: Option<String>,
pub description: Option<String>, pub description: Option<String>,
pub ingredients: Option<String>, pub ingredients: Option<String>,
pub unit: Unit,
} }
fn from_postgres_row_small(row: PgRow) -> Variant { fn from_postgres_row_small(row: PgRow) -> Variant {
@@ -21,6 +23,7 @@ fn from_postgres_row_small(row: PgRow) -> Variant {
name: row.get(4), name: row.get(4),
description: row.get(5), description: row.get(5),
ingredients: row.get(6), 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.name);
args.add(&variant.description); args.add(&variant.description);
args.add(&variant.ingredients); args.add(&variant.ingredients);
args.add(&variant.unit);
let statement = format!( 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", "variant",
); );
@@ -97,11 +101,12 @@ pub async fn update_variant(variant_id: i64, variant: &Variant, pool: PgPool) ->
args.add(&variant.name); args.add(&variant.name);
args.add(&variant.description); args.add(&variant.description);
args.add(&variant.ingredients); args.add(&variant.ingredients);
args.add(&variant.unit);
info!("Variant: {:#?}", variant); info!("Variant: {:#?}", variant);
let statement = format!( 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", "variant",
); );
+10 -5
View File
@@ -115,7 +115,7 @@
<v-card-text> <v-card-text>
<v-row no-gutters> <v-row no-gutters>
<v-col cols="2"> <v-col cols="2" style="padding: 5px;">
<v-row no-gutters> <v-row no-gutters>
<v-checkbox <v-checkbox
v-model="store.articleEdit.bio" v-model="store.articleEdit.bio"
@@ -126,16 +126,21 @@
<v-row no-gutters> <v-row no-gutters>
<v-col cols="5"> <v-col cols="5">
<v-sheet>
Herkunft: Herkunft:
</v-sheet>
</v-col> </v-col>
<v-col> <v-col>
<v-sheet>
{{ originToString(store.article.origin) }} {{ originToString(store.article.origin) }}
</v-sheet>
</v-col> </v-col>
</v-row> </v-row>
<v-row>
<v-select
v-model="store.articleEdit.default_unit"
:items="store.units"
label="Standard Einheit"
:readonly="!edit"
></v-select>
</v-row>
</v-col> </v-col>
<v-col cols="5" style="padding: 5px;"> <v-col cols="5" style="padding: 5px;">
@@ -3,6 +3,7 @@
import {useLabelEditorStore} from "~/store/labelEditorStore"; import {useLabelEditorStore} from "~/store/labelEditorStore";
import {Article} from "~/types/article"; import {Article} from "~/types/article";
import {Origin} from "~/types/origin"; import {Origin} from "~/types/origin";
import type {Unit} from "~/types/unit";
let store = useLabelEditorStore(); let store = useLabelEditorStore();
@@ -12,16 +13,18 @@
let name: Ref<string | null> = ref<string | null>(null); let name: Ref<string | null> = ref<string | null>(null);
let bio: Ref<boolean> = ref<boolean>(false); let bio: Ref<boolean> = ref<boolean>(false);
let origin: Ref<Origin | null> = ref<Origin | null>(null); let origin: Ref<Origin | null> = ref<Origin | null>(null);
let default_unit: Ref<Unit | null> = ref<Unit | null>(null);
let description: Ref<string | null> = ref<string | null>(null); let description: Ref<string | null> = ref<string | null>(null);
let ingredients: Ref<string | null> = ref<string | null>(null); let ingredients: Ref<string | null> = ref<string | null>(null);
async function create() { async function create() {
if (name.value != null && name.value.trim().length > 0 && if (name.value != null && name.value.trim().length > 0 &&
origin.value != null && origin.value.trim().length > 0 origin.value != null && origin.value.trim().length > 0 &&
default_unit.value != null && default_unit.value.trim().length > 0
) { ) {
loading.value = true; loading.value = true;
const article: Article = new Article(0, name.value, bio.value, origin.value, description.value, ingredients.value, []); const article: Article = new Article(0, name.value, bio.value, origin.value, description.value, ingredients.value, default_unit.value, []);
await store.createArticle(article); await store.createArticle(article);
@@ -102,6 +105,13 @@
label="Herkunft" label="Herkunft"
></v-select> ></v-select>
</v-col> </v-col>
<v-col>
<v-select
v-model="default_unit"
:items="store.units"
label="Standard Einheit"
></v-select>
</v-col>
</v-row> </v-row>
<v-textarea <v-textarea
@@ -1,39 +1,46 @@
<script setup lang="ts"> <script setup lang="ts">
import type {Ref} from "vue"; import type {Ref} from "vue";
import {useLabelEditorStore} from "~/store/labelEditorStore"; import {useLabelEditorStore} from "~/store/labelEditorStore";
import { Variant } from "~/types/variant"; import { Variant } from "~/types/variant";
import type {Unit} from "~/types/unit";
let store = useLabelEditorStore(); let store = useLabelEditorStore();
let dialog: Ref<boolean> = ref<boolean>(false); let dialog: Ref<boolean> = ref<boolean>(false);
let loading: Ref<boolean> = ref<boolean>(false); let loading: Ref<boolean> = ref<boolean>(false);
let weight: Ref<string | null> = ref<string | null>(null); let weight: Ref<string | null> = ref<string | null>(null);
let ean: Ref<string | null> = ref<string | null>(null); let ean: Ref<string | null> = ref<string | null>(null);
let name: Ref<string | null> = ref<string | null>(null); let name: Ref<string | null> = ref<string | null>(null);
let description: Ref<string | null> = ref<string | null>(null); let unit: Ref<Unit | null> = ref<Unit | null>(null);
let ingredients: Ref<string | null> = ref<string | null>(null); let description: Ref<string | null> = ref<string | null>(null);
let ingredients: Ref<string | null> = ref<string | null>(null);
async function create() { if (store.article != null) {
unit.value = store.article.default_unit;
}
async function create() {
if (store.article != null && if (store.article != null &&
weight.value != null && weight.value.trim().length > 0 weight.value != null && weight.value.trim().length > 0 &&
unit.value != null && unit.value.trim().length > 0
) { ) {
loading.value = true; loading.value = true;
const variant: Variant = new Variant(0, weight.value, ean.value, name.value, description.value, ingredients.value); const variant: Variant = new Variant(0, weight.value, ean.value, name.value, description.value, ingredients.value, unit.value);
await store.addVariant(variant); await store.addVariant(variant);
closeDialog(); closeDialog();
} }
} }
function closeDialog() { function closeDialog() {
dialog.value = false; dialog.value = false;
clearVariables(); clearVariables();
} }
function clearVariables() { function clearVariables() {
weight.value = null; weight.value = null;
ean.value = null; ean.value = null;
name.value = null; name.value = null;
@@ -41,7 +48,7 @@ function clearVariables() {
ingredients.value = null; ingredients.value = null;
loading.value = false; loading.value = false;
} }
</script> </script>
<template> <template>
@@ -87,10 +94,21 @@ function clearVariables() {
autofocus autofocus
/> />
<v-row>
<v-col>
<v-text-field <v-text-field
v-model.trim="weight" v-model.trim="weight"
label="Gewicht" label="Gewicht"
/> />
</v-col>
<v-col>
<v-select
v-model="unit"
:items="store.units"
label="Einheit"
></v-select>
</v-col>
</v-row>
<v-textarea <v-textarea
v-model.trim="ean" v-model.trim="ean"
+13 -2
View File
@@ -3,6 +3,7 @@
import type {Ref} from "vue"; import type {Ref} from "vue";
import {Variant} from "~/types/variant"; import {Variant} from "~/types/variant";
import DeleteConfirm from "~/components/ui/deleteConfirm.vue"; import DeleteConfirm from "~/components/ui/deleteConfirm.vue";
import type {Unit} from "~/types/unit";
let store = useLabelEditorStore(); let store = useLabelEditorStore();
@@ -15,6 +16,7 @@
let weight: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.weight))); let weight: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.weight)));
let ean: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.ean))); let ean: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.ean)));
let name: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.name))); let name: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.name)));
let unit: Ref<Unit | null> = ref<Unit | null>(JSON.parse(JSON.stringify(props.variant.unit)));
let description: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.description))); let description: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.description)));
let ingredients: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.ingredients))); let ingredients: Ref<string | null> = ref<string | null>(JSON.parse(JSON.stringify(props.variant.ingredients)));
@@ -35,13 +37,14 @@
weight.value = JSON.parse(JSON.stringify(props.variant.weight)); weight.value = JSON.parse(JSON.stringify(props.variant.weight));
ean.value = JSON.parse(JSON.stringify(props.variant.ean)); ean.value = JSON.parse(JSON.stringify(props.variant.ean));
name.value = JSON.parse(JSON.stringify(props.variant.name)); name.value = JSON.parse(JSON.stringify(props.variant.name));
unit.value = JSON.parse(JSON.stringify(props.variant.unit));
description.value = JSON.parse(JSON.stringify(props.variant.description)); description.value = JSON.parse(JSON.stringify(props.variant.description));
ingredients.value = JSON.parse(JSON.stringify(props.variant.ingredients)); ingredients.value = JSON.parse(JSON.stringify(props.variant.ingredients));
} }
async function updateVariant() { async function updateVariant() {
if (weight.value != null) { if (weight.value != null && unit.value != null) {
let updateVariant: Variant = new Variant(props.variant.id, weight.value, ean.value, name.value, description.value, ingredients.value); let updateVariant: Variant = new Variant(props.variant.id, weight.value, ean.value, name.value, description.value, ingredients.value, unit.value);
await store.updateVariant(updateVariant); await store.updateVariant(updateVariant);
@@ -137,6 +140,14 @@
:readonly="!edit" :readonly="!edit"
/> />
</v-row> </v-row>
<v-row>
<v-select
v-model="unit"
:items="store.units"
label="Einheit"
:readonly="!edit"
></v-select>
</v-row>
<v-row> <v-row>
<v-text-field <v-text-field
v-model.trim="ean" v-model.trim="ean"
+12 -2
View File
@@ -4,6 +4,7 @@ import {NotificationMessage} from "~/types/notificationMessage";
import type {RuntimeConfig} from "nuxt/schema"; import type {RuntimeConfig} from "nuxt/schema";
import {Origin} from "~/types/origin"; import {Origin} from "~/types/origin";
import type {Variant} from "~/types/variant"; import type {Variant} from "~/types/variant";
import {Unit} from "~/types/unit";
interface State { interface State {
config: RuntimeConfig, config: RuntimeConfig,
@@ -21,6 +22,7 @@ interface State {
articleEdit: Article | null, articleEdit: Article | null,
origins: Array<Origin>, origins: Array<Origin>,
units: Array<Unit>,
} }
export const useLabelEditorStore = defineStore('label_editor_store', { export const useLabelEditorStore = defineStore('label_editor_store', {
@@ -42,8 +44,16 @@ export const useLabelEditorStore = defineStore('label_editor_store', {
origins: [ origins: [
Origin.EU, Origin.EU,
Origin.NonEU, Origin.NonEU,
Origin.EUnonEU Origin.EUnonEU,
] ],
units: [
Unit.G,
Unit.Kg,
Unit.Ml,
Unit.L,
Unit.Stk,
],
}), }),
actions: { actions: {
// Notification // Notification
+4 -1
View File
@@ -1,5 +1,6 @@
import type {Variant} from "~/types/variant"; import type {Variant} from "~/types/variant";
import type {Origin} from "~/types/origin"; import type {Origin} from "~/types/origin";
import type {Unit} from "~/types/unit";
export class Article { export class Article {
readonly id: number; readonly id: number;
@@ -8,15 +9,17 @@ export class Article {
origin: Origin; origin: Origin;
description: string | null; description: string | null;
ingredients: string | null; ingredients: string | null;
default_unit: Unit;
variants: Array<Variant>; variants: Array<Variant>;
constructor(id: number, name: string, bio: boolean, origin: Origin, description: string | null, ingredients: string | null, variants: Array<Variant> | null) { constructor(id: number, name: string, bio: boolean, origin: Origin, description: string | null, ingredients: string | null, default_unit: Unit, variants: Array<Variant> | null) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.bio = bio; this.bio = bio;
this.origin = origin; this.origin = origin;
this.description = description; this.description = description;
this.ingredients = ingredients; this.ingredients = ingredients;
this.default_unit = default_unit;
if (variants == null) { if (variants == null) {
this.variants = []; this.variants = [];
+22
View File
@@ -0,0 +1,22 @@
export enum Unit {
Kg = "Kg",
G = "G",
L = "L",
Ml = "Ml",
Stk = "Stk",
}
export function unitToString(unit: Unit): string {
switch (unit) {
case Unit.Kg:
return "Kilogramm";
case Unit.G:
return "Gramm";
case Unit.L:
return "Liter";
case Unit.Ml:
return "Milliliter";
case Unit.Stk:
return "Stück";
}
}
+5 -1
View File
@@ -1,3 +1,5 @@
import type {Unit} from "~/types/unit";
export class Variant { export class Variant {
readonly id: number; readonly id: number;
weight: string; weight: string;
@@ -5,13 +7,15 @@ export class Variant {
name: string | null; name: string | null;
description: string | null; description: string | null;
ingredients: string | null; ingredients: string | null;
unit: Unit;
constructor(id: number, weight: string, ean: string|null, name: string|null, description:string|null, ingredients: string|null) { constructor(id: number, weight: string, ean: string|null, name: string|null, description:string|null, ingredients: string|null, unit: Unit) {
this.id = id; this.id = id;
this.weight = weight; this.weight = weight;
this.ean = ean; this.ean = ean;
this.name = name; this.name = name;
this.description = description; this.description = description;
this.ingredients = ingredients; this.ingredients = ingredients;
this.unit = unit;
} }
} }
+35
View File
@@ -0,0 +1,35 @@
-- Add migration script here
CREATE TABLE unit
(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 0 MINVALUE 0 ),
name text NOT NULL,
display text NOT NULL,
CONSTRAINT unit_id PRIMARY KEY (id)
INCLUDE(id)
);
-- Insert units
INSERT INTO unit (name, display) VALUES ('KG', 'kg');
INSERT INTO unit (name, display) VALUES ('G', 'g');
INSERT INTO unit (name, display) VALUES ('L', 'l');
INSERT INTO unit (name, display) VALUES ('ML', 'ml');
INSERT INTO unit (name, display) VALUES ('Stk', 'Stk');
-- Alter article and variant table to add unit
ALTER TABLE public.variant
ADD unit_id bigint NOT NULL DEFAULT 1,
ADD CONSTRAINT variant_unit_id_fkey
foreign key (unit_id)
references unit(id)
ON DELETE NO ACTION;
ALTER TABLE public.article
ADD default_unit_id bigint NOT NULL DEFAULT 1,
ADD CONSTRAINT article_unit_id_fkey
foreign key (default_unit_id)
references unit(id)
ON DELETE NO ACTION;
-- Add display text to origin table
ALTER TABLE public.origin
ADD display text NOT NULL DEFAULT 'NOT SET';
+6
View File
@@ -16,6 +16,7 @@ use tower_http::cors::{AllowOrigin, Any, CorsLayer};
use tracing::info; use tracing::info;
use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_CREDENTIALS}; use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_CREDENTIALS};
use common::models::article; use common::models::article;
use common::models::unit::Unit;
use common::models::variant::{delete_variant, insert_variant, update_variant, Variant}; use common::models::variant::{delete_variant, insert_variant, update_variant, Variant};
#[tokio::main] #[tokio::main]
@@ -102,6 +103,7 @@ async fn get_article(
origin: Origin::EU, origin: Origin::EU,
description: None, description: None,
ingredients: None, ingredients: None,
default_unit: Unit::G,
variants: vec![], variants: vec![],
}; };
@@ -129,6 +131,7 @@ async fn create_article(
origin: Origin::EU, origin: Origin::EU,
description: None, description: None,
ingredients: None, ingredients: None,
default_unit: Unit::G,
variants: vec![], variants: vec![],
}; };
@@ -158,6 +161,7 @@ async fn update_article_route(
origin: Origin::EU, origin: Origin::EU,
description: None, description: None,
ingredients: None, ingredients: None,
default_unit: Unit::G,
variants: vec![], variants: vec![],
}; };
@@ -206,6 +210,7 @@ async fn add_variant(
name: None, name: None,
description: None, description: None,
ingredients: None, ingredients: None,
unit: Unit::G,
}; };
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant)) (StatusCode::INTERNAL_SERVER_ERROR, Json(variant))
@@ -234,6 +239,7 @@ async fn update_variant_route(
name: None, name: None,
description: None, description: None,
ingredients: None, ingredients: None,
unit: Unit::G,
}; };
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant)) (StatusCode::INTERNAL_SERVER_ERROR, Json(variant))