From 3b880c0ae7963d783fb87a3389f98070e448495a Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Wed, 16 Jun 2021 14:47:57 -0600 Subject: *clears breath* extend tutorial further --- src/bin/tosin-admin.rs | 22 +++++++++++++ tests/tutorial/mod.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/src/bin/tosin-admin.rs b/src/bin/tosin-admin.rs index e519d50..0a359d3 100644 --- a/src/bin/tosin-admin.rs +++ b/src/bin/tosin-admin.rs @@ -29,10 +29,29 @@ tosin::main!(urls(), settings()); "#; const APP_MOD: &str = r#" +use tosin::apps::AppConfig; + +mod migrations; +pub mod models; pub mod urls; pub mod views; pub use urls::urls; + +pub const APP: AppConfig = AppConfig { + name: module_path!(), + migrations: migrations::migrations, +}; +"#; +const APP_MIGRATIONS: &str = r#" +use tosin::db::migration::{Migration, gather}; + +gather!(); +"#; +const APP_MODELS: &str = r#" +use tosin::db::models::{Model, Id}; + +// TODO define models "#; const APP_URLS: &str = r#" use tosin::urls::{UrlMap, url_map}; @@ -100,6 +119,9 @@ fn main() { let app_folder = Path::new("src").join(name); fs::create_dir(&app_folder).unwrap(); fs::write(app_folder.join("mod.rs"), APP_MOD).unwrap(); + fs::create_dir_all(app_folder.join("migrations")).unwrap(); + fs::write(app_folder.join("migrations/mod.rs"), APP_MIGRATIONS).unwrap(); + fs::write(app_folder.join("models.rs"), APP_MODELS).unwrap(); fs::write(app_folder.join("urls.rs"), APP_URLS).unwrap(); fs::write(app_folder.join("views.rs"), APP_VIEWS).unwrap(); } else { diff --git a/tests/tutorial/mod.rs b/tests/tutorial/mod.rs index a9eb76f..0bd626e 100644 --- a/tests/tutorial/mod.rs +++ b/tests/tutorial/mod.rs @@ -161,7 +161,7 @@ tosin::main!(urls(), settings()); // vibe check let test_tutorial1 = Path::new("src"); let example_tutorial1 = Path::new(PROJECT_DIR).join("examples/tutorial01"); - for file in &["main.rs", "polls/mod.rs", "polls/urls.rs", "polls/views.rs"] { + for file in &["main.rs", "polls/mod.rs", "polls/migrations/mod.rs", "polls/models.rs", "polls/urls.rs", "polls/views.rs"] { let test_path = test_tutorial1.join(file); let test_file = fs::read_to_string(test_path).unwrap(); let example_path = example_tutorial1.join(file); @@ -206,4 +206,86 @@ tosin::main!(urls(), settings()); .status() .unwrap() .check(); + + // update Cargo.toml + fs::write("Cargo.toml", r#" +[package] +name = "tutorial" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +time = "0.2.27" +tosin = { path = "/home/cactus/Documents/tosin" } + "#).unwrap(); + + // update polls/models.rs + fs::write("src/polls/models.rs", r#" +use tosin::db::models::{Model, Id}; + +#[derive(Model)] +pub struct Question { + id: Option, + #[model(max_length=200)] + question_text: String, + /// date published + pub_date: time::PrimitiveDateTime, +} + +#[derive(Model)] +pub struct Choice { + id: Option, + #[model(Question, on_delete=Cascade)] + question: Id, + #[model(max_length=200)] + choice_text: String, + #[model(default = 0)] + votes: usize, +} + "#).unwrap(); + + // update main.rs + fs::write("src/main.rs", r#" +use tosin::Settings; +use tosin::contrib::admin; +use tosin::db::backend::Connectable; +use tosin::urls::{UrlMap, url_map}; + +mod polls; + +fn urls() -> UrlMap { + url_map! { + "polls" / ..polls::urls(), + "admin" / ..admin::site::urls(), + } +} + +fn settings() -> Settings { + Settings { + installed_apps: &[ + &polls::APP, + &admin::APP, + ], + ..Settings::default() + } +} + +tosin::main!(urls(), settings()); + "#).unwrap(); + + // cargo run make-migrations polls + Command::new(CARGO) + .args(&["run", "make-migrations", "polls"]) + .status() + .unwrap() + .check(); + + // cargo run migrate + Command::new(CARGO) + .args(&["run", "migrate"]) + .status() + .unwrap() + .check(); } -- cgit v1.2.3