okay wow this is looking like a real thing now actually. this is kinda cool

This commit is contained in:
Erin 2024-04-15 03:13:37 -04:00
parent 70df1a64f3
commit ac593e2882
Signed by: erin
SSH key fingerprint: SHA256:clvLPaxKthBet+VUQTKQdDkjaqg2/KnYHQaPASp5pFE

View file

@ -1,17 +1,34 @@
use sqlx::SqlitePool; use sqlx::SqlitePool;
use std::env; use std::env;
#[rocket::get("/txt?<page>")] struct Comment {
async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> String { id: i64,
let records: Vec<_> = sqlx::query!( name: Option<String>,
content: String,
}
async fn get_comments(pool: &SqlitePool, page: &str) -> Result<Vec<Comment>, sqlx::Error> {
let results = sqlx::query!(
"SELECT id, name, content FROM comments WHERE page = ?", "SELECT id, name, content FROM comments WHERE page = ?",
page page
) )
.fetch_all(pool.inner()) .fetch_all(pool)
.await .await?
.expect("what the fuck"); .into_iter()
.map(|result| Comment {
id: result.id,
name: result.name,
content: result.content,
})
.collect();
return Ok(results);
}
records #[rocket::get("/txt?<page>")]
async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> Result<String, String> {
Ok(get_comments(pool, page)
.await
.map_err(|_| "error fetching comments for this page, sorry")?
.into_iter() .into_iter()
.map(|rec| { .map(|rec| {
format!( format!(
@ -21,7 +38,7 @@ async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> String {
rec.content, rec.content,
) )
}) })
.collect() .collect::<String>())
} }
#[rocket::get("/add?<page>&<name>&<content>")] #[rocket::get("/add?<page>&<name>&<content>")]
@ -30,7 +47,7 @@ async fn add(
name: Option<&str>, name: Option<&str>,
content: &str, content: &str,
pool: &rocket::State<SqlitePool>, pool: &rocket::State<SqlitePool>,
) -> () { ) -> Result<(), String> {
sqlx::query!( sqlx::query!(
"INSERT INTO comments (page, name, content) VALUES (?, ?, ?)", "INSERT INTO comments (page, name, content) VALUES (?, ?, ?)",
page, page,
@ -39,9 +56,9 @@ async fn add(
) )
.execute(pool.inner()) .execute(pool.inner())
.await .await
.expect("ajkl;shfklshadflgksdfg"); .map_err(|_| "ajkl;shfklshadflgksdfg")?;
() Ok(())
} }
#[rocket::launch] #[rocket::launch]