1 //! Functions to be called from actix code. They are asynchonrous and won't block worker thread caller.
5 use actix_web
::{error
::BlockingError
, web
};
6 use chrono
::{prelude
::*, Duration
};
12 pub enum DBAsyncError
{
14 ActixError(BlockingError
),
18 impl fmt
::Display
for DBAsyncError
{
19 fn fmt(&self, f
: &mut fmt
::Formatter
) -> std
::result
::Result
<(), fmt
::Error
> {
20 write!(f
, "{:?}", self)
24 impl std
::error
::Error
for DBAsyncError {}
26 impl From
<DBError
> for DBAsyncError
{
27 fn from(error
: DBError
) -> Self {
28 DBAsyncError
::DBError(error
)
32 impl From
<BlockingError
> for DBAsyncError
{
33 fn from(error
: BlockingError
) -> Self {
34 DBAsyncError
::ActixError(error
)
39 fn from_dyn_error(error
: Box
<dyn std
::error
::Error
>) -> Self {
40 DBAsyncError
::Other(error
.to_string())
45 error
: std
::result
::Result
<std
::result
::Result
<T
, DBAsyncError
>, BlockingError
>,
50 type Result
<T
> = std
::result
::Result
<T
, DBAsyncError
>;
53 pub async
fn get_all_recipe_titles_async(&self) -> Result
<Vec
<(i64, String
)>> {
54 let self_copy
= self.clone();
55 web
::block(move || self_copy
.get_all_recipe_titles().unwrap_or_default())
57 .map_err(DBAsyncError
::from
)
60 pub async
fn get_recipe_async(&self, id
: i64) -> Result
<model
::Recipe
> {
61 let self_copy
= self.clone();
63 web
::block(move || self_copy
.get_recipe(id
).map_err(DBAsyncError
::from
)).await
,
67 pub async
fn load_user_async(&self, user_id
: i64) -> Result
<model
::User
> {
68 let self_copy
= self.clone();
70 web
::block(move || self_copy
.load_user(user_id
).map_err(DBAsyncError
::from
)).await
,
74 pub async
fn sign_up_async(&self, email
: &str, password
: &str) -> Result
<SignUpResult
> {
75 let self_copy
= self.clone();
76 let email_copy
= email
.to_string();
77 let password_copy
= password
.to_string();
81 .sign_up(&email_copy
, &password_copy
)
82 .map_err(DBAsyncError
::from
)
88 pub async
fn validation_async(
91 validation_time
: Duration
,
94 ) -> Result
<ValidationResult
> {
95 let self_copy
= self.clone();
96 let token_copy
= token
.to_string();
97 let ip_copy
= ip
.to_string();
98 let user_agent_copy
= user_agent
.to_string();
102 .validation(&token_copy
, validation_time
, &ip_copy
, &user_agent_copy
)
103 .map_err(DBAsyncError
::from
)
109 pub async
fn sign_in_async(
115 ) -> Result
<SignInResult
> {
116 let self_copy
= self.clone();
117 let email_copy
= email
.to_string();
118 let password_copy
= password
.to_string();
119 let ip_copy
= ip
.to_string();
120 let user_agent_copy
= user_agent
.to_string();
124 .sign_in(&email_copy
, &password_copy
, &ip_copy
, &user_agent_copy
)
125 .map_err(DBAsyncError
::from
)
131 pub async
fn authentication_async(
136 ) -> Result
<AuthenticationResult
> {
137 let self_copy
= self.clone();
138 let token_copy
= token
.to_string();
139 let ip_copy
= ip
.to_string();
140 let user_agent_copy
= user_agent
.to_string();
144 .authentication(&token_copy
, &ip_copy
, &user_agent_copy
)
145 .map_err(DBAsyncError
::from
)
151 pub async
fn sign_out_async(&self, token
: &str) -> Result
<()> {
152 let self_copy
= self.clone();
153 let token_copy
= token
.to_string();
155 web
::block(move || self_copy
.sign_out(&token_copy
).map_err(DBAsyncError
::from
)).await
,
159 pub async
fn create_recipe_async(&self, user_id
: i64) -> Result
<i64> {
160 let self_copy
= self.clone();
162 web
::block(move || self_copy
.create_recipe(user_id
).map_err(DBAsyncError
::from
)).await
,
166 pub async
fn set_recipe_title_async(&self, recipe_id
: i64, title
: &str) -> Result
<()> {
167 let self_copy
= self.clone();
168 let title_copy
= title
.to_string();
172 .set_recipe_title(recipe_id
, &title_copy
)
173 .map_err(DBAsyncError
::from
)
179 pub async
fn set_recipe_description_async(
184 let self_copy
= self.clone();
185 let description_copy
= description
.to_string();
189 .set_recipe_description(recipe_id
, &description_copy
)
190 .map_err(DBAsyncError
::from
)