Added functionality to added article.
This commit is contained in:
@@ -14,3 +14,4 @@ sqlx = { version = "0.8.6", features = [ "runtime-async-std-native-tls", "postgr
|
||||
|
||||
# Etc.
|
||||
chrono = { version = "0.4.42", features = [ "serde" ] }
|
||||
log = "0.4.29"
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
use sqlx::{FromRow, PgPool};
|
||||
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};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
pub struct Article {
|
||||
@@ -13,3 +16,25 @@ pub struct Article {
|
||||
pub ingredients: Option<String>,
|
||||
pub variants: Vec<Variant>,
|
||||
}
|
||||
|
||||
pub async fn insert_article(article: &Article, pool: PgPool) {
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&article.name);
|
||||
args.add(&article.bio);
|
||||
args.add(&article.origin);
|
||||
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",
|
||||
);
|
||||
|
||||
let res = sqlx::query_with(
|
||||
statement.as_str(),
|
||||
args)
|
||||
.execute(&pool).await.unwrap();
|
||||
|
||||
info!("{:#?}", res);
|
||||
}
|
||||
|
||||
@@ -6,15 +6,22 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, sqlx::Type)]
|
||||
pub enum Origin {
|
||||
EU,
|
||||
NEU,
|
||||
EUnEU,
|
||||
NonEU,
|
||||
EUnonEU,
|
||||
}
|
||||
impl Origin {
|
||||
pub const VEC: &'static [Origin] = &[Origin::EU, Origin::NonEU, Origin::EUnonEU];
|
||||
|
||||
pub fn position(&self) -> usize {
|
||||
Origin::VEC.iter().position(|r| *r == *self).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn to_string(unit: Origin, f: &mut Formatter) -> fmt::Result {
|
||||
match unit {
|
||||
Origin::EU => write!(f, "EU"),
|
||||
Origin::NEU => write!(f, "Nicht EU"),
|
||||
Origin::EUnEU => write!(f, "EU / Nicht EU"),
|
||||
Origin::NonEU => write!(f, "Nicht EU"),
|
||||
Origin::EUnonEU => write!(f, "EU / Nicht EU"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user