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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Product review operations for managing customer reviews and ratings
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::{Commerce, CreateReview, ProductId, CustomerId};
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! let review = commerce.reviews().create(CreateReview {
//! product_id: ProductId::new(),
//! customer_id: CustomerId::new(),
//! rating: 5,
//! title: Some("Excellent product!".into()),
//! body: Some("Works exactly as described.".into()),
//! ..Default::default()
//! })?;
//!
//! println!("Review created with rating: {}", review.rating);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use stateset_core::{
CreateReview, ProductId, Result, Review, ReviewFilter, ReviewId, ReviewSummary, UpdateReview,
};
use stateset_db::Database;
use std::sync::Arc;
/// Product review operations.
pub struct Reviews {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for Reviews {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Reviews").finish_non_exhaustive()
}
}
impl Reviews {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
/// Create a new product review.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateReview, ProductId, CustomerId};
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let review = commerce.reviews().create(CreateReview {
/// product_id: ProductId::new(),
/// customer_id: CustomerId::new(),
/// rating: 4,
/// title: Some("Good value".into()),
/// body: Some("Decent quality for the price.".into()),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create(&self, input: CreateReview) -> Result<Review> {
self.db.reviews().create(input)
}
/// Get a review by ID.
pub fn get(&self, id: ReviewId) -> Result<Option<Review>> {
self.db.reviews().get(id)
}
/// Update a review.
pub fn update(&self, id: ReviewId, input: UpdateReview) -> Result<Review> {
self.db.reviews().update(id, input)
}
/// List reviews with optional filtering.
pub fn list(&self, filter: ReviewFilter) -> Result<Vec<Review>> {
self.db.reviews().list(filter)
}
/// Delete a review.
pub fn delete(&self, id: ReviewId) -> Result<()> {
self.db.reviews().delete(id)
}
/// Get aggregate review summary for a product.
///
/// Returns average rating, total count, and rating distribution.
pub fn get_summary(&self, product_id: ProductId) -> Result<ReviewSummary> {
self.db.reviews().get_summary(product_id)
}
/// Mark a review as helpful (increment helpful count).
pub fn mark_helpful(&self, id: ReviewId) -> Result<()> {
self.db.reviews().mark_helpful(id)
}
/// Mark a review as reported (increment reported count).
pub fn mark_reported(&self, id: ReviewId) -> Result<()> {
self.db.reviews().mark_reported(id)
}
}