-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineitem.rs
65 lines (58 loc) · 2.09 KB
/
lineitem.rs
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
use std::sync::{Arc, OnceLock};
use serde::{Deserialize, Serialize};
use vantage::prelude::*;
use crate::{order::Order, postgres, Product, ProductTable};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct LineItem {
pub id: i64,
pub price: i64,
pub quantity: i64,
pub order_id: i64,
}
impl Entity for LineItem {}
impl LineItem {
pub fn static_table() -> &'static Table<Postgres, LineItem> {
static TABLE: OnceLock<Table<Postgres, LineItem>> = OnceLock::new();
TABLE.get_or_init(|| {
Table::new_with_entity("order_line", postgres())
.with_column("quantity")
.with_column("order_id")
.with_column("product_id")
.with_expression("total", |t: &Table<Postgres, LineItem>| {
t.price().render_chunk().mul(t.quantity())
})
.with_expression("price", |t| {
let product = t.get_subquery_as::<Product>("product").unwrap();
product.field_query(product.price()).render_chunk()
})
.with_one("order", "order_id", || Box::new(Order::table()))
.with_one("product", "product_id", || Box::new(Product::table()))
})
}
pub fn table() -> Table<Postgres, LineItem> {
LineItem::static_table().clone()
}
}
pub trait LineItemTable: AnyTable {
fn as_table(&self) -> &Table<Postgres, LineItem> {
self.as_any_ref().downcast_ref().unwrap()
}
fn quantity(&self) -> Arc<PgValueColumn> {
self.get_column("quantity").unwrap()
}
fn order_id(&self) -> Arc<PgValueColumn> {
self.get_column("order_id").unwrap()
}
fn product_id(&self) -> Arc<PgValueColumn> {
self.get_column("product_id").unwrap()
}
fn total(&self) -> Box<dyn SqlField> {
self.as_table().search_for_field("total").unwrap()
}
fn price(&self) -> Box<dyn SqlField>;
}
impl LineItemTable for Table<Postgres, LineItem> {
fn price(&self) -> Box<dyn SqlField> {
self.search_for_field("price").unwrap()
}
}