actor_sample/order_actor/
entity.rs1use crate::clients::{ProductClient, UserClient};
9use crate::model::{Order, OrderCreate, OrderId};
10use crate::order_actor::OrderError;
11use actor_framework::{ActorClient, ActorEntity};
12use async_trait::async_trait;
13
14#[doc(hidden)]
16#[allow(dead_code)]
18pub const ENTITY_IMPL_PRESENT: bool = true;
19
20#[async_trait]
21impl ActorEntity for Order {
22 type Id = OrderId;
23 type Create = OrderCreate;
24 type Update = (); type Action = (); type ActionResult = ();
27 type Context = (UserClient, ProductClient);
28 type Error = OrderError;
29
30 fn from_create_params(id: Self::Id, params: Self::Create) -> Result<Self, Self::Error> {
34 Ok(Self::new(
35 id,
36 params.user_id,
37 params.product_id,
38 params.quantity,
39 params.total,
40 ))
41 }
42
43 async fn on_create(
45 &mut self,
46 (user_client, product_client): &Self::Context,
47 ) -> Result<(), Self::Error> {
48 let user = user_client.get(self.user_id.clone()).await?;
50
51 if user.is_none() {
52 return Err(OrderError::InvalidUser(self.user_id.to_string()));
53 }
54
55 product_client
57 .reserve_stock(self.product_id.clone(), self.quantity)
58 .await?;
59
60 Ok(())
61 }
62
63 async fn handle_action(
64 &mut self,
65 _action: Self::Action,
66 _ctx: &Self::Context,
67 ) -> Result<Self::ActionResult, Self::Error> {
68 Ok(())
69 }
70
71 async fn on_update(
72 &mut self,
73 _update: Self::Update,
74 _ctx: &Self::Context,
75 ) -> Result<(), Self::Error> {
76 Ok(())
77 }
78
79 async fn on_delete(&self, _ctx: &Self::Context) -> Result<(), Self::Error> {
80 Ok(())
81 }
82}