From 70df1a64f3aa2caff42bc2940a9786b80790586a Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 15 Apr 2024 02:54:16 -0400 Subject: [PATCH] alright this is looking like an actual codebase now --- migrations/20240415061216_add-page-col.sql | 1 + src/main.rs | 73 +++++++++++++--------- 2 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 migrations/20240415061216_add-page-col.sql diff --git a/migrations/20240415061216_add-page-col.sql b/migrations/20240415061216_add-page-col.sql new file mode 100644 index 0000000..51b2dc7 --- /dev/null +++ b/migrations/20240415061216_add-page-col.sql @@ -0,0 +1 @@ +ALTER TABLE comments ADD COLUMN page TEXT NOT NULL DEFAULT "/"; diff --git a/src/main.rs b/src/main.rs index 62d427b..94ef4eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,51 @@ use sqlx::SqlitePool; use std::env; +#[rocket::get("/txt?")] +async fn index(page: &str, pool: &rocket::State) -> String { + let records: Vec<_> = sqlx::query!( + "SELECT id, name, content FROM comments WHERE page = ?", + page + ) + .fetch_all(pool.inner()) + .await + .expect("what the fuck"); + + records + .into_iter() + .map(|rec| { + format!( + "- [{}] {}: {}\n", + rec.id, + rec.name.unwrap_or("anonymous".to_owned()), + rec.content, + ) + }) + .collect() +} + +#[rocket::get("/add?&&")] +async fn add( + page: &str, + name: Option<&str>, + content: &str, + pool: &rocket::State, +) -> () { + sqlx::query!( + "INSERT INTO comments (page, name, content) VALUES (?, ?, ?)", + page, + name, + content + ) + .execute(pool.inner()) + .await + .expect("ajkl;shfklshadflgksdfg"); + + () +} + #[rocket::launch] async fn rocket() -> _ { - println!("Hello, world!"); - let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is required"); let pool = SqlitePool::connect(&db_url) .await @@ -13,31 +54,3 @@ async fn rocket() -> _ { .manage(pool) .mount("/", rocket::routes![index, add]) } - -#[rocket::get("/")] -async fn index(pool: &rocket::State) -> String { - let records = sqlx::query!("SELECT id, name, content FROM comments") - .fetch_all(pool.inner()) - .await - .expect("what the fuck"); - - let mut result = "yeetus\n".to_owned(); - for rec in records { - result += &format!( - "- {}: {}\n", - rec.name.unwrap_or("anonymous".to_owned()), - rec.content - ); - } - result -} - -#[rocket::get("/add")] -async fn add(pool: &rocket::State) -> &str { - sqlx::query!("INSERT INTO comments (content) VALUES (\"hi\")") - .execute(pool.inner()) - .await - .expect("ajkl;shfklshadflgksdfg"); - - "neat ok" -}