stateset_embedded/
channels.rs1use stateset_core::{
4 Channel, ChannelFilter, ChannelId, ChannelProductMapping, ChannelProductSyncItem,
5 CreateChannel, Result, UpdateChannel,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10pub struct Channels {
12 db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Channels {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 f.debug_struct("Channels").finish_non_exhaustive()
18 }
19}
20
21impl Channels {
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::Channels)
30 }
31
32 fn ensure(&self) -> Result<()> {
33 self.db.ensure_capability(DatabaseCapability::Channels)
34 }
35
36 pub fn create(&self, input: CreateChannel) -> Result<Channel> {
38 self.ensure()?;
39 self.db.channels().create(input)
40 }
41
42 pub fn get(&self, id: ChannelId) -> Result<Option<Channel>> {
44 self.ensure()?;
45 self.db.channels().get(id)
46 }
47
48 pub fn update(&self, id: ChannelId, input: UpdateChannel) -> Result<Channel> {
50 self.ensure()?;
51 self.db.channels().update(id, input)
52 }
53
54 pub fn list(&self, filter: ChannelFilter) -> Result<Vec<Channel>> {
56 self.ensure()?;
57 self.db.channels().list(filter)
58 }
59
60 pub fn delete(&self, id: ChannelId) -> Result<()> {
62 self.ensure()?;
63 self.db.channels().delete(id)
64 }
65
66 pub fn set_lock(&self, id: ChannelId, locked: bool) -> Result<Channel> {
68 self.ensure()?;
69 self.db.channels().set_lock(id, locked)
70 }
71
72 pub fn sync_products(&self, id: ChannelId, items: Vec<ChannelProductSyncItem>) -> Result<u64> {
74 self.ensure()?;
75 self.db.channels().sync_products(id, items)
76 }
77
78 pub fn list_product_mappings(&self, id: ChannelId) -> Result<Vec<ChannelProductMapping>> {
80 self.ensure()?;
81 self.db.channels().list_product_mappings(id)
82 }
83}