Skip to content

Commit

Permalink
Merge pull request #122 from suchapalaver/fix/fix-code-smells-around-…
Browse files Browse the repository at this point in the history
…sql-diesel-queries

Fix code smells around diesel SQL queries
  • Loading branch information
suchapalaver authored Jan 4, 2024
2 parents ca813c9 + d2d5341 commit bbee0be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
4 changes: 2 additions & 2 deletions 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 All @@ -64,7 +64,7 @@ pub struct NewSection<'a> {
pub name: &'a str,
}

#[derive(Queryable)]
#[derive(Queryable, Selectable)]
#[diesel(table_name = sections)]
pub struct Section {
pub id: i32,
Expand Down
42 changes: 19 additions & 23 deletions crates/persistence/src/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl SqliteStore {
.load::<Item>(connection)?)
}

fn get_recipe(
fn get_recipe_model_for_recipe(
connection: &mut SqliteConnection,
recipe: &str,
) -> Result<Option<Vec<RecipeModel>>, StoreError> {
Expand All @@ -212,37 +212,32 @@ impl SqliteStore {
.optional()?)
}

fn get_section_for_item(
fn get_section_model_for_item(
connection: &mut SqliteConnection,
item_id: i32,
) -> Result<Option<String>, StoreError> {
) -> Result<Option<Section>, StoreError> {
use crate::schema::{items_sections, sections};

Ok(items_sections::table
.filter(items_sections::item_id.eq(item_id))
.left_join(sections::table.on(sections::id.eq(items_sections::section_id)))
.select(sections::name.nullable())
.first::<Option<String>>(connection)
.optional()?
.flatten())
.inner_join(sections::table.on(sections::id.eq(items_sections::section_id)))
.select(Section::as_select())
.first(connection)
.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,16 +500,16 @@ impl Storage for SqliteStore {
all_items
.into_iter()
.map(|item| {
let section = Self::get_section_for_item(connection, item.id)?;
let item_recipes = Self::get_item_recipes(connection, item.id)?;
let section = Self::get_section_model_for_item(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);
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 Expand Up @@ -550,7 +545,8 @@ impl Storage for SqliteStore {
tokio::task::spawn_blocking(move || {
let mut connection = store.connection()?;
connection.immediate_transaction(|connection| {
let Some(results) = Self::get_recipe(connection, recipe.as_str())? else {
let Some(results) = Self::get_recipe_model_for_recipe(connection, recipe.as_str())?
else {
return Ok(StoreResponse::RecipeIngredients(None));
};

Expand Down

0 comments on commit bbee0be

Please sign in to comment.