alright this is looking like an actual codebase now
This commit is contained in:
parent
4b4122267a
commit
70df1a64f3
1
migrations/20240415061216_add-page-col.sql
Normal file
1
migrations/20240415061216_add-page-col.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ALTER TABLE comments ADD COLUMN page TEXT NOT NULL DEFAULT "/";
|
73
src/main.rs
73
src/main.rs
|
@ -1,10 +1,51 @@
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use std::env;
|
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]
|
#[rocket::launch]
|
||||||
async fn rocket() -> _ {
|
async fn rocket() -> _ {
|
||||||
println!("Hello, world!");
|
|
||||||
|
|
||||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is required");
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is required");
|
||||||
let pool = SqlitePool::connect(&db_url)
|
let pool = SqlitePool::connect(&db_url)
|
||||||
.await
|
.await
|
||||||
|
@ -13,31 +54,3 @@ async fn rocket() -> _ {
|
||||||
.manage(pool)
|
.manage(pool)
|
||||||
.mount("/", rocket::routes![index, add])
|
.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"
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue