From c47dfbb1f900b7183f116c191ce7e4557a74ea19 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda <34771614+starvy@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:00:49 +0200 Subject: [PATCH] =?UTF-8?q?Refactoring=20aby=20to=20=C5=A1lo=20spustit=20(?= =?UTF-8?q?#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * db: candidate entity * chore: candidate migrace * chore: import candidate migrace * fix: default value pro citizenship * fix: unique pro evideční číslo přihlášky * chore: parent migrace * fix: unique pro evideční číslo přihlášky * chore: sample fill admin table * fix: chybějící serde pro admin entitu * chore: remove mock tests * remove all post entity imports Co-authored-by: EETagent --- api/src/lib.rs | 51 +++++----------------------- core/Cargo.toml | 4 --- core/src/mutation.rs | 50 +++------------------------ core/src/query.rs | 22 ++---------- core/tests/mock.rs | 79 ------------------------------------------- core/tests/prepare.rs | 50 --------------------------- entity/src/admin.rs | 4 ++- 7 files changed, 20 insertions(+), 240 deletions(-) delete mode 100644 core/tests/mock.rs delete mode 100644 core/tests/prepare.rs diff --git a/api/src/lib.rs b/api/src/lib.rs index f595676..df560b8 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate rocket; +use rocket::serde::json::Json; use rocket::fairing::{self, AdHoc}; use rocket::form::{ Form}; @@ -14,60 +15,26 @@ use sea_orm_rocket::{Connection, Database}; mod pool; use pool::Db; -pub use entity::post; -pub use entity::post::Entity as Post; +pub use entity::candidate; +pub use entity::candidate::Entity as Candidate; #[post("/", data = "")] -async fn create(conn: Connection<'_, Db>, post_form: Form) -> Flash { +async fn create(conn: Connection<'_, Db>, post_form: Json) -> Flash { let db = conn.into_inner(); let form = post_form.into_inner(); - Mutation::create_post(db, form) + Mutation::create_candidate(db, form) .await .expect("could not insert post"); Flash::success(Redirect::to("/"), "Post successfully added.") } -#[post("/", data = "")] -async fn update( - conn: Connection<'_, Db>, - id: i32, - post_form: Form, -) -> Flash { - let db = conn.into_inner(); - - let form = post_form.into_inner(); - - Mutation::update_post_by_id(db, id, form) - .await - .expect("could not update post"); - - Flash::success(Redirect::to("/"), "Post successfully edited.") -} - -#[delete("/")] -async fn delete(conn: Connection<'_, Db>, id: i32) -> Flash { - let db = conn.into_inner(); - - Mutation::delete_post(db, id) - .await - .expect("could not delete post"); - - Flash::success(Redirect::to("/"), "Post successfully deleted.") -} - -#[delete("/")] -async fn destroy(conn: Connection<'_, Db>) -> Result<(), rocket::response::Debug> { - let db = conn.into_inner(); - - Mutation::delete_all_posts(db) - .await - .map_err(|e| e.to_string())?; - - Ok(()) +#[get("/hello")] +async fn hello() -> &'static str { + "Hello, world!" } async fn run_migrations(rocket: Rocket) -> fairing::Result { @@ -82,7 +49,7 @@ async fn start() -> Result<(), rocket::Error> { .attach(Db::init()) .attach(AdHoc::try_on_ignite("Migrations", run_migrations)) //.mount("/", FileServer::from(relative!("/static"))) - .mount("/", routes![create, delete, destroy, update]) + .mount("/", routes![create, hello]) .register("/", catchers![]) .launch() .await diff --git a/core/Cargo.toml b/core/Cargo.toml index 0fcb32c..3793774 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,7 +19,3 @@ tokio = "1.21.2" [features] mock = ["sea-orm/mock"] - -[[test]] -name = "mock" -required-features = ["mock"] diff --git a/core/src/mutation.rs b/core/src/mutation.rs index dd6891d..f3f9e61 100644 --- a/core/src/mutation.rs +++ b/core/src/mutation.rs @@ -1,53 +1,13 @@ -use ::entity::{post, post::Entity as Post}; +use ::entity::{candidate, candidate::Entity as Candidate}; use sea_orm::*; pub struct Mutation; impl Mutation { - pub async fn create_post( + pub async fn create_candidate( db: &DbConn, - form_data: post::Model, - ) -> Result { - post::ActiveModel { - title: Set(form_data.title.to_owned()), - text: Set(form_data.text.to_owned()), - ..Default::default() - } - .save(db) - .await - } - - pub async fn update_post_by_id( - db: &DbConn, - id: i32, - form_data: post::Model, - ) -> Result { - 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, - title: Set(form_data.title.to_owned()), - text: Set(form_data.text.to_owned()), - } - .update(db) - .await - } - - pub async fn delete_post(db: &DbConn, id: i32) -> Result { - 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 - } - - pub async fn delete_all_posts(db: &DbConn) -> Result { - Post::delete_many().exec(db).await + form_data: candidate::Model, + ) -> Result { + todo!() } } diff --git a/core/src/query.rs b/core/src/query.rs index e8d2668..72aa09f 100644 --- a/core/src/query.rs +++ b/core/src/query.rs @@ -1,26 +1,10 @@ -use ::entity::{post, post::Entity as Post}; +use ::entity::{candidate, candidate::Entity as Candidate}; use sea_orm::*; pub struct Query; impl Query { - pub async fn find_post_by_id(db: &DbConn, id: i32) -> Result, DbErr> { - Post::find_by_id(id).one(db).await - } - - /// If ok, returns (post models, num pages). - pub async fn find_posts_in_page( - db: &DbConn, - page: u64, - posts_per_page: u64, - ) -> Result<(Vec, u64), DbErr> { - // Setup paginator - let paginator = Post::find() - .order_by_asc(post::Column::Id) - .paginate(db, posts_per_page); - let num_pages = paginator.num_pages().await?; - - // Fetch paginated posts - paginator.fetch_page(page - 1).await.map(|p| (p, num_pages)) + pub async fn find_candidate_by_id(db: &DbConn, id: i32) -> Result, DbErr> { + Candidate::find_by_id(id).one(db).await } } diff --git a/core/tests/mock.rs b/core/tests/mock.rs deleted file mode 100644 index 84b187e..0000000 --- a/core/tests/mock.rs +++ /dev/null @@ -1,79 +0,0 @@ -mod prepare; - -use entity::post; -use prepare::prepare_mock_db; -use rocket_example_core::{Mutation, Query}; - -#[tokio::test] -async fn main() { - let db = &prepare_mock_db(); - - { - let post = Query::find_post_by_id(db, 1).await.unwrap().unwrap(); - - assert_eq!(post.id, 1); - } - - { - let post = Query::find_post_by_id(db, 5).await.unwrap().unwrap(); - - assert_eq!(post.id, 5); - } - - { - let post = Mutation::create_post( - db, - post::Model { - id: 0, - title: "Title D".to_owned(), - text: "Text D".to_owned(), - }, - ) - .await - .unwrap(); - - assert_eq!( - post, - post::ActiveModel { - id: sea_orm::ActiveValue::Unchanged(6), - title: sea_orm::ActiveValue::Unchanged("Title D".to_owned()), - text: sea_orm::ActiveValue::Unchanged("Text D".to_owned()) - } - ); - } - - { - let post = Mutation::update_post_by_id( - db, - 1, - post::Model { - id: 1, - title: "New Title A".to_owned(), - text: "New Text A".to_owned(), - }, - ) - .await - .unwrap(); - - assert_eq!( - post, - post::Model { - id: 1, - title: "New Title A".to_owned(), - text: "New Text A".to_owned(), - } - ); - } - - { - let result = Mutation::delete_post(db, 5).await.unwrap(); - - assert_eq!(result.rows_affected, 1); - } - - { - let result = Mutation::delete_all_posts(db).await.unwrap(); - - assert_eq!(result.rows_affected, 5); - } -} diff --git a/core/tests/prepare.rs b/core/tests/prepare.rs deleted file mode 100644 index 4518049..0000000 --- a/core/tests/prepare.rs +++ /dev/null @@ -1,50 +0,0 @@ -use ::entity::post; -use sea_orm::*; - -#[cfg(feature = "mock")] -pub fn prepare_mock_db() -> DatabaseConnection { - MockDatabase::new(DatabaseBackend::Postgres) - .append_query_results(vec![ - vec![post::Model { - id: 1, - title: "Title A".to_owned(), - text: "Text A".to_owned(), - }], - vec![post::Model { - id: 5, - title: "Title C".to_owned(), - text: "Text C".to_owned(), - }], - vec![post::Model { - id: 6, - title: "Title D".to_owned(), - text: "Text D".to_owned(), - }], - vec![post::Model { - id: 1, - title: "Title A".to_owned(), - text: "Text A".to_owned(), - }], - vec![post::Model { - id: 1, - title: "New Title A".to_owned(), - text: "New Text A".to_owned(), - }], - vec![post::Model { - id: 5, - title: "Title C".to_owned(), - text: "Text C".to_owned(), - }], - ]) - .append_exec_results(vec![ - MockExecResult { - last_insert_id: 6, - rows_affected: 1, - }, - MockExecResult { - last_insert_id: 6, - rows_affected: 5, - }, - ]) - .into_connection() -} diff --git a/entity/src/admin.rs b/entity/src/admin.rs index ccfab5a..1dd8420 100644 --- a/entity/src/admin.rs +++ b/entity/src/admin.rs @@ -1,8 +1,10 @@ //! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 use sea_orm::entity::prelude::*; +use rocket::serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[serde(crate = "rocket::serde")] #[sea_orm(table_name = "admin")] pub struct Model { #[sea_orm(primary_key)]