use stateset_core::{
Company, CompanyFilter, CompanyId, CompanyPriceOverride, CompanyShippingAddress, Contact,
ContactId, CreateCompany, CreateContact, Result, UpdateCompany,
};
use stateset_db::{Database, DatabaseCapability};
use std::sync::Arc;
pub struct Companies {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for Companies {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Companies").finish_non_exhaustive()
}
}
impl Companies {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
#[must_use]
pub fn is_supported(&self) -> bool {
self.db.supports_capability(DatabaseCapability::Companies)
}
fn ensure(&self) -> Result<()> {
self.db.ensure_capability(DatabaseCapability::Companies)
}
pub fn create(&self, input: CreateCompany) -> Result<Company> {
self.ensure()?;
self.db.companies().create(input)
}
pub fn get(&self, id: CompanyId) -> Result<Option<Company>> {
self.ensure()?;
self.db.companies().get(id)
}
pub fn update(&self, id: CompanyId, input: UpdateCompany) -> Result<Company> {
self.ensure()?;
self.db.companies().update(id, input)
}
pub fn list(&self, filter: CompanyFilter) -> Result<Vec<Company>> {
self.ensure()?;
self.db.companies().list(filter)
}
pub fn delete(&self, id: CompanyId) -> Result<()> {
self.ensure()?;
self.db.companies().delete(id)
}
pub fn list_addresses(&self, id: CompanyId) -> Result<Vec<CompanyShippingAddress>> {
self.ensure()?;
self.db.companies().list_addresses(id)
}
pub fn list_price_overrides(&self, id: CompanyId) -> Result<Vec<CompanyPriceOverride>> {
self.ensure()?;
self.db.companies().list_price_overrides(id)
}
pub fn create_contact(&self, input: CreateContact) -> Result<Contact> {
self.ensure()?;
self.db.companies().create_contact(input)
}
pub fn get_contact(&self, id: ContactId) -> Result<Option<Contact>> {
self.ensure()?;
self.db.companies().get_contact(id)
}
pub fn list_contacts(&self, company_id: CompanyId) -> Result<Vec<Contact>> {
self.ensure()?;
self.db.companies().list_contacts(company_id)
}
}