Skip to main content

stateset_embedded/
wishlists.rs

1//! Wishlist operations for managing customer wishlists
2//!
3//! # Example
4//!
5//! ```rust,ignore
6//! use stateset_embedded::{Commerce, CreateWishlist, CustomerId};
7//!
8//! let commerce = Commerce::new("./store.db")?;
9//!
10//! let wishlist = commerce.wishlists().create(CreateWishlist {
11//!     customer_id: CustomerId::new(),
12//!     name: Some("Birthday Wishes".into()),
13//!     ..Default::default()
14//! })?;
15//!
16//! println!("Wishlist created: {:?}", wishlist.name);
17//! # Ok::<(), stateset_embedded::CommerceError>(())
18//! ```
19
20use stateset_core::{
21    AddWishlistItem, CreateWishlist, ProductId, Result, UpdateWishlist, Wishlist, WishlistFilter,
22    WishlistId, WishlistItem,
23};
24use stateset_db::{Database, DatabaseCapability};
25use std::sync::Arc;
26
27/// Wishlist operations for managing customer wishlists.
28pub struct Wishlists {
29    db: Arc<dyn Database>,
30}
31
32impl std::fmt::Debug for Wishlists {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        f.debug_struct("Wishlists").finish_non_exhaustive()
35    }
36}
37
38impl Wishlists {
39    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
40        Self { db }
41    }
42
43    /// Whether wishlists are supported by the active backend.
44    #[must_use]
45    pub fn is_supported(&self) -> bool {
46        self.db.supports_capability(DatabaseCapability::Wishlists)
47    }
48
49    fn ensure_supported(&self) -> Result<()> {
50        self.db.ensure_capability(DatabaseCapability::Wishlists)
51    }
52
53    /// Create a new wishlist.
54    ///
55    /// # Example
56    ///
57    /// ```rust,ignore
58    /// use stateset_embedded::{Commerce, CreateWishlist, CustomerId};
59    ///
60    /// let commerce = Commerce::new("./store.db")?;
61    ///
62    /// let wishlist = commerce.wishlists().create(CreateWishlist {
63    ///     customer_id: CustomerId::new(),
64    ///     name: Some("Holiday Gift Ideas".into()),
65    ///     ..Default::default()
66    /// })?;
67    /// # Ok::<(), stateset_embedded::CommerceError>(())
68    /// ```
69    pub fn create(&self, input: CreateWishlist) -> Result<Wishlist> {
70        self.ensure_supported()?;
71        self.db.wishlists().create(input)
72    }
73
74    /// Get a wishlist by ID.
75    pub fn get(&self, id: WishlistId) -> Result<Option<Wishlist>> {
76        self.ensure_supported()?;
77        self.db.wishlists().get(id)
78    }
79
80    /// Update wishlist metadata.
81    pub fn update(&self, id: WishlistId, input: UpdateWishlist) -> Result<Wishlist> {
82        self.ensure_supported()?;
83        self.db.wishlists().update(id, input)
84    }
85
86    /// List wishlists with optional filtering.
87    pub fn list(&self, filter: WishlistFilter) -> Result<Vec<Wishlist>> {
88        self.ensure_supported()?;
89        self.db.wishlists().list(filter)
90    }
91
92    /// Delete a wishlist and all its items.
93    pub fn delete(&self, id: WishlistId) -> Result<()> {
94        self.ensure_supported()?;
95        self.db.wishlists().delete(id)
96    }
97
98    /// Add an item to a wishlist.
99    pub fn add_item(&self, wishlist_id: WishlistId, item: AddWishlistItem) -> Result<WishlistItem> {
100        self.ensure_supported()?;
101        self.db.wishlists().add_item(wishlist_id, item)
102    }
103
104    /// Remove an item from a wishlist by product ID.
105    pub fn remove_item(&self, wishlist_id: WishlistId, product_id: ProductId) -> Result<()> {
106        self.ensure_supported()?;
107        self.db.wishlists().remove_item(wishlist_id, product_id)
108    }
109}