alright this is looking like an actual codebase now

This commit is contained in:
Erin 2024-04-15 02:54:16 -04:00
parent 4b4122267a
commit 70df1a64f3
Signed by: erin
SSH key fingerprint: SHA256:clvLPaxKthBet+VUQTKQdDkjaqg2/KnYHQaPASp5pFE
2 changed files with 44 additions and 30 deletions

View file

@ -0,0 +1 @@
ALTER TABLE comments ADD COLUMN page TEXT NOT NULL DEFAULT "/";

View file

@ -1,10 +1,51 @@
use sqlx::SqlitePool;
use std::env;
#[rocket::get("/txt?<page>")]
async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> 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?<page>&<name>&<content>")]
async fn add(
page: &str,
name: Option<&str>,
content: &str,
pool: &rocket::State<SqlitePool>,
) -> () {
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<SqlitePool>) -> 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<SqlitePool>) -> &str {
sqlx::query!("INSERT INTO comments (content) VALUES (\"hi\")")
.execute(pool.inner())
.await
.expect("ajkl;shfklshadflgksdfg");
"neat ok"
}