Something went wrong. Try again.
Lotting software suite
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171use crate::models::{ingredient::Ingredient, user::Permissions};
use super::{ApplyOption, CrudBackend, Update};use anyhow::Result;use chrono::{DateTime, Utc};use serde::{Deserialize, Serialize};use sqlx::{PgConnection, prelude::FromRow};use uuid::Uuid;
#[derive(Debug, Deserialize, Serialize, Clone, FromRow)]pub struct Batch { pub id: Uuid, pub created_at: DateTime<Utc>, pub flavor: Uuid, pub lot_number: String,}
#[derive(Debug, Deserialize, Serialize)]pub struct BatchUpdate { created_at: Option<DateTime<Utc>>, flavor: Option<Uuid>, lot_number: Option<String>,}
#[derive(Debug, Deserialize, Serialize)]pub struct BatchCreate { flavor: Uuid, lot_number: String, ingredients: Vec<Uuid>,}
impl Update<Batch> for BatchUpdate { fn apply(self, other: &mut Batch) -> bool { let Self { flavor, created_at, lot_number, } = self; flavor.apply_to(&mut other.flavor) | created_at.apply_to(&mut other.created_at) | lot_number.apply_to(&mut other.lot_number) }}
impl CrudBackend for Batch { type Identifier = Uuid;
type Create = BatchCreate; type Update = BatchUpdate;
const TABLE_NAME: &'static str = "batches"; const PERM_READ: Permissions = Permissions::BatchRead; const PERM_CREATE: Permissions = Permissions::BatchCreate; const PERM_UPDATE: Permissions = Permissions::BatchUpdate; const PERM_DELETE: Permissions = Permissions::BatchDelete;
fn id(&self) -> Self::Identifier { self.id }
async fn create_inner(conn: &mut PgConnection, create: Self::Create) -> Result<Self> { let batch = sqlx::query_as!( Batch, r#"INSERT INTO batches (flavor, lot_number) VALUES ($1, $2) RETURNING *"#, create.flavor, create.lot_number ) .fetch_one(&mut *conn) .await?;
for ingredient in &create.ingredients { sqlx::query!( r#" INSERT INTO batch_ingredients (batch_id, ingredient_id) VALUES ($1, $2)"#, batch.id, ingredient ) .execute(&mut *conn) .await?; }
Ok(batch) }
async fn read_inner<'a>( conn: impl sqlx::prelude::Executor<'a, Database = sqlx::Postgres>, id: Self::Identifier, ) -> Result<Self> { let batch = sqlx::query_as!(Batch, r#"SELECT * FROM batches WHERE id = $1"#, id) .fetch_one(conn) .await?;
Ok(batch) }
async fn update_inner(&mut self, conn: &mut PgConnection) -> Result<()> { sqlx::query!( r#"UPDATE batches SET flavor = $1, lot_number = $2 WHERE id = $3"#, self.flavor, self.lot_number, self.id ) .execute(conn) .await?;
Ok(()) }
async fn delete_inner(&self, conn: &mut PgConnection) -> Result<()> { sqlx::query!(r#"DELETE FROM batches WHERE id = $1"#, self.id) .execute(conn) .await?;
Ok(()) }}
impl Batch { pub async fn remove_ingredient( &mut self, conn: &mut PgConnection, ingredient_id: Uuid, ) -> Result<()> { sqlx::query!( r#"DELETE FROM batch_ingredients WHERE batch_id = $1 AND ingredient_id = $2"#, self.id, ingredient_id ) .execute(conn) .await?;
Ok(()) }
pub async fn add_ingredient( &mut self, conn: &mut PgConnection, ingredient_id: Uuid, ) -> Result<()> { sqlx::query!( r#"INSERT INTO batch_ingredients (batch_id, ingredient_id) VALUES ($1, $2)"#, self.id, ingredient_id ) .execute(conn) .await?;
Ok(()) }
pub async fn get_ingredients<'a>( &self, conn: impl sqlx::prelude::Executor<'a, Database = sqlx::Postgres>, ) -> Result<Vec<Ingredient>> { let ingredients = sqlx::query_as!( Ingredient, r#" SELECT ingredients.* FROM ingredients INNER JOIN batch_ingredients ON ingredients.id = batch_ingredients.ingredient_id WHERE batch_ingredients.batch_id = $1 "#, self.id ) .fetch_all(conn) .await?;
Ok(ingredients) }}