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
};
10 use crate::user
::User
;
13 pub enum DBAsyncError
{
15 ActixError(BlockingError
),
19 impl fmt
::Display
for DBAsyncError
{
20 fn fmt(&self, f
: &mut fmt
::Formatter
) -> std
::result
::Result
<(), fmt
::Error
> {
21 write!(f
, "{:?}", self)
25 impl std
::error
::Error
for DBAsyncError {}
27 impl From
<DBError
> for DBAsyncError
{
28 fn from(error
: DBError
) -> Self {
29 DBAsyncError
::DBError(error
)
33 impl From
<BlockingError
> for DBAsyncError
{
34 fn from(error
: BlockingError
) -> Self {
35 DBAsyncError
::ActixError(error
)
40 fn from_dyn_error(error
: Box
<dyn std
::error
::Error
>) -> Self {
41 DBAsyncError
::Other(error
.to_string())
46 error
: std
::result
::Result
<std
::result
::Result
<T
, DBAsyncError
>, BlockingError
>,
51 type Result
<T
> = std
::result
::Result
<T
, DBAsyncError
>;
54 pub async
fn get_all_recipe_titles_async(&self) -> Result
<Vec
<(i64, String
)>> {
55 let self_copy
= self.clone();
56 web
::block(move || self_copy
.get_all_recipe_titles().unwrap_or_default())
58 .map_err(DBAsyncError
::from
)
61 pub async
fn get_recipe_async(&self, id
: i64) -> Result
<model
::Recipe
> {
62 let self_copy
= self.clone();
64 web
::block(move || self_copy
.get_recipe(id
).map_err(DBAsyncError
::from
)).await
,
68 pub async
fn load_user_async(&self, user_id
: i64) -> Result
<User
> {
69 let self_copy
= self.clone();
71 web
::block(move || self_copy
.load_user(user_id
).map_err(DBAsyncError
::from
)).await
,
75 pub async
fn sign_up_async(&self, email
: &str, password
: &str) -> Result
<SignUpResult
> {
76 let self_copy
= self.clone();
77 let email_copy
= email
.to_string();
78 let password_copy
= password
.to_string();
82 .sign_up(&email_copy
, &password_copy
)
83 .map_err(DBAsyncError
::from
)
89 pub async
fn validation_async(
92 validation_time
: Duration
,
95 ) -> Result
<ValidationResult
> {
96 let self_copy
= self.clone();
97 let token_copy
= token
.to_string();
98 let ip_copy
= ip
.to_string();
99 let user_agent_copy
= user_agent
.to_string();
103 .validation(&token_copy
, validation_time
, &ip_copy
, &user_agent_copy
)
104 .map_err(DBAsyncError
::from
)
110 pub async
fn sign_in_async(
116 ) -> Result
<SignInResult
> {
117 let self_copy
= self.clone();
118 let email_copy
= email
.to_string();
119 let password_copy
= password
.to_string();
120 let ip_copy
= ip
.to_string();
121 let user_agent_copy
= user_agent
.to_string();
125 .sign_in(&email_copy
, &password_copy
, &ip_copy
, &user_agent_copy
)
126 .map_err(DBAsyncError
::from
)
132 pub async
fn authentication_async(
137 ) -> Result
<AuthenticationResult
> {
138 let self_copy
= self.clone();
139 let token_copy
= token
.to_string();
140 let ip_copy
= ip
.to_string();
141 let user_agent_copy
= user_agent
.to_string();
145 .authentication(&token_copy
, &ip_copy
, &user_agent_copy
)
146 .map_err(DBAsyncError
::from
)
152 pub async
fn sign_out_async(&self, token
: &str) -> Result
<()> {
153 let self_copy
= self.clone();
154 let token_copy
= token
.to_string();
156 web
::block(move || self_copy
.sign_out(&token_copy
).map_err(DBAsyncError
::from
)).await
,
160 pub async
fn create_recipe_async(&self, user_id
: i64) -> Result
<i64> {
161 let self_copy
= self.clone();
163 web
::block(move || self_copy
.create_recipe(user_id
).map_err(DBAsyncError
::from
)).await
,
167 pub async
fn set_recipe_title_async(&self, recipe_id
: i64, title
: &str) -> Result
<()> {
168 let self_copy
= self.clone();
169 let title_copy
= title
.to_string();
173 .set_recipe_title(recipe_id
, &title_copy
)
174 .map_err(DBAsyncError
::from
)
180 pub async
fn set_recipe_description_async(
185 let self_copy
= self.clone();
186 let description_copy
= description
.to_string();
190 .set_recipe_description(recipe_id
, &description_copy
)
191 .map_err(DBAsyncError
::from
)