Skip to content

Commit

Permalink
fix(sqlite): fix code smells around 'get_recipe_models_for_item' query
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Jan 4, 2024
1 parent 963cb4b commit d2d5341
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/persistence/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct NewRecipe<'a> {
pub name: &'a str,
}

#[derive(Queryable)]
#[derive(Queryable, Selectable)]
#[diesel(table_name = recipes)]
pub struct RecipeModel {
pub id: i32,
Expand Down
20 changes: 8 additions & 12 deletions crates/persistence/src/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,18 @@ impl SqliteStore {
.optional()?)
}

fn get_item_recipes(
fn get_recipe_models_for_item(
connection: &mut SqliteConnection,
item_id: i32,
) -> Result<Vec<String>, StoreError> {
) -> Result<Option<Vec<RecipeModel>>, StoreError> {
use crate::schema::{items_recipes, recipes};

Ok(items_recipes::table
.filter(items_recipes::item_id.eq(item_id))
.left_join(recipes::table.on(recipes::id.eq(items_recipes::recipe_id)))
.select(recipes::name.nullable())
.load::<Option<String>>(connection)
.optional()?
.into_iter()
.flatten()
.flatten()
.collect::<Vec<String>>())
.inner_join(recipes::table.on(recipes::id.eq(items_recipes::recipe_id)))
.select(RecipeModel::as_select())
.load(connection)
.optional()?)
}
}

Expand Down Expand Up @@ -505,15 +501,15 @@ impl Storage for SqliteStore {
.into_iter()
.map(|item| {
let section = Self::get_section_model_for_item(connection, item.id)?;
let item_recipes = Self::get_item_recipes(connection, item.id)?;
let item_recipes = Self::get_recipe_models_for_item(connection, item.id)?;

let mut item: common::item::Item = item.into();

if let Some(section) = section {
item = item.with_section(section.name());
}

if !item_recipes.is_empty() {
if let Some(item_recipes) = item_recipes {
item = item.with_recipes(
item_recipes
.into_iter()
Expand Down

0 comments on commit d2d5341

Please sign in to comment.