Added functionality to select article.
This commit is contained in:
@@ -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<Variant>,
|
||||
}
|
||||
|
||||
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<Vec<Article>> {
|
||||
let statement = format!(
|
||||
"SELECT * FROM {}",
|
||||
"article",
|
||||
);
|
||||
|
||||
let res = sqlx::query(
|
||||
statement.as_str())
|
||||
.fetch_all(&pool).await;
|
||||
|
||||
let mut articles: Vec<Article> = 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<Article> {
|
||||
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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -3,6 +3,6 @@ use sqlx::FromRow;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]
|
||||
pub struct Variant {
|
||||
pub id: usize,
|
||||
pub id: i64,
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user