1 //! Functions to be called from actix code. They are asynchonrous and won't block worker thread caller.
5 use chrono
::{prelude
::*, Duration
};
6 use actix_web
::{web
, error
::BlockingError
};
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())
45 fn combine_errors
<T
>(error
: std
::result
::Result
<std
::result
::Result
<T
, DBAsyncError
>, BlockingError
>) -> Result
<T
> {
49 type Result
<T
> = std
::result
::Result
<T
, DBAsyncError
>;
52 pub async
fn get_all_recipe_titles_async(&self) -> Result
<Vec
<(i64, String
)>> {
53 let self_copy
= self.clone();
54 web
::block(move || { self_copy
.get_all_recipe_titles().unwrap_or_default() }).await
.map_err(DBAsyncError
::from
)
57 pub async
fn get_recipe_async(&self, id
: i64) -> Result
<model
::Recipe
> {
58 let self_copy
= self.clone();
59 combine_errors(web
::block(move || { self_copy
.get_recipe(id
).map_err(DBAsyncError
::from
) }).await
)
62 pub async
fn load_user_async(&self, user_id
: i64) -> Result
<User
> {
63 let self_copy
= self.clone();
64 combine_errors(web
::block(move || { self_copy
.load_user(user_id
).map_err(DBAsyncError
::from
) }).await
)
67 pub async
fn sign_up_async(&self, email
: &str, password
: &str) -> Result
<SignUpResult
> {
68 let self_copy
= self.clone();
69 let email_copy
= email
.to_string();
70 let password_copy
= password
.to_string();
71 combine_errors(web
::block(move || { self_copy
.sign_up(&email_copy
, &password_copy
).map_err(DBAsyncError
::from
) }).await
)
74 pub async
fn validation_async(&self, token
: &str, validation_time
: Duration
, ip
: &str, user_agent
: &str) -> Result
<ValidationResult
> {
75 let self_copy
= self.clone();
76 let token_copy
= token
.to_string();
77 let ip_copy
= ip
.to_string();
78 let user_agent_copy
= user_agent
.to_string();
79 combine_errors(web
::block(move || { self_copy
.validation(&token_copy
, validation_time
, &ip_copy
, &user_agent_copy
).map_err(DBAsyncError
::from
) }).await
)
82 pub async
fn sign_in_async(&self, email
: &str, password
: &str, ip
: &str, user_agent
: &str) -> Result
<SignInResult
> {
83 let self_copy
= self.clone();
84 let email_copy
= email
.to_string();
85 let password_copy
= password
.to_string();
86 let ip_copy
= ip
.to_string();
87 let user_agent_copy
= user_agent
.to_string();
88 combine_errors(web
::block(move || { self_copy
.sign_in(&email_copy
, &password_copy
, &ip_copy
, &user_agent_copy
).map_err(DBAsyncError
::from
) }).await
)
91 pub async
fn authentication_async(&self, token
: &str, ip
: &str, user_agent
: &str) -> Result
<AuthenticationResult
> {
92 let self_copy
= self.clone();
93 let token_copy
= token
.to_string();
94 let ip_copy
= ip
.to_string();
95 let user_agent_copy
= user_agent
.to_string();
96 combine_errors(web
::block(move || { self_copy
.authentication(&token_copy
, &ip_copy
, &user_agent_copy
).map_err(DBAsyncError
::from
) }).await
)
99 pub async
fn sign_out_async(&self, token
: &str) -> Result
<()> {
100 let self_copy
= self.clone();
101 let token_copy
= token
.to_string();
102 combine_errors(web
::block(move || { self_copy
.sign_out(&token_copy
).map_err(DBAsyncError
::from
) }).await
)
105 pub async
fn create_recipe_async(&self, user_id
: i64) -> Result
<i64> {
106 let self_copy
= self.clone();
107 combine_errors(web
::block(move || { self_copy
.create_recipe(user_id
).map_err(DBAsyncError
::from
) }).await
)
110 pub async
fn set_recipe_title_async(&self, recipe_id
: i64, title
: &str) -> Result
<()> {
111 let self_copy
= self.clone();
112 let title_copy
= title
.to_string();
113 combine_errors(web
::block(move || { self_copy
.set_recipe_title(recipe_id
, &title_copy
).map_err(DBAsyncError
::from
) }).await
)