1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Repository for StockEntryItem entities
//!
//! Originally generated by metaphor-schema; now **user-owned** — this exact path is declared under
//! `user_owned` in `metaphor.codegen.yaml`, so the generator skips it wholesale. The custom methods
//! below hold the hand-written StockEntryItem SQL
//! (4-layer rule: services orchestrate, repositories hold the SQL).
//!
//! Thin newtype over `backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>`.
//! All standard CRUD methods are available via `Deref`.
use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::PgPool;
use uuid::Uuid;
use crate::domain::entity::StockEntryItem;
/// Table name for StockEntryItem entities
pub const TABLE_NAME: &str = "inventory.stock_entry_items";
/// Repository for StockEntryItem entities.
///
/// All standard CRUD, soft-delete, pagination, and bulk methods are
/// provided automatically via `Deref` to `backbone_orm::GenericCrudRepository`.
pub struct StockEntryItemRepository(
backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for StockEntryItemRepository {
type Target = backbone_orm::GenericCrudRepository<StockEntryItem, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl StockEntryItemRepository {
/// Create a new repository instance.
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
/// The exact row a transfer line insert writes.
pub struct NewStockEntryItemRow {
pub id: Uuid,
pub entry_id: Uuid,
pub item_id: Uuid,
pub quantity: Decimal,
}
/// Hand-written StockEntryItem SQL. Lives here per the module's 4-layer rule.
impl StockEntryItemRepository {
/// Insert one transfer line. Takes the CALLER'S connection so it commits with the paired
/// out/in movement; the caller has already relayed the ambient org scope onto it — don't
/// re-bind here.
pub async fn insert_item(
&self,
conn: &mut sqlx::PgConnection,
l: &NewStockEntryItemRow,
) -> Result<(), sqlx::Error> {
sqlx::query("INSERT INTO inventory.stock_entry_items (id, entry_id, item_id, quantity) VALUES ($1,$2,$3,$4)")
.bind(l.id).bind(l.entry_id).bind(l.item_id).bind(l.quantity)
.execute(conn)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(StockEntryItemRepository, StockEntryItem, soft_delete);