diff --git a/common/src/models/article.rs b/common/src/models/article.rs index 24b8c5f..0aaa425 100644 --- a/common/src/models/article.rs +++ b/common/src/models/article.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use sqlx::{FromRow, PgPool}; use crate::models::origin::Origin; -use crate::models::variant::Variant; +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}; @@ -29,7 +29,20 @@ fn from_postgres_row_small(row: PgRow) -> Article { } } -fn from_postgres_row(row: PgRow, pool: PgPool) -> Article { +async fn from_postgres_row(row: PgRow, pool: PgPool) -> Article { + let variants_option = load_article_variants(row.get(0), pool).await; + + let variants = match variants_option { + Some(variants_inner) => { + info!("Found Variants"); + variants_inner + } + None => { + info!("No Variants"); + vec![] + } + }; + Article { id: row.get(0), name: row.get(1), @@ -37,7 +50,7 @@ fn from_postgres_row(row: PgRow, pool: PgPool) -> Article { origin: row.get(3), description: row.get(4), ingredients: row.get(5), - variants: vec![], + variants: variants, } } @@ -85,7 +98,7 @@ pub async fn load_article(id: i64, pool: PgPool) -> Option
{ match res { Ok(record) => { - let article = from_postgres_row(record, pool); + let article = from_postgres_row(record, pool).await; Some(article) } @@ -96,7 +109,7 @@ pub async fn load_article(id: i64, pool: PgPool) -> Option
{ } } -pub async fn insert_article(article: &Article, pool: PgPool) { +pub async fn insert_article(article: &Article, pool: PgPool) -> Option
{ let mut args = PgArguments::default(); args.add(&article.name); args.add(&article.bio); @@ -105,14 +118,26 @@ pub async fn insert_article(article: &Article, pool: PgPool) { args.add(&article.ingredients); let statement = format!( - "INSERT INTO {} (name, bio, origin, description, ingredients) VALUES ($1, $2, $3, $4, $5)", + "INSERT INTO {} (name, bio, origin, description, ingredients) VALUES ($1, $2, $3, $4, $5) RETURNING *", "article", ); let res = sqlx::query_with( statement.as_str(), args) - .execute(&pool).await.unwrap(); + .fetch_one(&pool).await; info!("{:#?}", res); + + match res { + Ok(record) => { + let article = from_postgres_row(record, pool).await; + + Some(article) + } + Err(err) => { + error!("Error while loading newly created article: {}", article.name); + None + } + } } diff --git a/common/src/models/variant.rs b/common/src/models/variant.rs index 73605ab..07e7d23 100644 --- a/common/src/models/variant.rs +++ b/common/src/models/variant.rs @@ -1,8 +1,90 @@ +use log::{error, info}; use serde::{Deserialize, Serialize}; -use sqlx::FromRow; +use sqlx::{Arguments, FromRow, PgPool, Row}; +use sqlx::postgres::{PgArguments, PgRow}; #[derive(Debug, Deserialize, Serialize, Clone, FromRow)] pub struct Variant { pub id: i64, - + pub weight: String, + pub ean: Option, + pub name: Option, + pub description: Option, + pub ingredients: Option, +} + +fn from_postgres_row_small(row: PgRow) -> Variant { + Variant { + id: row.get(0), + weight: row.get(2), + ean: row.get(3), + name: row.get(4), + description: row.get(5), + ingredients: row.get(6), + } +} + +pub async fn load_article_variants(article_id: i64, pool: PgPool) -> Option> { + let statement = format!( + "SELECT * FROM {} WHERE article_id = $1", + "variant", + ); + + let mut args = PgArguments::default(); + args.add(article_id); + + let res = sqlx::query_with( + statement.as_str(), args) + .fetch_all(&pool).await; + + let mut variants: Vec = Vec::new(); + + match res { + Ok(records) => { + for record in records { + variants.push( + from_postgres_row_small(record) + ); + } + + Some(variants) + } + Err(err) => { + error!("Error while loading variants for article: {}", article_id); + None + } + } +} + +pub async fn insert_variant(article_id: i64, variant: &Variant, pool: PgPool) -> Option { + let mut args = PgArguments::default(); + args.add(&article_id); + args.add(&variant.weight); + args.add(&variant.ean); + args.add(&variant.name); + args.add(&variant.description); + args.add(&variant.ingredients); + + let statement = format!( + "INSERT INTO {} (article_id, weight, ean, name, description, ingredients) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *", + "variant", + ); + + let res = sqlx::query_with( + statement.as_str(), + args) + .fetch_one(&pool).await; + + info!("{:#?}", res); + + match res { + Ok(record) => { + let variant = from_postgres_row_small(record); + Some(variant) + } + Err(err) => { + error!("Error while loading newly created variant for article: {}", article_id); + None + } + } } diff --git a/frontend/app/components/article/article.vue b/frontend/app/components/article/article.vue index 9068ab0..a9838c8 100644 --- a/frontend/app/components/article/article.vue +++ b/frontend/app/components/article/article.vue @@ -47,6 +47,7 @@ counter :persistentCounter=true rows="6" + no-resize >