okay wow this is looking like a real thing now actually. this is kinda cool
This commit is contained in:
parent
70df1a64f3
commit
ac593e2882
39
src/main.rs
39
src/main.rs
|
@ -1,17 +1,34 @@
|
|||
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!(
|
||||
struct Comment {
|
||||
id: i64,
|
||||
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 = ?",
|
||||
page
|
||||
)
|
||||
.fetch_all(pool.inner())
|
||||
.await
|
||||
.expect("what the fuck");
|
||||
.fetch_all(pool)
|
||||
.await?
|
||||
.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()
|
||||
.map(|rec| {
|
||||
format!(
|
||||
|
@ -21,7 +38,7 @@ async fn index(page: &str, pool: &rocket::State<SqlitePool>) -> String {
|
|||
rec.content,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
.collect::<String>())
|
||||
}
|
||||
|
||||
#[rocket::get("/add?<page>&<name>&<content>")]
|
||||
|
@ -30,7 +47,7 @@ async fn add(
|
|||
name: Option<&str>,
|
||||
content: &str,
|
||||
pool: &rocket::State<SqlitePool>,
|
||||
) -> () {
|
||||
) -> Result<(), String> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO comments (page, name, content) VALUES (?, ?, ?)",
|
||||
page,
|
||||
|
@ -39,9 +56,9 @@ async fn add(
|
|||
)
|
||||
.execute(pool.inner())
|
||||
.await
|
||||
.expect("ajkl;shfklshadflgksdfg");
|
||||
.map_err(|_| "ajkl;shfklshadflgksdfg")?;
|
||||
|
||||
()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
|
|
Loading…
Reference in a new issue