stateset_embedded/
companies.rs1use stateset_core::{
4 Company, CompanyFilter, CompanyId, CompanyPriceOverride, CompanyShippingAddress, Contact,
5 ContactId, CreateCompany, CreateContact, Result, UpdateCompany,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10pub struct Companies {
12 db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Companies {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 f.debug_struct("Companies").finish_non_exhaustive()
18 }
19}
20
21impl Companies {
22 pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23 Self { db }
24 }
25
26 #[must_use]
28 pub fn is_supported(&self) -> bool {
29 self.db.supports_capability(DatabaseCapability::Companies)
30 }
31
32 fn ensure(&self) -> Result<()> {
33 self.db.ensure_capability(DatabaseCapability::Companies)
34 }
35
36 pub fn create(&self, input: CreateCompany) -> Result<Company> {
38 self.ensure()?;
39 self.db.companies().create(input)
40 }
41
42 pub fn get(&self, id: CompanyId) -> Result<Option<Company>> {
44 self.ensure()?;
45 self.db.companies().get(id)
46 }
47
48 pub fn update(&self, id: CompanyId, input: UpdateCompany) -> Result<Company> {
50 self.ensure()?;
51 self.db.companies().update(id, input)
52 }
53
54 pub fn list(&self, filter: CompanyFilter) -> Result<Vec<Company>> {
56 self.ensure()?;
57 self.db.companies().list(filter)
58 }
59
60 pub fn delete(&self, id: CompanyId) -> Result<()> {
62 self.ensure()?;
63 self.db.companies().delete(id)
64 }
65
66 pub fn list_addresses(&self, id: CompanyId) -> Result<Vec<CompanyShippingAddress>> {
68 self.ensure()?;
69 self.db.companies().list_addresses(id)
70 }
71
72 pub fn list_price_overrides(&self, id: CompanyId) -> Result<Vec<CompanyPriceOverride>> {
74 self.ensure()?;
75 self.db.companies().list_price_overrides(id)
76 }
77
78 pub fn create_contact(&self, input: CreateContact) -> Result<Contact> {
80 self.ensure()?;
81 self.db.companies().create_contact(input)
82 }
83
84 pub fn get_contact(&self, id: ContactId) -> Result<Option<Contact>> {
86 self.ensure()?;
87 self.db.companies().get_contact(id)
88 }
89
90 pub fn list_contacts(&self, company_id: CompanyId) -> Result<Vec<Contact>> {
92 self.ensure()?;
93 self.db.companies().list_contacts(company_id)
94 }
95}