diff --git a/examples/actix3_example/core/src/mutation.rs b/examples/actix3_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/actix3_example/core/src/mutation.rs +++ b/examples/actix3_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/actix_example/core/src/mutation.rs b/examples/actix_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/actix_example/core/src/mutation.rs +++ b/examples/actix_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/axum_example/core/src/mutation.rs b/examples/axum_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/axum_example/core/src/mutation.rs +++ b/examples/axum_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/graphql_example/core/src/mutation.rs b/examples/graphql_example/core/src/mutation.rs index 6eac6814a..1f2447fba 100644 --- a/examples/graphql_example/core/src/mutation.rs +++ b/examples/graphql_example/core/src/mutation.rs @@ -23,11 +23,11 @@ impl Mutation { id: i32, form_data: note::Model, ) -> Result { - let note: note::ActiveModel = if let Ok(Some(note)) = Note::find_by_id(id).one(db).await { - note.into() - } else { - return Err(DbErr::Custom("Cannot find note.".to_owned())); - }; + let note: note::ActiveModel = Note::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find note.".to_owned())) + .map(Into::into)?; note::ActiveModel { id: note.id, @@ -39,11 +39,11 @@ impl Mutation { } pub async fn delete_note(db: &DbConn, id: i32) -> Result { - let note: note::ActiveModel = if let Ok(Some(note)) = Note::find_by_id(id).one(db).await { - note.into() - } else { - return Err(DbErr::Custom("Cannot find note.".to_owned())); - }; + let note: note::ActiveModel = Note::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find note.".to_owned())) + .map(Into::into)?; note.delete(db).await } diff --git a/examples/jsonrpsee_example/api/src/lib.rs b/examples/jsonrpsee_example/api/src/lib.rs index 2e5f2c723..f98cc1832 100644 --- a/examples/jsonrpsee_example/api/src/lib.rs +++ b/examples/jsonrpsee_example/api/src/lib.rs @@ -60,7 +60,7 @@ impl PostRpcServer for PpcImpl { async fn insert(&self, p: post::Model) -> RpcResult { let new_post = Mutation::create_post(&self.conn, p) .await - .expect("could not insert post"); + .internal_call_error()?; Ok(new_post.id.unwrap()) } diff --git a/examples/jsonrpsee_example/core/src/mutation.rs b/examples/jsonrpsee_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/jsonrpsee_example/core/src/mutation.rs +++ b/examples/jsonrpsee_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/poem_example/core/src/mutation.rs b/examples/poem_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/poem_example/core/src/mutation.rs +++ b/examples/poem_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/rocket_example/api/Rocket.toml b/examples/rocket_example/Rocket.toml similarity index 91% rename from examples/rocket_example/api/Rocket.toml rename to examples/rocket_example/Rocket.toml index fc294bd2f..41e0183c9 100644 --- a/examples/rocket_example/api/Rocket.toml +++ b/examples/rocket_example/Rocket.toml @@ -1,5 +1,5 @@ [default] -template_dir = "templates/" +template_dir = "api/templates/" [default.databases.sea_orm] # Mysql diff --git a/examples/rocket_example/api/Cargo.toml b/examples/rocket_example/api/Cargo.toml index e0d718d8e..1a88308fc 100644 --- a/examples/rocket_example/api/Cargo.toml +++ b/examples/rocket_example/api/Cargo.toml @@ -20,6 +20,7 @@ rocket_dyn_templates = { version = "0.1.0-rc.1", features = [ serde_json = { version = "^1" } entity = { path = "../entity" } migration = { path = "../migration" } +tokio = "1.20.0" [dependencies.sea-orm-rocket] path = "../../../sea-orm-rocket/lib" # remove this line in your own project and use the git line diff --git a/examples/rocket_example/api/src/lib.rs b/examples/rocket_example/api/src/lib.rs index f283b1015..51918c7a3 100644 --- a/examples/rocket_example/api/src/lib.rs +++ b/examples/rocket_example/api/src/lib.rs @@ -1,7 +1,6 @@ #[macro_use] extern crate rocket; -use futures::executor::block_on; use rocket::fairing::{self, AdHoc}; use rocket::form::{Context, Form}; use rocket::fs::{relative, FileServer}; @@ -144,7 +143,8 @@ async fn run_migrations(rocket: Rocket) -> fairing::Result { Ok(rocket) } -fn rocket() -> Rocket { +#[tokio::main] +async fn start() -> Result<(), rocket::Error> { rocket::build() .attach(Db::init()) .attach(AdHoc::try_on_ignite("Migrations", run_migrations)) @@ -155,10 +155,13 @@ fn rocket() -> Rocket { ) .register("/", catchers![not_found]) .attach(Template::fairing()) + .launch() + .await + .map(|_| ()) } pub fn main() { - let result = block_on(rocket().launch()); + let result = start(); println!("Rocket: deorbit."); diff --git a/examples/rocket_example/core/src/mutation.rs b/examples/rocket_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/rocket_example/core/src/mutation.rs +++ b/examples/rocket_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/salvo_example/core/src/mutation.rs b/examples/salvo_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/salvo_example/core/src/mutation.rs +++ b/examples/salvo_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await } diff --git a/examples/tonic_example/core/src/mutation.rs b/examples/tonic_example/core/src/mutation.rs index 7f0150a63..dd6891d4a 100644 --- a/examples/tonic_example/core/src/mutation.rs +++ b/examples/tonic_example/core/src/mutation.rs @@ -22,11 +22,11 @@ impl Mutation { id: i32, form_data: post::Model, ) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post::ActiveModel { id: post.id, @@ -38,11 +38,11 @@ impl Mutation { } pub async fn delete_post(db: &DbConn, id: i32) -> Result { - let post: post::ActiveModel = if let Ok(Some(post)) = Post::find_by_id(id).one(db).await { - post.into() - } else { - return Err(DbErr::Custom("Cannot find post.".to_owned())); - }; + let post: post::ActiveModel = Post::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find post.".to_owned())) + .map(Into::into)?; post.delete(db).await }