diff options
| author | Fuwn <[email protected]> | 2021-02-20 22:57:33 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-02-20 22:57:33 +0000 |
| commit | 0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7 (patch) | |
| tree | c4f1201aeea4fbac52fa033cda8c3f72beb062fa /src | |
| parent | chore: update log (diff) | |
| download | chan-0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7.tar.xz chan-0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7.zip | |
chore: formatting
Diffstat (limited to 'src')
| -rw-r--r-- | src/api.rs | 26 | ||||
| -rw-r--r-- | src/db.rs | 98 | ||||
| -rw-r--r-- | src/main.rs | 28 | ||||
| -rw-r--r-- | src/structures.rs | 14 | ||||
| -rw-r--r-- | src/ui.rs | 58 |
5 files changed, 112 insertions, 112 deletions
@@ -4,19 +4,19 @@ use rocket::response::Redirect; use crate::db::*; use crate::structures::*; -// POST: Create a new thread. +/// POST: Create a new thread. #[post("/post", data = "<thread>")] pub fn post(thread: Form<Thread>) -> Redirect { - // Pretty rudimentary error handling. - match new_thread(Thread { - board: thread.board.clone(), - name: thread.name.clone(), - comment: thread.comment.clone(), - }) { - Ok(()) => { }, - Err(why) => println!("Error creating new thread: {}", why) - } + // Pretty rudimentary error handling. + match new_thread(Thread { + board: thread.board.clone(), + name: thread.name.clone(), + comment: thread.comment.clone(), + }) { + Ok(()) => { }, + Err(why) => println!("Error creating new thread: {}", why) + } - // Redirect to all posts. - Redirect::to(format!("/board/{}", thread.board)) -} + // Redirect to all posts. + Redirect::to(format!("/board/{}", thread.board)) +}
\ No newline at end of file @@ -4,71 +4,71 @@ use crate::structures::*; /// Create a new thread in the SQLite database. pub fn new_thread(mut thread: Thread) -> Result<()> { - // Open SQLite database file. - let db = Connection::open("chan.db")?; + // Open SQLite database file. + let db = Connection::open("chan.db")?; - // If the name field in the POST request is empty, user should be "anonymous". - if thread.name == "" { - thread.name = "Anonymous".to_owned(); - } + // If the name field in the POST request is empty, user should be "anonymous". + if thread.name == "" { + thread.name = "Anonymous".to_owned(); + } - // Add thread entry to database. - db.execute( - "INSERT INTO threads (board, name, comment) VALUES (?1, ?2, ?3)", - params![thread.board, thread.name, thread.comment] - )?; + // Add thread entry to database. + db.execute( + "INSERT INTO threads (board, name, comment) VALUES (?1, ?2, ?3)", + params![thread.board, thread.name, thread.comment] + )?; - Ok(()) + Ok(()) } /// Get all threads from the SQLite databse. pub fn _get_all_threads() -> Result<Vec<Thread>, SQLError> { - // Open SQLite database file. - let db = Connection::open("chan.db")?; + // Open SQLite database file. + let db = Connection::open("chan.db")?; - // Get all entries from the thread table. - let mut statement = db.prepare("SELECT board, name, comment FROM threads")?; - let threads = statement.query_map(params![], |row| { - Ok(Thread { - board: row.get(0)?, - name: row.get(1)?, - comment: row.get(2)? - }) - })?; - threads.collect() + // Get all entries from the thread table. + let mut statement = db.prepare("SELECT board, name, comment FROM threads")?; + let threads = statement.query_map(params![], |row| { + Ok(Thread { + board: row.get(0)?, + name: row.get(1)?, + comment: row.get(2)? + }) + })?; + threads.collect() } /// Get all boards from the SQLite databse. pub fn get_boards() -> Result<Vec<Board>, SQLError> { - // Open SQLite database file. - let db = Connection::open("chan.db")?; + // Open SQLite database file. + let db = Connection::open("chan.db")?; - // Get all entries from the thread table. - let mut statement = db.prepare("SELECT tag, name, nsfw, disabled FROM boards")?; - let boards = statement.query_map(params![], |row| { - Ok(Board { - tag: row.get(0)?, - name: row.get(1)?, - nsfw: row.get(2)?, - disabled: row.get(3)? - }) - })?; - boards.collect() + // Get all entries from the thread table. + let mut statement = db.prepare("SELECT tag, name, nsfw, disabled FROM boards")?; + let boards = statement.query_map(params![], |row| { + Ok(Board { + tag: row.get(0)?, + name: row.get(1)?, + nsfw: row.get(2)?, + disabled: row.get(3)? + }) + })?; + boards.collect() } /// Get all threads from a specific board within the SQLite databse. pub fn get_threads(board: String) -> Result<Vec<Thread>, SQLError> { - // Open SQLite database file. - let db = Connection::open("chan.db")?; + // Open SQLite database file. + let db = Connection::open("chan.db")?; - // Get all entries from the thread table. - let mut statement = db.prepare("SELECT board, name, comment FROM threads WHERE board = (?)")?; - let threads = statement.query_map(params![board], |row| { - Ok(Thread { - board: row.get(0)?, - name: row.get(1)?, - comment: row.get(2)? - }) - })?; - threads.collect() + // Get all entries from the thread table. + let mut statement = db.prepare("SELECT board, name, comment FROM threads WHERE board = (?)")?; + let threads = statement.query_map(params![board], |row| { + Ok(Thread { + board: row.get(0)?, + name: row.get(1)?, + comment: row.get(2)? + }) + })?; + threads.collect() } diff --git a/src/main.rs b/src/main.rs index c9afc46..5694dd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,18 +13,18 @@ mod structures; use rocket_contrib::templates::Template; fn main() { - rocket::ignite() - .attach(Template::fairing()) - .register(catchers![ - ui::not_found - ]) - .mount("/", routes![ - ui::index, - ui::boards, - ui::board - ]) - .mount("/api/v1/", routes![ - api::post - ]) - .launch(); + rocket::ignite() + .attach(Template::fairing()) + .register(catchers![ + ui::not_found + ]) + .mount("/", routes![ + ui::index, + ui::boards, + ui::board + ]) + .mount("/api/v1/", routes![ + api::post + ]) + .launch(); } diff --git a/src/structures.rs b/src/structures.rs index 8d0efb3..1114bbc 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -1,16 +1,16 @@ /// The format a valid SQLlite thread entry should have. #[derive(FromForm, Debug, Serialize, Deserialize)] pub struct Thread { - pub board: String, - pub name: String, - pub comment: String + pub board: String, + pub name: String, + pub comment: String } /// The format a valid SQLlite thread entry should have. #[derive(FromForm, Debug, Serialize, Deserialize, PartialEq)] pub struct Board { - pub tag: String, - pub name: String, - pub nsfw: i32, - pub disabled: i32 + pub tag: String, + pub name: String, + pub nsfw: i32, + pub disabled: i32 } @@ -2,24 +2,24 @@ use rocket_contrib::templates::Template; use crate::db::*; -// GET: Index. +/// GET: Index. #[get("/")] pub fn index() -> Template { - Template::render("index", &()) + Template::render("index", &()) } #[catch(404)] pub fn not_found() -> Template { - Template::render("404", &()) + Template::render("404", &()) } -// GET: Make a new thread. +/// GET: Make a new thread. // #[get("/post")] // pub fn make_post() -> Template { // Template::render("post", &()) // } -// GET: Check out all the threads. +/// GET: Check out all the threads. // #[get("/threads")] // pub fn threads() -> Template { // let context = get_all_threads().unwrap(); @@ -27,34 +27,34 @@ pub fn not_found() -> Template { // Template::render("threads", threads) // } -// GET: Check out all the boards. +/// GET: Check out all the boards. #[get("/boards")] pub fn boards() -> Template { - let context = get_boards().unwrap(); - let threads = serde_json::json!(&context); - Template::render("boards", threads) + let context = get_boards().unwrap(); + let threads = serde_json::json!(&context); + Template::render("boards", threads) } -// GET: Check out all threads within a board. +/// GET: Check out all threads within a board. #[get("/board/<board>")] pub fn board(board: String) -> Template { - let boards = serde_json::json!(get_boards().unwrap()); - - let mut board_count: i32 = 0; - for i in boards.as_array().unwrap() { - if i.get("tag") == Some(&serde_json::json!(board)) { - board_count += 1; - } - } - if board_count == 0 { return Template::render("404", &()); } - - // Return 404 if the board is not found. - // This doesn't work, but I kept it because I might try to fix it later. - // if !boards.as_array().unwrap().contains(&serde_json::json!(board)) { - // return Template::render("404", &()); - // } - - let context = get_threads(board).unwrap(); - let threads = serde_json::json!(&context); - Template::render("threads", threads) + let boards = serde_json::json!(get_boards().unwrap()); + + let mut board_count: i32 = 0; + for i in boards.as_array().unwrap() { + if i.get("tag") == Some(&serde_json::json!(board)) { + board_count += 1; + } + } + if board_count == 0 { return Template::render("404", &()); } + + // Return 404 if the board is not found. + // This doesn't work, but I kept it because I might try to fix it later. + // if !boards.as_array().unwrap().contains(&serde_json::json!(board)) { + // return Template::render("404", &()); + // } + + let context = get_threads(board).unwrap(); + let threads = serde_json::json!(&context); + Template::render("threads", threads) } |