actor_sample/model/
order.rs

1/// Represents a customer order.
2///
3/// # Actor Framework
4/// This struct implements the [`ActorEntity`](actor_framework::ActorEntity) trait,
5/// allowing it to be managed by a [`ResourceActor`](actor_framework::ResourceActor).
6///
7/// See [`impl ActorEntity for Order`](#impl-ActorEntity-for-Order) for details on:
8/// - Creation parameters ([`OrderCreate`])
9use crate::model::{ProductId, UserId};
10use serde::{Deserialize, Serialize};
11use std::fmt::Display;
12
13/// Type-safe identifier for Orders.
14#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
15pub struct OrderId(pub u32);
16
17impl From<u32> for OrderId {
18    fn from(id: u32) -> Self {
19        Self(id)
20    }
21}
22
23impl Display for OrderId {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        write!(f, "order_{}", self.0)
26    }
27}
28
29#[derive(Debug, Clone)]
30pub struct Order {
31    #[allow(dead_code)]
32    pub id: OrderId,
33    pub user_id: UserId,
34    pub product_id: ProductId,
35    pub quantity: u32,
36    pub total: f64,
37    #[allow(dead_code)]
38    pub status: String,
39}
40
41/// Payload for creating a new order.
42#[derive(Debug, Clone)]
43pub struct OrderCreate {
44    pub user_id: UserId,
45    pub product_id: ProductId,
46    pub quantity: u32,
47    pub total: f64,
48}
49
50impl Order {
51    /// Creates a new Order instance.
52    ///
53    /// # Arguments
54    /// * `id` - Unique identifier (typically set by the actor system)
55    /// * `user_id` - ID of the user placing the order
56    /// * `product_id` - ID of the product being ordered
57    /// * `quantity` - Quantity ordered
58    /// * `total` - Total price for the order
59    ///
60    /// # Notes
61    /// The order is initialized with status "Created".
62    /// This constructor is kept for backward compatibility.
63    pub fn new(
64        id: OrderId,
65        user_id: UserId,
66        product_id: ProductId,
67        quantity: u32,
68        total: f64,
69    ) -> Self {
70        Self {
71            id,
72            user_id,
73            product_id,
74            quantity,
75            total,
76            status: "Created".to_string(),
77        }
78    }
79}