-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
63 lines (58 loc) · 1.98 KB
/
client.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
use crate::{order::Order, postgres, Bakery};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, OnceLock};
use vantage::prelude::*;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
pub struct Client {
pub id: i64,
pub name: String,
pub contact_details: String,
pub bakery_id: i64,
}
impl Entity for Client {}
impl Client {
pub fn static_table() -> &'static Table<Postgres, Client> {
static TABLE: OnceLock<Table<Postgres, Client>> = OnceLock::new();
TABLE.get_or_init(|| {
Table::new_with_entity("client", postgres())
.with_id_column("id")
.with_column("name")
.with_title_column("email")
.with_column("contact_details")
.with_column("is_paying_client")
.with_column("bakery_id")
.with_one("bakery", "bakery_id", || Box::new(Bakery::table()))
.with_many("orders", "client_id", || Box::new(Order::table()))
})
}
pub fn table() -> Table<Postgres, Client> {
Client::static_table().clone()
}
}
pub trait ClientTable: SqlTable {
fn name(&self) -> Arc<PgValueColumn> {
self.get_column("name").unwrap()
}
fn email(&self) -> Arc<PgValueColumn> {
self.get_column("email").unwrap()
}
fn contact_details(&self) -> Arc<PgValueColumn> {
self.get_column("contact_details").unwrap()
}
fn bakery_id(&self) -> Arc<PgValueColumn> {
self.get_column("bakery_id").unwrap()
}
fn is_paying_client(&self) -> Arc<PgValueColumn> {
self.get_column("is_paying_client").unwrap()
}
fn ref_bakery(&self) -> Table<Postgres, Bakery>;
fn ref_orders(&self) -> Table<Postgres, Order>;
}
impl ClientTable for Table<Postgres, Client> {
fn ref_bakery(&self) -> Table<Postgres, Bakery> {
self.get_ref_as("bakery").unwrap()
}
fn ref_orders(&self) -> Table<Postgres, Order> {
self.get_ref_as("orders").unwrap()
}
}