-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add recipe ingredient draft entity
This is used for external recipes which can not assign an ingredient to a specific step.
- Loading branch information
Showing
25 changed files
with
598 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::{ | ||
command::error::{CommandError, CommandError::NotFound}, | ||
entity::recipe_ingredient_draft::Model, | ||
entity_crud::{ | ||
recipe_ingredient_draft::{ | ||
RecipeIngredientDraftCondition, RecipeIngredientDraftCreate, RecipeIngredientDraftCrud, | ||
RecipeIngredientDraftFilter, RecipeIngredientDraftUpdate, | ||
}, | ||
EntityCrudTrait, | ||
}, | ||
}; | ||
|
||
#[tauri::command] | ||
pub async fn entity_create_recipe_ingredient_draft( | ||
create: RecipeIngredientDraftCreate, | ||
) -> Result<i64, CommandError> { | ||
let id = RecipeIngredientDraftCrud::create(create).await?; | ||
Ok(id) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn entity_read_recipe_ingredient_draft(id: i64) -> Result<Model, CommandError> { | ||
let model_option = RecipeIngredientDraftCrud::read(id).await?; | ||
let model = model_option.ok_or(NotFound)?; | ||
Ok(model) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn entity_update_recipe_ingredient_draft( | ||
update: RecipeIngredientDraftUpdate, | ||
) -> Result<(), CommandError> { | ||
RecipeIngredientDraftCrud::update(update).await?; | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn entity_delete_recipe_ingredient_draft(id: i64) -> Result<(), CommandError> { | ||
RecipeIngredientDraftCrud::delete(id).await?; | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn entity_list_recipe_ingredient_draft( | ||
filter: RecipeIngredientDraftFilter, | ||
) -> Result<Vec<i64>, CommandError> { | ||
let list = RecipeIngredientDraftCrud::list(filter).await?; | ||
Ok(list) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn entity_count_recipe_ingredient_draft( | ||
condition: Option<RecipeIngredientDraftCondition>, | ||
) -> Result<i64, CommandError> { | ||
let count = RecipeIngredientDraftCrud::count(condition).await?; | ||
Ok(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! This module implements the recipe ingredient draft entity. | ||
//! | ||
//! See [`Model`] for more information. | ||
use sea_orm::entity::prelude::*; | ||
use serde::Serialize; | ||
|
||
/// This struct represents a recipe ingredient draft. | ||
/// | ||
/// Just like [`super::recipe_step_ingredient::Model`] it is not yet split up into quantity, unit, and does not include a reference to an [`super::ingredient::Model`]. | ||
/// But in contrast to [`super::recipe_step_ingredient::Model`] this entity is related to the recipe directly because a [`super::recipe_step::Model`] could not be determined. | ||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
#[sea_orm(table_name = "recipe_ingredient_draft")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i64, | ||
pub order: i64, | ||
pub text: String, | ||
pub recipe_id: i64, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::recipe::Entity", | ||
from = "Column::RecipeId", | ||
to = "super::recipe::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "Cascade" | ||
)] | ||
Recipe, | ||
} | ||
|
||
impl Related<super::recipe::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Recipe.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! This module implements [`EntityCrudTrait`] for [`crate::entity::recipe_ingredient_draft`]. | ||
use sea_orm::{ | ||
sea_query::IntoCondition, | ||
ActiveValue::{NotSet, Set, Unchanged}, | ||
ColumnTrait, Condition, DeriveIntoActiveModel, IntoActiveModel, QueryOrder, Select, | ||
}; | ||
use serde::Deserialize; | ||
|
||
use crate::{ | ||
entity::recipe_ingredient_draft::{ActiveModel, Column, Entity, Model, PrimaryKey, Relation}, | ||
entity_crud::{EntityCrudTrait, Filter, Order, OrderBy}, | ||
event::channel::{ | ||
ENTITY_ACTION_CREATED_RECIPE_INGREDIENT_DRAFT, | ||
ENTITY_ACTION_DELETED_RECIPE_INGREDIENT_DRAFT, | ||
ENTITY_ACTION_UPDATED_RECIPE_INGREDIENT_DRAFT, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Deserialize, DeriveIntoActiveModel)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RecipeIngredientDraftCreate { | ||
pub order: i64, | ||
pub text: String, | ||
pub recipe_id: i64, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RecipeIngredientDraftUpdate { | ||
pub id: i64, | ||
pub order: Option<i64>, | ||
pub text: Option<String>, | ||
} | ||
|
||
impl IntoActiveModel<ActiveModel> for RecipeIngredientDraftUpdate { | ||
fn into_active_model(self) -> ActiveModel { | ||
ActiveModel { | ||
id: Unchanged(self.id), | ||
order: match self.order { | ||
Some(order) => Set(order), | ||
_ => NotSet, | ||
}, | ||
text: match self.text { | ||
Some(text) => Set(text), | ||
_ => NotSet, | ||
}, | ||
recipe_id: NotSet, | ||
} | ||
} | ||
} | ||
|
||
pub type RecipeIngredientDraftFilter = | ||
Filter<RecipeIngredientDraftCondition, RecipeIngredientDraftOrderBy>; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RecipeIngredientDraftCondition { | ||
pub recipe_id: Option<i64>, | ||
} | ||
|
||
impl IntoCondition for RecipeIngredientDraftCondition { | ||
fn into_condition(self) -> Condition { | ||
Condition::all().add_option( | ||
self.recipe_id | ||
.map(|recipe_id| Column::RecipeId.eq(recipe_id)), | ||
) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum RecipeIngredientDraftOrderBy { | ||
Order(Order), | ||
} | ||
|
||
impl OrderBy for RecipeIngredientDraftOrderBy { | ||
type Entity = Entity; | ||
|
||
fn add(self, select: Select<Self::Entity>) -> Select<Self::Entity> { | ||
match self { | ||
RecipeIngredientDraftOrderBy::Order(order) => { | ||
select.order_by(Column::Order, order.into()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct RecipeIngredientDraftCrud {} | ||
|
||
impl EntityCrudTrait for RecipeIngredientDraftCrud { | ||
type Entity = Entity; | ||
type Model = Model; | ||
type ActiveModel = ActiveModel; | ||
type Column = Column; | ||
type Relation = Relation; | ||
type PrimaryKey = PrimaryKey; | ||
type EntityCreate = RecipeIngredientDraftCreate; | ||
type EntityUpdate = RecipeIngredientDraftUpdate; | ||
type EntityCondition = RecipeIngredientDraftCondition; | ||
type EntityOrderBy = RecipeIngredientDraftOrderBy; | ||
|
||
fn primary_key_value(model: &Model) -> i64 { | ||
model.id | ||
} | ||
|
||
fn primary_key_colum() -> Column { | ||
Column::Id | ||
} | ||
|
||
fn entity_action_created_channel() -> &'static str { | ||
ENTITY_ACTION_CREATED_RECIPE_INGREDIENT_DRAFT | ||
} | ||
|
||
fn entity_action_updated_channel() -> &'static str { | ||
ENTITY_ACTION_UPDATED_RECIPE_INGREDIENT_DRAFT | ||
} | ||
|
||
fn entity_action_deleted_channel() -> &'static str { | ||
ENTITY_ACTION_DELETED_RECIPE_INGREDIENT_DRAFT | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.