X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fdata%2Fasynchronous.rs;h=7ef77b8c2bc853b199f2fa199f68e22bd532f84e;hb=0a1631e66c861de2799cd98fc93686ff121c9fce;hp=186dad629c33f3c265b3fcd853299075b5e3d8ee;hpb=cbe276fc0601041b13087a6ffd80c5b126dfbe59;p=recipes.git diff --git a/backend/src/data/asynchronous.rs b/backend/src/data/asynchronous.rs index 186dad6..7ef77b8 100644 --- a/backend/src/data/asynchronous.rs +++ b/backend/src/data/asynchronous.rs @@ -2,8 +2,8 @@ use std::fmt; +use actix_web::{error::BlockingError, web}; use chrono::{prelude::*, Duration}; -use actix_web::{web, error::BlockingError}; use super::db::*; use crate::model; @@ -22,15 +22,15 @@ impl fmt::Display for DBAsyncError { } } -impl std::error::Error for DBAsyncError { } +impl std::error::Error for DBAsyncError {} -impl From for DBAsyncError { +impl From for DBAsyncError { fn from(error: DBError) -> Self { DBAsyncError::DBError(error) } } -impl From for DBAsyncError { +impl From for DBAsyncError { fn from(error: BlockingError) -> Self { DBAsyncError::ActixError(error) } @@ -42,7 +42,9 @@ impl DBAsyncError { } } -fn combine_errors(error: std::result::Result, BlockingError>) -> Result { +fn combine_errors( + error: std::result::Result, BlockingError>, +) -> Result { error? } @@ -51,71 +53,144 @@ type Result = std::result::Result; impl Connection { pub async fn get_all_recipe_titles_async(&self) -> Result> { let self_copy = self.clone(); - web::block(move || { self_copy.get_all_recipe_titles().unwrap_or_default() }).await.map_err(DBAsyncError::from) + web::block(move || self_copy.get_all_recipe_titles().unwrap_or_default()) + .await + .map_err(DBAsyncError::from) } pub async fn get_recipe_async(&self, id: i64) -> Result { let self_copy = self.clone(); - combine_errors(web::block(move || { self_copy.get_recipe(id).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || self_copy.get_recipe(id).map_err(DBAsyncError::from)).await, + ) } pub async fn load_user_async(&self, user_id: i64) -> Result { let self_copy = self.clone(); - combine_errors(web::block(move || { self_copy.load_user(user_id).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || self_copy.load_user(user_id).map_err(DBAsyncError::from)).await, + ) } pub async fn sign_up_async(&self, email: &str, password: &str) -> Result { let self_copy = self.clone(); let email_copy = email.to_string(); let password_copy = password.to_string(); - combine_errors(web::block(move || { self_copy.sign_up(&email_copy, &password_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .sign_up(&email_copy, &password_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } - pub async fn validation_async(&self, token: &str, validation_time: Duration, ip: &str, user_agent: &str) -> Result { + pub async fn validation_async( + &self, + token: &str, + validation_time: Duration, + ip: &str, + user_agent: &str, + ) -> Result { let self_copy = self.clone(); let token_copy = token.to_string(); let ip_copy = ip.to_string(); let user_agent_copy = user_agent.to_string(); - combine_errors(web::block(move || { self_copy.validation(&token_copy, validation_time, &ip_copy, &user_agent_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .validation(&token_copy, validation_time, &ip_copy, &user_agent_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } - pub async fn sign_in_async(&self, email: &str, password: &str, ip: &str, user_agent: &str) -> Result { + pub async fn sign_in_async( + &self, + email: &str, + password: &str, + ip: &str, + user_agent: &str, + ) -> Result { let self_copy = self.clone(); let email_copy = email.to_string(); let password_copy = password.to_string(); let ip_copy = ip.to_string(); let user_agent_copy = user_agent.to_string(); - combine_errors(web::block(move || { self_copy.sign_in(&email_copy, &password_copy, &ip_copy, &user_agent_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .sign_in(&email_copy, &password_copy, &ip_copy, &user_agent_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } - pub async fn authentication_async(&self, token: &str, ip: &str, user_agent: &str) -> Result { + pub async fn authentication_async( + &self, + token: &str, + ip: &str, + user_agent: &str, + ) -> Result { let self_copy = self.clone(); let token_copy = token.to_string(); let ip_copy = ip.to_string(); let user_agent_copy = user_agent.to_string(); - combine_errors(web::block(move || { self_copy.authentication(&token_copy, &ip_copy, &user_agent_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .authentication(&token_copy, &ip_copy, &user_agent_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } pub async fn sign_out_async(&self, token: &str) -> Result<()> { let self_copy = self.clone(); let token_copy = token.to_string(); - combine_errors(web::block(move || { self_copy.sign_out(&token_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || self_copy.sign_out(&token_copy).map_err(DBAsyncError::from)).await, + ) } pub async fn create_recipe_async(&self, user_id: i64) -> Result { let self_copy = self.clone(); - combine_errors(web::block(move || { self_copy.create_recipe(user_id).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || self_copy.create_recipe(user_id).map_err(DBAsyncError::from)).await, + ) } pub async fn set_recipe_title_async(&self, recipe_id: i64, title: &str) -> Result<()> { let self_copy = self.clone(); let title_copy = title.to_string(); - combine_errors(web::block(move || { self_copy.set_recipe_title(recipe_id, &title_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .set_recipe_title(recipe_id, &title_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } - pub async fn set_recipe_description_async(&self, recipe_id: i64, description: &str) -> Result<()> { + pub async fn set_recipe_description_async( + &self, + recipe_id: i64, + description: &str, + ) -> Result<()> { let self_copy = self.clone(); let description_copy = description.to_string(); - combine_errors(web::block(move || { self_copy.set_recipe_description(recipe_id, &description_copy).map_err(DBAsyncError::from) }).await) + combine_errors( + web::block(move || { + self_copy + .set_recipe_description(recipe_id, &description_copy) + .map_err(DBAsyncError::from) + }) + .await, + ) } -} \ No newline at end of file +}