diff --git a/common/src/models/article.rs b/common/src/models/article.rs index 9c51508..24b8c5f 100644 --- a/common/src/models/article.rs +++ b/common/src/models/article.rs @@ -4,11 +4,11 @@ use crate::models::origin::Origin; use crate::models::variant::Variant; use sqlx::{Arguments, Pool, Row, Postgres, Error}; use sqlx::postgres::{PgArguments, PgQueryResult, PgRow}; -use log::{debug, info}; +use log::{debug, error, info}; #[derive(Debug, Deserialize, Serialize, Clone, FromRow)] pub struct Article { - pub id: usize, + pub id: i64, pub name: String, pub bio: bool, pub origin: Origin, @@ -17,6 +17,85 @@ pub struct Article { pub variants: Vec, } +fn from_postgres_row_small(row: PgRow) -> Article { + Article { + id: row.get(0), + name: row.get(1), + bio: row.get(2), + origin: row.get(3), + description: row.get(4), + ingredients: row.get(5), + variants: vec![], + } +} + +fn from_postgres_row(row: PgRow, pool: PgPool) -> Article { + Article { + id: row.get(0), + name: row.get(1), + bio: row.get(2), + origin: row.get(3), + description: row.get(4), + ingredients: row.get(5), + variants: vec![], + } +} + +pub async fn load_articles(pool: PgPool) -> Option> { + let statement = format!( + "SELECT * FROM {}", + "article", + ); + + let res = sqlx::query( + statement.as_str()) + .fetch_all(&pool).await; + + let mut articles: Vec
= Vec::new(); + + match res { + Ok(records) => { + for record in records { + articles.push( + from_postgres_row_small(record) + ); + } + + Some(articles) + } + Err(err) => { + error!("Error while loading all articles"); + None + } + } +} + +pub async fn load_article(id: i64, pool: PgPool) -> Option
{ + let statement = format!( + "SELECT * FROM {} WHERE id = $1", + "article" + ); + + let mut args = PgArguments::default(); + args.add(id); + + let res = sqlx::query_with( + statement.as_str(), args) + .fetch_one(&pool).await; + + match res { + Ok(record) => { + let article = from_postgres_row(record, pool); + + Some(article) + } + Err(err) => { + error!("Error while loading all article id: {}", id); + None + } + } +} + pub async fn insert_article(article: &Article, pool: PgPool) { let mut args = PgArguments::default(); args.add(&article.name); @@ -25,7 +104,6 @@ pub async fn insert_article(article: &Article, pool: PgPool) { args.add(&article.description); args.add(&article.ingredients); - // Add to price history: let statement = format!( "INSERT INTO {} (name, bio, origin, description, ingredients) VALUES ($1, $2, $3, $4, $5)", "article", diff --git a/common/src/models/origin.rs b/common/src/models/origin.rs index 939df11..b0d463e 100644 --- a/common/src/models/origin.rs +++ b/common/src/models/origin.rs @@ -2,7 +2,7 @@ use std::fmt; use std::fmt::Formatter; use serde::{Deserialize, Serialize}; -#[repr(i32)] +#[repr(i64)] #[derive(Deserialize, Serialize, Copy, Clone, PartialEq, sqlx::Type)] pub enum Origin { EU, diff --git a/common/src/models/variant.rs b/common/src/models/variant.rs index 8c03ba4..73605ab 100644 --- a/common/src/models/variant.rs +++ b/common/src/models/variant.rs @@ -3,6 +3,6 @@ use sqlx::FromRow; #[derive(Debug, Deserialize, Serialize, Clone, FromRow)] pub struct Variant { - pub id: usize, + pub id: i64, } diff --git a/frontend/app/components/article/article.vue b/frontend/app/components/article/article.vue new file mode 100644 index 0000000..b8c3e5c --- /dev/null +++ b/frontend/app/components/article/article.vue @@ -0,0 +1,60 @@ + + + + + \ No newline at end of file diff --git a/frontend/app/components/article/pickArticle.vue b/frontend/app/components/article/pickArticle.vue new file mode 100644 index 0000000..fb91bbf --- /dev/null +++ b/frontend/app/components/article/pickArticle.vue @@ -0,0 +1,137 @@ + + + + + \ No newline at end of file diff --git a/frontend/app/components/layout/customHeader.vue b/frontend/app/components/layout/customHeader.vue index b10a3d8..8047f9b 100644 --- a/frontend/app/components/layout/customHeader.vue +++ b/frontend/app/components/layout/customHeader.vue @@ -1,5 +1,6 @@