cscart_rs/service/
methods.rs

1use crate::prelude::*;
2use serde_json::Value;
3use std::future::Future;
4
5pub trait CreateResource {
6    type Response;
7    fn create(
8        &self,
9        data: serde_json::Value,
10    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
11}
12
13pub trait GetAllResource {
14    type Response;
15    fn get_all(
16        &self,
17        options: GetAllOptions,
18    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
19}
20
21pub trait GetResourceById {
22    type Response;
23    fn get_by_id<T: Into<String>>(
24        &self,
25        id: T,
26    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
27}
28
29pub trait UpdateResource {
30    type Response;
31    fn update_by_id<T: Into<String>>(
32        &self,
33        id: T,
34        data: Value,
35    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
36}
37
38pub trait DeleteResource {
39    type Response;
40    fn delete_by_id<T: Into<String>>(
41        &self,
42        id: T,
43    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
44}
45
46pub trait GetAllSubResource {
47    type Response;
48    fn get_all_sub_entity<T: Into<String>>(
49        &self,
50        id: T,
51        entity: T,
52    ) -> impl Future<Output = anyhow::Result<Self::Response>>;
53}
54
55#[macro_export]
56macro_rules! impl_create_method {
57    ($service:ident, $return_type:ident) => {
58        impl CreateResource for $service {
59            type Response = $return_type;
60            async fn create(&self, data: serde_json::Value) -> anyhow::Result<Self::Response> {
61                let handler = $crate::handler::Handler::new()
62                    .host(&self.config.host())
63                    .username(self.config.auth().username())
64                    .api_key(self.config.auth().api_key())
65                    .path(self.config.resource.as_ref().unwrap().path())
66                    .build();
67
68                let response_value = handler.create(data).await?;
69                let response: $return_type = serde_json::from_value(response_value)?;
70                Ok(response)
71            }
72        }
73    };
74}
75
76#[macro_export]
77macro_rules! impl_get_by_id_method {
78    ($service:ident, $return_type:ident) => {
79        impl GetResourceById for $service {
80            type Response = $return_type;
81            async fn get_by_id<T: Into<String>>(&self, id: T) -> anyhow::Result<Self::Response> {
82                let handler = $crate::handler::Handler::new()
83                    .host(&self.config.host())
84                    .username(self.config.auth().username())
85                    .api_key(self.config.auth().api_key())
86                    .path(format!(
87                        "{}/{}",
88                        self.config.resource.as_ref().unwrap().path(),
89                        id.into()
90                    ))
91                    .build();
92
93                let response_value = handler.read().await?;
94                let response: $return_type = serde_json::from_value(response_value)?;
95                Ok(response)
96            }
97        }
98    };
99}
100
101#[macro_export]
102macro_rules! impl_get_all_method {
103    ($service:ident, $return_type:ident) => {
104        impl GetAllResource for $service {
105            type Response = $return_type;
106            async fn get_all(&self, options: GetAllOptions) -> anyhow::Result<Self::Response> {
107                let handler = $crate::handler::Handler::new()
108                    .host(&self.config.host())
109                    .username(self.config.auth().username())
110                    .api_key(self.config.auth().api_key())
111                    .path(self.config.resource.as_ref().unwrap().path())
112                    .set_query_params(options.params())
113                    .build();
114
115                let response_value = handler.read().await?;
116                let response: $return_type = serde_json::from_value(response_value)?;
117                Ok(response)
118            }
119        }
120    };
121}
122
123#[macro_export]
124macro_rules! impl_update_by_id_method {
125    ($service:ident, $return_type:ident) => {
126        impl UpdateResource for $service {
127            type Response = $return_type;
128            async fn update_by_id<T: Into<String>>(
129                &self,
130                id: T,
131                data: Value,
132            ) -> anyhow::Result<Self::Response> {
133                let handler = Handler::new()
134                    .host(&self.config.host())
135                    .username(self.config.auth().username())
136                    .api_key(self.config.auth().api_key())
137                    .path(format!(
138                        "{}/{}",
139                        self.config.resource.as_ref().unwrap().path(),
140                        id.into()
141                    ))
142                    .build();
143
144                let response_value = handler.update(data).await?;
145                let response: $return_type = serde_json::from_value(response_value)?;
146                Ok(response)
147            }
148        }
149    };
150}
151
152#[macro_export]
153macro_rules! impl_delete_by_id_method {
154    ($service:ident, $return_type:ident) => {
155        use $crate::handler::Handler;
156        impl DeleteResource for $service {
157            type Response = $return_type;
158            async fn delete_by_id<T: Into<String>>(&self, id: T) -> anyhow::Result<Self::Response> {
159                let handler = Handler::new()
160                    .host(&self.config.host())
161                    .username(self.config.auth().username())
162                    .api_key(self.config.auth().api_key())
163                    .path(format!(
164                        "{}/{}",
165                        self.config.resource.as_ref().unwrap().path(),
166                        id.into()
167                    ))
168                    .build();
169
170                let response_value = handler.delete().await?;
171                let response: $return_type = serde_json::from_value(response_value)?;
172                Ok(response)
173            }
174        }
175    };
176}
177
178#[macro_export]
179macro_rules! impl_get_all_entity_method {
180    ($service:ident, $return_type:ident) => {
181        impl GetAllSubResource for $service {
182            type Response = $return_type;
183            async fn get_all_sub_entity<T: Into<String>>(
184                &self,
185                id: T,
186                entity: T,
187            ) -> anyhow::Result<Self::Response> {
188                let handler = $crate::handler::Handler::new()
189                    .host(&self.config.host())
190                    .username(self.config.auth().username())
191                    .api_key(self.config.auth().api_key())
192                    .path(format!(
193                        "{}/{}/{}",
194                        self.config.resource.as_ref().unwrap().path(),
195                        id.into(),
196                        entity.into()
197                    ))
198                    .build();
199
200                let response_value = handler.read().await?;
201                let response: $return_type = serde_json::from_value(response_value)?;
202                Ok(response)
203            }
204        }
205    };
206}