fix that refactoring
This commit is contained in:
@@ -140,48 +140,6 @@
|
|||||||
},
|
},
|
||||||
"query": "DELETE FROM inventory_items\n WHERE id = ?"
|
"query": "DELETE FROM inventory_items\n WHERE id = ?"
|
||||||
},
|
},
|
||||||
"53c45b9447118c8b448df6836e68a38ff78d0b7f4d5344a1e5064d31e70396b3": {
|
|
||||||
"describe": {
|
|
||||||
"columns": [
|
|
||||||
{
|
|
||||||
"name": "id",
|
|
||||||
"ordinal": 0,
|
|
||||||
"type_info": "Text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "name",
|
|
||||||
"ordinal": 1,
|
|
||||||
"type_info": "Text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "weight",
|
|
||||||
"ordinal": 2,
|
|
||||||
"type_info": "Int64"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "description",
|
|
||||||
"ordinal": 3,
|
|
||||||
"type_info": "Text"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "category_id",
|
|
||||||
"ordinal": 4,
|
|
||||||
"type_info": "Text"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"nullable": [
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
false
|
|
||||||
],
|
|
||||||
"parameters": {
|
|
||||||
"Right": 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"query": "SELECT\n id,\n name,\n weight,\n description,\n category_id\n FROM inventory_items AS item\n WHERE item.id = ?"
|
|
||||||
},
|
|
||||||
"5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533": {
|
"5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
|
|||||||
@@ -674,7 +674,7 @@ async fn trip_row(
|
|||||||
let item_row = view::trip::TripItemListRow::build(
|
let item_row = view::trip::TripItemListRow::build(
|
||||||
trip_id,
|
trip_id,
|
||||||
&item,
|
&item,
|
||||||
models::inventory::Item::get_category_max_weight(
|
models::inventory::InventoryItem::get_category_max_weight(
|
||||||
&state.database_pool,
|
&state.database_pool,
|
||||||
item.item.category_id,
|
item.item.category_id,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ pub struct Product {
|
|||||||
pub comment: Option<String>,
|
pub comment: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct inventoryItem {
|
pub struct InventoryItem {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
@@ -300,6 +300,7 @@ impl InventoryItem {
|
|||||||
|
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_category_max_weight(
|
pub async fn get_category_max_weight(
|
||||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||||
category_id: Uuid,
|
category_id: Uuid,
|
||||||
@@ -329,84 +330,64 @@ impl InventoryItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[derive(Debug)]
|
#[derive(Debug)]
|
||||||
// pub struct Item {
|
pub struct Item {
|
||||||
// pub id: Uuid,
|
pub id: Uuid,
|
||||||
// pub name: String,
|
pub name: String,
|
||||||
// pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
// pub weight: i64,
|
pub weight: i64,
|
||||||
// pub category_id: Uuid,
|
pub category_id: Uuid,
|
||||||
// }
|
}
|
||||||
|
|
||||||
// pub struct DbInventoryItemsRow {
|
pub struct DbInventoryItemsRow {
|
||||||
// pub id: String,
|
pub id: String,
|
||||||
// pub name: String,
|
pub name: String,
|
||||||
// pub weight: i64,
|
pub weight: i64,
|
||||||
// pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
// pub category_id: String,
|
pub category_id: String,
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl TryFrom<DbInventoryItemsRow> for Item {
|
impl TryFrom<DbInventoryItemsRow> for Item {
|
||||||
// type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
// fn try_from(row: DbInventoryItemsRow) -> Result<Self, Self::Error> {
|
fn try_from(row: DbInventoryItemsRow) -> Result<Self, Self::Error> {
|
||||||
// Ok(Item {
|
Ok(Item {
|
||||||
// id: Uuid::try_parse(&row.id)?,
|
id: Uuid::try_parse(&row.id)?,
|
||||||
// name: row.name,
|
name: row.name,
|
||||||
// description: row.description, // TODO
|
description: row.description, // TODO
|
||||||
// weight: row.weight,
|
weight: row.weight,
|
||||||
// category_id: Uuid::try_parse(&row.category_id)?,
|
category_id: Uuid::try_parse(&row.category_id)?,
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl Item {
|
impl Item {
|
||||||
// pub async fn find(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<Option<Item>, Error> {
|
pub async fn _get_category_total_picked_weight(
|
||||||
// let id_param = id.to_string();
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||||
// sqlx::query_as!(
|
category_id: Uuid,
|
||||||
// DbInventoryItemsRow,
|
) -> Result<i64, Error> {
|
||||||
// "SELECT
|
let category_id_param = category_id.to_string();
|
||||||
// id,
|
Ok(sqlx::query!(
|
||||||
// name,
|
"
|
||||||
// weight,
|
SELECT COALESCE(SUM(i_item.weight), 0) as weight
|
||||||
// description,
|
FROM inventory_items_categories as category
|
||||||
// category_id
|
INNER JOIN inventory_items as i_item
|
||||||
// FROM inventory_items AS item
|
ON i_item.category_id = category.id
|
||||||
// WHERE item.id = ?",
|
INNER JOIN trips_items as t_item
|
||||||
// id_param,
|
ON i_item.id = t_item.item_id
|
||||||
// )
|
WHERE category_id = ?
|
||||||
// .fetch_optional(pool)
|
AND t_item.pick = 1
|
||||||
// .await?
|
",
|
||||||
// .map(|row| row.try_into())
|
category_id_param
|
||||||
// .transpose()
|
)
|
||||||
// }
|
.fetch_one(pool)
|
||||||
|
.map_ok(|row| {
|
||||||
// pub async fn _get_category_total_picked_weight(
|
// convert to i64 because that the default integer type, but looks
|
||||||
// pool: &sqlx::Pool<sqlx::Sqlite>,
|
// like COALESCE return i32?
|
||||||
// category_id: Uuid,
|
//
|
||||||
// ) -> Result<i64, Error> {
|
// We can be certain that the row exists, as we COALESCE it
|
||||||
// let category_id_param = category_id.to_string();
|
row.weight.unwrap() as i64
|
||||||
// Ok(sqlx::query!(
|
})
|
||||||
// "
|
.await?)
|
||||||
// SELECT COALESCE(SUM(i_item.weight), 0) as weight
|
}
|
||||||
// FROM inventory_items_categories as category
|
}
|
||||||
// INNER JOIN inventory_items as i_item
|
|
||||||
// ON i_item.category_id = category.id
|
|
||||||
// INNER JOIN trips_items as t_item
|
|
||||||
// ON i_item.id = t_item.item_id
|
|
||||||
// WHERE category_id = ?
|
|
||||||
// AND t_item.pick = 1
|
|
||||||
// ",
|
|
||||||
// category_id_param
|
|
||||||
// )
|
|
||||||
// .fetch_one(pool)
|
|
||||||
// .map_ok(|row| {
|
|
||||||
// // convert to i64 because that the default integer type, but looks
|
|
||||||
// // like COALESCE return i32?
|
|
||||||
// //
|
|
||||||
// // We can be certain that the row exists, as we COALESCE it
|
|
||||||
// row.weight.unwrap() as i64
|
|
||||||
// })
|
|
||||||
// .await?)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
138
rust/src/out
Normal file
138
rust/src/out
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
pub struct InventoryItem {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub weight: i64,
|
||||||
|
pub category: Category,
|
||||||
|
pub product: Option<Product>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DbInventoryItemRow {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub weight: i64,
|
||||||
|
pub category_id: String,
|
||||||
|
pub category_name: String,
|
||||||
|
pub category_description: Option<String>,
|
||||||
|
pub product_id: Option<String>,
|
||||||
|
pub product_name: Option<String>,
|
||||||
|
pub product_description: Option<String>,
|
||||||
|
pub product_comment: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<DbInventoryItemRow> for InventoryItem {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(row: DbInventoryItemRow) -> Result<Self, Self::Error> {
|
||||||
|
Ok(InventoryItem {
|
||||||
|
id: Uuid::try_parse(&row.id)?,
|
||||||
|
name: row.name,
|
||||||
|
description: row.description,
|
||||||
|
weight: row.weight,
|
||||||
|
category: Category {
|
||||||
|
id: Uuid::try_parse(&row.category_id)?,
|
||||||
|
name: row.category_name,
|
||||||
|
description: row.category_description,
|
||||||
|
items: None,
|
||||||
|
},
|
||||||
|
product: row
|
||||||
|
.product_id
|
||||||
|
.map(|id| -> Result<Product, Error> {
|
||||||
|
Ok(Product {
|
||||||
|
id: Uuid::try_parse(&id)?,
|
||||||
|
name: row.product_name.unwrap(),
|
||||||
|
description: row.product_description,
|
||||||
|
comment: row.product_comment,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.transpose()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InventoryItem {
|
||||||
|
pub async fn find(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<Option<Self>, Error> {
|
||||||
|
let id_param = id.to_string();
|
||||||
|
|
||||||
|
sqlx::query_as!(
|
||||||
|
DbInventoryItemRow,
|
||||||
|
"SELECT
|
||||||
|
item.id AS id,
|
||||||
|
item.name AS name,
|
||||||
|
item.description AS description,
|
||||||
|
weight,
|
||||||
|
category.id AS category_id,
|
||||||
|
category.name AS category_name,
|
||||||
|
category.description AS category_description,
|
||||||
|
product.id AS product_id,
|
||||||
|
product.name AS product_name,
|
||||||
|
product.description AS product_description,
|
||||||
|
product.comment AS product_comment
|
||||||
|
FROM inventory_items AS item
|
||||||
|
INNER JOIN inventory_items_categories as category
|
||||||
|
ON item.category_id = category.id
|
||||||
|
LEFT JOIN inventory_products AS product
|
||||||
|
ON item.product_id = product.id
|
||||||
|
WHERE item.id = ?",
|
||||||
|
id_param,
|
||||||
|
)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
.map(|row| row.try_into())
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn name_exists(pool: &sqlx::Pool<sqlx::Sqlite>, name: &str) -> Result<bool, Error> {
|
||||||
|
Ok(sqlx::query!(
|
||||||
|
"SELECT id
|
||||||
|
FROM inventory_items
|
||||||
|
WHERE name = ?",
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
.map(|_row| ())
|
||||||
|
.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<bool, Error> {
|
||||||
|
let id_param = id.to_string();
|
||||||
|
let results = sqlx::query!(
|
||||||
|
"DELETE FROM inventory_items
|
||||||
|
WHERE id = ?",
|
||||||
|
id_param
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(results.rows_affected() != 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn save(
|
||||||
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||||
|
name: &str,
|
||||||
|
category_id: Uuid,
|
||||||
|
weight: u32,
|
||||||
|
) -> Result<Uuid, Error> {
|
||||||
|
let id = Uuid::new_v4();
|
||||||
|
let id_param = id.to_string();
|
||||||
|
let category_id_param = category_id.to_string();
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO inventory_items
|
||||||
|
(id, name, description, weight, category_id)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?, ?, ?)",
|
||||||
|
id_param,
|
||||||
|
name,
|
||||||
|
"",
|
||||||
|
weight,
|
||||||
|
category_id_param
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user