-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbakery.rs
53 lines (48 loc) · 1.62 KB
/
bakery.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
use crate::{client::*, postgres, product::*};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, OnceLock};
use vantage::prelude::*;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct Bakery {
pub name: String,
pub profit_margin: i64,
}
impl Entity for Bakery {}
impl Bakery {
pub fn static_table() -> &'static Table<Postgres, Bakery> {
static TABLE: OnceLock<Table<Postgres, Bakery>> = OnceLock::new();
TABLE.get_or_init(|| {
Table::new_with_entity("bakery", postgres())
.with_id_column("id")
.with_column("name")
.with_column("profit_margin")
.with_many("clients", "bakery_id", || Box::new(Client::table()))
.with_many("products", "bakery_id", || Box::new(Product::table()))
})
}
pub fn table() -> Table<Postgres, Bakery> {
Bakery::static_table().clone()
}
}
pub trait BakeryTable: AnyTable {
// fields
fn id(&self) -> Arc<PgValueColumn> {
self.get_column("id").unwrap()
}
fn name(&self) -> Arc<PgValueColumn> {
self.get_column("name").unwrap()
}
fn profit_margin(&self) -> Arc<PgValueColumn> {
self.get_column("profit_margin").unwrap()
}
fn ref_clients(&self) -> Table<Postgres, Client>;
fn ref_products(&self) -> Table<Postgres, Product>;
}
impl BakeryTable for Table<Postgres, Bakery> {
fn ref_clients(&self) -> Table<Postgres, Client> {
self.get_ref_as("clients").unwrap()
}
fn ref_products(&self) -> Table<Postgres, Product> {
self.get_ref_as("products").unwrap()
}
}