Added variants base functionality.
This commit is contained in:
@@ -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<Article> {
|
||||
|
||||
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<Article> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn insert_article(article: &Article, pool: PgPool) {
|
||||
pub async fn insert_article(article: &Article, pool: PgPool) -> Option<Article> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String>,
|
||||
pub name: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub ingredients: Option<String>,
|
||||
}
|
||||
|
||||
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<Vec<Variant>> {
|
||||
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<Variant> = 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<Variant> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user