Added nutritional values, with backend loading function.
This commit is contained in:
1 parent
917f3dc640
commit
627564e8c6
10 files changed
+294
-3
No files matched your search
@@ -5,6 +5,7 @@ use crate::models::variant::{load_article_variants, Variant};
|
||||
use sqlx::{Arguments, Pool, Row, Postgres, Error, AssertSqlSafe};
|
||||
use sqlx::postgres::{PgArguments, PgQueryResult, PgRow};
|
||||
use log::{debug, error, info};
|
||||
use crate::models::nutritional::{load_nutritional, Nutritional};
|
||||
use crate::models::unit::Unit;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
@@ -23,6 +24,7 @@ pub struct Article {
|
||||
pub bio_info: Option<String>,
|
||||
pub topm_id: Option<String>,
|
||||
pub additional_name: Option<String>,
|
||||
pub nutritional: Option<Nutritional>,
|
||||
}
|
||||
|
||||
fn from_postgres_row_small(row: PgRow) -> Article {
|
||||
@@ -41,6 +43,7 @@ fn from_postgres_row_small(row: PgRow) -> Article {
|
||||
bio_info: row.get(10),
|
||||
topm_id: row.get(11),
|
||||
additional_name: row.get(12),
|
||||
nutritional: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +61,16 @@ async fn from_postgres_row(row: PgRow, pool: PgPool) -> Article {
|
||||
}
|
||||
};
|
||||
|
||||
let nutritional_id_option: Option<i64> = row.get(13);
|
||||
let nutritional: Option<Nutritional> = match nutritional_id_option {
|
||||
Some(nutritional_id) => {
|
||||
load_nutritional(nutritional_id, &pool).await
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Article {
|
||||
id: row.get(0),
|
||||
name: row.get(1),
|
||||
@@ -73,6 +86,7 @@ async fn from_postgres_row(row: PgRow, pool: PgPool) -> Article {
|
||||
bio_info: row.get(10),
|
||||
topm_id: row.get(11),
|
||||
additional_name: row.get(12),
|
||||
nutritional: nutritional,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,3 +5,4 @@ pub mod unit;
|
||||
pub mod label;
|
||||
pub mod settings;
|
||||
pub mod supplier;
|
||||
pub mod nutritional;
|
||||
@@ -0,0 +1,172 @@
|
||||
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 Nutritional {
|
||||
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 Nutritional {
|
||||
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) -> Nutritional {
|
||||
Nutritional {
|
||||
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_nutritional(nutritional_id: i64, pool: &PgPool) -> Option<Nutritional> {
|
||||
let statement = format!(
|
||||
"SELECT * FROM {} WHERE id = $1",
|
||||
"nutritional"
|
||||
);
|
||||
|
||||
let mut args = PgArguments::default();
|
||||
args.add(nutritional_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 nutritional value: ID: {}", nutritional_id);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn insert_nutritional(nutritional: &Nutritional, pool: PgPool) -> Option<Nutritional> {
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&nutritional.calorific_value_kj);
|
||||
args.add(&nutritional.calorific_value_kcal);
|
||||
args.add(&nutritional.fat);
|
||||
args.add(&nutritional.saturated_fat);
|
||||
args.add(&nutritional.carbohydrates);
|
||||
args.add(&nutritional.sugar);
|
||||
args.add(&nutritional.fibers);
|
||||
args.add(&nutritional.protein);
|
||||
args.add(&nutritional.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 *",
|
||||
"nutritional",
|
||||
);
|
||||
|
||||
let res = sqlx::query_with(
|
||||
AssertSqlSafe(statement.as_str()),
|
||||
args)
|
||||
.fetch_one(&pool).await;
|
||||
|
||||
info!("{:#?}", res);
|
||||
|
||||
match res {
|
||||
Ok(record) => {
|
||||
let nutritional = from_postgres_row(record, &pool).await;
|
||||
|
||||
Some(nutritional)
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Error while loading newly created nutritional values. ID: {}", nutritional.id);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_nutritional(nutritional: &Nutritional, pool: PgPool) -> Option<Nutritional> {
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&nutritional.id);
|
||||
args.add(&nutritional.calorific_value_kj);
|
||||
args.add(&nutritional.calorific_value_kcal);
|
||||
args.add(&nutritional.fat);
|
||||
args.add(&nutritional.saturated_fat);
|
||||
args.add(&nutritional.carbohydrates);
|
||||
args.add(&nutritional.sugar);
|
||||
args.add(&nutritional.fibers);
|
||||
args.add(&nutritional.protein);
|
||||
args.add(&nutritional.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 *",
|
||||
"nutritional",
|
||||
);
|
||||
|
||||
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 nutritional values. ID {}", nutritional.id);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_nutritional(nutritional_id: i64, pool: PgPool) -> Option<()> {
|
||||
let result = sqlx::query(
|
||||
AssertSqlSafe(format!("DELETE FROM {} WHERE id = {}",
|
||||
"nutritional",
|
||||
nutritional_id
|
||||
).as_str())
|
||||
).execute(&pool).await;
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
Some(())
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error deleting nutritional. ID: {:#?}", err);
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Arguments, AssertSqlSafe, FromRow, PgPool, Row};
|
||||
use sqlx::postgres::{PgArguments, PgRow};
|
||||
use crate::models::label::{load_label, Label};
|
||||
use crate::models::nutritional::{load_nutritional, Nutritional};
|
||||
use crate::models::unit::Unit;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
@@ -17,6 +18,7 @@ pub struct Variant {
|
||||
pub label: Option<Label>,
|
||||
pub variant_description: Option<String>,
|
||||
pub topm_row: Option<String>,
|
||||
pub nutritional: Option<Nutritional>,
|
||||
}
|
||||
|
||||
async fn from_postgres_row(row: PgRow, pool: &PgPool) -> Variant {
|
||||
@@ -30,6 +32,16 @@ async fn from_postgres_row(row: PgRow, pool: &PgPool) -> Variant {
|
||||
}
|
||||
};
|
||||
|
||||
let nutritional_id_option: Option<i64> = row.get(11);
|
||||
let nutritional: Option<Nutritional> = match nutritional_id_option {
|
||||
Some(nutritional_id) => {
|
||||
load_nutritional(nutritional_id, &pool).await
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Variant {
|
||||
id: row.get(0),
|
||||
weight: row.get(2),
|
||||
@@ -41,6 +53,7 @@ async fn from_postgres_row(row: PgRow, pool: &PgPool) -> Variant {
|
||||
label: label,
|
||||
variant_description: row.get(9),
|
||||
topm_row: row.get(10),
|
||||
nutritional: nutritional,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +99,15 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
}
|
||||
};
|
||||
|
||||
let nutritional_id = match &variant.nutritional {
|
||||
Some(label) => {
|
||||
Some(label.id)
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&article_id);
|
||||
args.add(&variant.weight);
|
||||
@@ -97,9 +119,12 @@ pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) ->
|
||||
args.add(label_id);
|
||||
args.add(&variant.variant_description);
|
||||
args.add(&variant.topm_row);
|
||||
args.add(nutritional_id);
|
||||
|
||||
let statement = format!(
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients, unit_id, label_id, variant_description, topm_row) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *",
|
||||
"INSERT INTO {} (article_id, weight, ean, name, description, ingredients, unit_id, label_id, \
|
||||
variant_description, topm_row, nutritional_id) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *",
|
||||
"variant",
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
@@ -1,6 +1,7 @@
|
||||
import type {Variant} from "~/types/variant";
|
||||
import type {Origin} from "~/types/origin";
|
||||
import type {Unit} from "~/types/unit";
|
||||
import type {Nutritional} from "~/types/nutritional.ts";
|
||||
|
||||
export class Article {
|
||||
readonly id: number;
|
||||
@@ -17,11 +18,13 @@ export class Article {
|
||||
bio_info: string | null;
|
||||
topm_id: string | null;
|
||||
additional_name: string | null;
|
||||
nutritional: Nutritional | null;
|
||||
|
||||
constructor(id: number, name: string, bio: boolean, origin: Origin, description: string | null,
|
||||
ingredients: string | null, default_unit: Unit, variants: Array<Variant> | null,
|
||||
mhd: string | null, bio_origin: string | null, allergens_text: string | null,
|
||||
bio_info: string | null, topm_id: string | null, additional_name: string | null) {
|
||||
bio_info: string | null, topm_id: string | null, additional_name: string | null,
|
||||
nutritional: Nutritional | null) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.bio = bio;
|
||||
@@ -42,5 +45,6 @@ export class Article {
|
||||
this.bio_info = bio_info;
|
||||
this.topm_id = topm_id;
|
||||
this.additional_name = additional_name;
|
||||
this.nutritional = nutritional;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export class Nutritional {
|
||||
id: number;
|
||||
calorific_value_kj: number;
|
||||
calorific_value_kcal: number;
|
||||
fat: number;
|
||||
saturated_fat: number;
|
||||
carbohydrates: number;
|
||||
sugar: number;
|
||||
fibers: number;
|
||||
protein: number;
|
||||
salt: number;
|
||||
|
||||
constructor(id: number, calorific_value_kj: number, calorific_value_kcal: number, fat: number,
|
||||
saturated_fat: number, carbohydrates: number, sugar: number,
|
||||
fibers: number, protein: number, salt: number) {
|
||||
this.id = id;
|
||||
this.calorific_value_kj = calorific_value_kj;
|
||||
this.calorific_value_kcal = calorific_value_kcal;
|
||||
this.fat = fat;
|
||||
this.saturated_fat = saturated_fat;
|
||||
this.carbohydrates = carbohydrates;
|
||||
this.sugar = sugar;
|
||||
this.fibers = fibers;
|
||||
this.protein = protein;
|
||||
this.salt = salt;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import type {Unit} from "~/types/unit";
|
||||
import type {Label} from "~/types/label";
|
||||
import type {Nutritional} from "~/types/nutritional.ts";
|
||||
|
||||
export class Variant {
|
||||
readonly id: number;
|
||||
@@ -12,10 +13,12 @@ export class Variant {
|
||||
label: Label | null;
|
||||
variant_description: string | null;
|
||||
topm_row: string | null;
|
||||
nutritional: Nutritional | null;
|
||||
|
||||
constructor(id: number, weight: string, ean: string|null, name: string|null,
|
||||
description:string|null, ingredients: string|null, unit: Unit,
|
||||
label: Label | null, variant_description: string | null, topm_row: string | null) {
|
||||
label: Label | null, variant_description: string | null, topm_row: string | null,
|
||||
nutritional: Nutritional | null) {
|
||||
this.id = id;
|
||||
this.weight = weight;
|
||||
this.ean = ean;
|
||||
@@ -26,5 +29,6 @@ export class Variant {
|
||||
this.label = label;
|
||||
this.variant_description = variant_description;
|
||||
this.topm_row = topm_row;
|
||||
this.nutritional = nutritional;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Add migration script here
|
||||
CREATE TABLE nutritional
|
||||
(
|
||||
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 ) Unique,
|
||||
calorific_value_kj double precision DEFAULT 0 NOT NULL,
|
||||
calorific_value_kcal double precision DEFAULT 0 NOT NULL,
|
||||
fat double precision DEFAULT 0 NOT NULL,
|
||||
saturated_fat double precision DEFAULT 0 NOT NULL,
|
||||
carbohydrates double precision DEFAULT 0 NOT NULL,
|
||||
sugar double precision DEFAULT 0 NOT NULL,
|
||||
fibers double precision DEFAULT 0 NOT NULL,
|
||||
protein double precision DEFAULT 0 NOT NULL,
|
||||
salt double precision DEFAULT 0 NOT NULL
|
||||
);
|
||||
|
||||
ALTER TABLE public.article
|
||||
ADD nutritional_id bigint,
|
||||
ADD CONSTRAINT article_nutritional_id_fkey
|
||||
foreign key (nutritional_id)
|
||||
references nutritional(id)
|
||||
ON DELETE NO ACTION;
|
||||
|
||||
ALTER TABLE public.variant
|
||||
ADD nutritional_id bigint,
|
||||
ADD CONSTRAINT variant_nutritional_id_fkey
|
||||
foreign key (nutritional_id)
|
||||
references nutritional(id)
|
||||
ON DELETE NO ACTION;
|
||||
@@ -125,6 +125,7 @@ async fn get_article(
|
||||
bio_info: None,
|
||||
topm_id: None,
|
||||
additional_name: None,
|
||||
nutritional: None,
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(article))
|
||||
@@ -159,6 +160,7 @@ async fn create_article(
|
||||
bio_info: None,
|
||||
topm_id: None,
|
||||
additional_name: None,
|
||||
nutritional: None,
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(article))
|
||||
@@ -195,6 +197,7 @@ async fn update_article_route(
|
||||
bio_info: None,
|
||||
topm_id: None,
|
||||
additional_name: None,
|
||||
nutritional: None,
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(article))
|
||||
@@ -246,6 +249,7 @@ async fn add_variant(
|
||||
label: None,
|
||||
variant_description: None,
|
||||
topm_row: None,
|
||||
nutritional: None,
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant))
|
||||
@@ -278,6 +282,7 @@ async fn update_variant_route(
|
||||
label: None,
|
||||
variant_description: None,
|
||||
topm_row: None,
|
||||
nutritional: None,
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant))
|
||||
|
||||
Reference in new issue
Block a user