Added data label for backend.
This commit is contained in:
@@ -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::label::{load_label, Label};
|
||||
use crate::models::unit::Unit;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
@@ -13,9 +14,13 @@ pub struct Variant {
|
||||
pub description: Option<String>,
|
||||
pub ingredients: Option<String>,
|
||||
pub unit: Unit,
|
||||
pub label: Option<Label>,
|
||||
pub variant_description: Option<String>,
|
||||
}
|
||||
|
||||
fn from_postgres_row_small(row: PgRow) -> Variant {
|
||||
async fn from_postgres_row(row: PgRow, pool: &PgPool) -> Variant {
|
||||
let label = load_label(row.get(0), pool).await;
|
||||
|
||||
Variant {
|
||||
id: row.get(0),
|
||||
weight: row.get(2),
|
||||
@@ -24,10 +29,12 @@ fn from_postgres_row_small(row: PgRow) -> Variant {
|
||||
description: row.get(5),
|
||||
ingredients: row.get(6),
|
||||
unit: row.get(7),
|
||||
label: label,
|
||||
variant_description: row.get(9),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn load_article_variants(article_id: i64, pool: PgPool) -> Option<Vec<Variant>> {
|
||||
pub async fn load_article_variants(article_id: i64, pool: &PgPool) -> Option<Vec<Variant>> {
|
||||
let statement = format!(
|
||||
"SELECT * FROM {} WHERE article_id = $1",
|
||||
"variant",
|
||||
@@ -38,7 +45,7 @@ pub async fn load_article_variants(article_id: i64, pool: PgPool) -> Option<Vec<
|
||||
|
||||
let res = sqlx::query_with(
|
||||
statement.as_str(), args)
|
||||
.fetch_all(&pool).await;
|
||||
.fetch_all(pool).await;
|
||||
|
||||
let mut variants: Vec<Variant> = Vec::new();
|
||||
|
||||
@@ -46,7 +53,7 @@ pub async fn load_article_variants(article_id: i64, pool: PgPool) -> Option<Vec<
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
variants.push(
|
||||
from_postgres_row_small(record)
|
||||
from_postgres_row(record, &pool).await
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,6 +67,15 @@ pub async fn load_article_variants(article_id: i64, pool: PgPool) -> Option<Vec<
|
||||
}
|
||||
|
||||
pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) -> Option<Variant> {
|
||||
let label_id = match &variant.label {
|
||||
Some(label) => {
|
||||
Some(label.id)
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&article_id);
|
||||
args.add(&variant.weight);
|
||||
@@ -68,9 +84,11 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
args.add(&variant.description);
|
||||
args.add(&variant.ingredients);
|
||||
args.add(&variant.unit);
|
||||
args.add(label_id);
|
||||
args.add(&variant.variant_description);
|
||||
|
||||
let statement = format!(
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients, unit_id) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *",
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients, unit_id, label_id, variant_description) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *",
|
||||
"variant",
|
||||
);
|
||||
|
||||
@@ -83,7 +101,7 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
|
||||
match res {
|
||||
Ok(record) => {
|
||||
let variant = from_postgres_row_small(record);
|
||||
let variant = from_postgres_row(record, &pool).await;
|
||||
Some(variant)
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -94,6 +112,15 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
}
|
||||
|
||||
pub async fn update_variant(variant_id: i64, variant: &Variant, pool: PgPool) -> Option<Variant> {
|
||||
let label_id = match &variant.label {
|
||||
Some(label) => {
|
||||
Some(label.id)
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&variant.id);
|
||||
args.add(&variant.weight);
|
||||
@@ -102,11 +129,13 @@ pub async fn update_variant(variant_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
args.add(&variant.description);
|
||||
args.add(&variant.ingredients);
|
||||
args.add(&variant.unit);
|
||||
args.add(label_id);
|
||||
args.add(&variant.variant_description);
|
||||
|
||||
info!("Variant: {:#?}", variant);
|
||||
|
||||
let statement = format!(
|
||||
"UPDATE {} SET weight = $2, ean = $3, name = $4, description = $5, ingredients = $6, unit_id = $7 WHERE id = $1 RETURNING *",
|
||||
"UPDATE {} SET weight = $2, ean = $3, name = $4, description = $5, ingredients = $6, unit_id = $7, label_id = $8, variant_description = $9 WHERE id = $1 RETURNING *",
|
||||
"variant",
|
||||
);
|
||||
|
||||
@@ -119,7 +148,7 @@ pub async fn update_variant(variant_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
|
||||
match res {
|
||||
Ok(record) => {
|
||||
let variant = from_postgres_row_small(record);
|
||||
let variant = from_postgres_row(record, &pool).await;
|
||||
Some(variant)
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
Reference in New Issue
Block a user