mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-13 11:32:19 +00:00
Refactoring aby to šlo spustit (#4)
* 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 <vojta.jungmann@gmail.com>
This commit is contained in:
parent
9dc330da6a
commit
c47dfbb1f9
7 changed files with 20 additions and 240 deletions
|
|
@ -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 = "<post_form>")]
|
||||
async fn create(conn: Connection<'_, Db>, post_form: Form<post::Model>) -> Flash<Redirect> {
|
||||
async fn create(conn: Connection<'_, Db>, post_form: Json<candidate::Model>) -> Flash<Redirect> {
|
||||
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("/<id>", data = "<post_form>")]
|
||||
async fn update(
|
||||
conn: Connection<'_, Db>,
|
||||
id: i32,
|
||||
post_form: Form<post::Model>,
|
||||
) -> Flash<Redirect> {
|
||||
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("/<id>")]
|
||||
async fn delete(conn: Connection<'_, Db>, id: i32) -> Flash<Redirect> {
|
||||
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<String>> {
|
||||
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<Build>) -> 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
|
||||
|
|
|
|||
|
|
@ -19,7 +19,3 @@ tokio = "1.21.2"
|
|||
|
||||
[features]
|
||||
mock = ["sea-orm/mock"]
|
||||
|
||||
[[test]]
|
||||
name = "mock"
|
||||
required-features = ["mock"]
|
||||
|
|
|
|||
|
|
@ -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, DbErr> {
|
||||
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<post::Model, DbErr> {
|
||||
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<DeleteResult, DbErr> {
|
||||
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<DeleteResult, DbErr> {
|
||||
Post::delete_many().exec(db).await
|
||||
form_data: candidate::Model,
|
||||
) -> Result<candidate::ActiveModel, DbErr> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Option<post::Model>, 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<post::Model>, 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<Option<candidate::Model>, DbErr> {
|
||||
Candidate::find_by_id(id).one(db).await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue