From 41d25225e2847652b0fb4a1a6f5b3409952e75ae Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Fri, 27 Aug 2021 22:20:46 +1000 Subject: [PATCH] Create a post #[post("/", data = "")] --- examples/rocket_example/src/main.rs | 2 - examples/rocket_example/src/sqlx/mod.rs | 61 ++++++++++------------- examples/rocket_example/src/sqlx/post.rs | 2 +- examples/rocket_example/src/sqlx/setup.rs | 50 +++++++++---------- 4 files changed, 51 insertions(+), 64 deletions(-) diff --git a/examples/rocket_example/src/main.rs b/examples/rocket_example/src/main.rs index f22b2ca1..774f58a6 100644 --- a/examples/rocket_example/src/main.rs +++ b/examples/rocket_example/src/main.rs @@ -1,7 +1,5 @@ #[macro_use] extern crate rocket; -#[macro_use] -extern crate rocket_db_pools; mod sqlx; diff --git a/examples/rocket_example/src/sqlx/mod.rs b/examples/rocket_example/src/sqlx/mod.rs index 0d2fd30e..2909ed9d 100644 --- a/examples/rocket_example/src/sqlx/mod.rs +++ b/examples/rocket_example/src/sqlx/mod.rs @@ -1,15 +1,12 @@ -use rocket::fairing::{self, AdHoc}; +use rocket::fairing::AdHoc; use rocket::response::status::Created; -use rocket::serde::{json::Json, Deserialize, Serialize}; +use rocket::serde::json::Json; use rocket::{futures, Build, Rocket}; use rocket_db_pools::{sqlx, Connection, Database}; -use futures::{future::TryFutureExt, stream::TryStreamExt}; use sea_orm::entity::*; -use sea_orm::{ - DatabaseBackend, QueryFilter, SqlxSqliteConnector, SqlxSqlitePoolConnection, Statement, -}; +use sea_orm::{DatabaseBackend, Statement}; mod setup; @@ -19,38 +16,30 @@ struct Db(sea_orm::Database); type Result> = std::result::Result; -// use post::*; mod post; pub use post::Entity as Post; -use sea_orm::DatabaseConnection; -// #[derive(Debug, Clone, Deserialize, Serialize)] -// #[serde(crate = "rocket::serde")] -// struct Post { -// #[serde(skip_deserializing, skip_serializing_if = "Option::is_none")] -// id: Option, -// title: String, -// text: String, -// } +#[post("/", data = "")] +async fn create( + conn: Connection, + post: Json, +) -> Result>> { + let _post = post::ActiveModel { + title: Set(post.title.to_owned()), + text: Set(post.text.to_owned()), + ..Default::default() + } + .save(&conn) + .await + .expect("could not insert post"); -// #[post("/", data = "")] -// async fn create(mut db: Connection, post: Json) -> Result>> { -// // There is no support for `RETURNING`. -// sqlx::query!( -// "INSERT INTO posts (title, text) VALUES (?, ?)", -// post.title, -// post.text -// ) -// .execute(&mut *db) -// .await?; - -// Ok(Created::new("/").body(post)) -// } + Ok(Created::new("/").body(post)) +} #[get("/")] -async fn list(mut con: Connection) -> Result>> { +async fn list(conn: Connection) -> Result>> { let ids = Post::find() - .all(&con) + .all(&conn) .await .expect("could not retrieve posts") .into_iter() @@ -61,9 +50,9 @@ async fn list(mut con: Connection) -> Result>> { } #[get("/")] -async fn read(mut con: Connection, id: i64) -> Option> { +async fn read(conn: Connection, id: i64) -> Option> { let post: Option = Post::find_by_id(id) - .one(&con) + .one(&conn) .await .expect("could not find post"); @@ -74,7 +63,7 @@ async fn read(mut con: Connection, id: i64) -> Option> { } #[delete("/")] -async fn delete(mut conn: Connection, id: i64) -> Result> { +async fn delete(conn: Connection, id: i64) -> Result> { let post: post::ActiveModel = Post::find_by_id(id) .one(&conn) .await @@ -87,7 +76,7 @@ async fn delete(mut conn: Connection, id: i64) -> Result> { } #[delete("/")] -async fn destroy(mut conn: Connection) -> Result<()> { +async fn destroy(conn: Connection) -> Result<()> { let _result = Post::delete_many().exec(&conn).await.unwrap(); Ok(()) } @@ -206,7 +195,7 @@ pub fn stage() -> AdHoc { // None => Err(rocket), // } })) - .mount("/sqlx", routes![list, read, delete, destroy]) + .mount("/sqlx", routes![create, delete, destroy, list, read,]) }) } diff --git a/examples/rocket_example/src/sqlx/post.rs b/examples/rocket_example/src/sqlx/post.rs index 58caf669..05ec80fe 100644 --- a/examples/rocket_example/src/sqlx/post.rs +++ b/examples/rocket_example/src/sqlx/post.rs @@ -1,4 +1,4 @@ -use rocket::serde::{json::Json, Deserialize, Serialize}; +use rocket::serde::{Deserialize, Serialize}; use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity, Deserialize, Serialize)] diff --git a/examples/rocket_example/src/sqlx/setup.rs b/examples/rocket_example/src/sqlx/setup.rs index fac405be..2421d84f 100644 --- a/examples/rocket_example/src/sqlx/setup.rs +++ b/examples/rocket_example/src/sqlx/setup.rs @@ -1,37 +1,37 @@ use sea_orm::{error::*, sea_query, DbConn, ExecResult}; -use sea_orm::sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, TableCreateStatement}; +use sea_orm::sea_query::{ColumnDef, TableCreateStatement}; // mod post; pub use super::post::*; async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result { - let builder = db.get_database_backend(); - db.execute(builder.build(stmt)).await + let builder = db.get_database_backend(); + db.execute(builder.build(stmt)).await } pub async fn create_post_table(db: &DbConn) -> Result { - let stmt = sea_query::Table::create() - .table(super::post::Entity) - .if_not_exists() - .col( - ColumnDef::new(super::post::Column::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(super::post::Column::Title) - .string() - .not_null(), - ) - .col( - ColumnDef::new(super::post::Column::Text) - .string() - .not_null(), - ) - .to_owned(); + let stmt = sea_query::Table::create() + .table(super::post::Entity) + .if_not_exists() + .col( + ColumnDef::new(super::post::Column::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(super::post::Column::Title) + .string() + .not_null(), + ) + .col( + ColumnDef::new(super::post::Column::Text) + .string() + .not_null(), + ) + .to_owned(); - create_table(db, &stmt).await + create_table(db, &stmt).await }