diff options
| author | Fuwn <[email protected]> | 2020-11-19 23:53:52 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2020-11-19 23:53:52 +0000 |
| commit | 26b647ca3f006662676690e6b99bfbfb32167738 (patch) | |
| tree | 1815e651090e928341b7f88be6781028a400b19a /src | |
| parent | feat: limitations in readme (diff) | |
| download | chan-26b647ca3f006662676690e6b99bfbfb32167738.tar.xz chan-26b647ca3f006662676690e6b99bfbfb32167738.zip | |
feat, chore (desc)
feat:
- add example database
- ADD BOARDS
- 404 page
- better ui (bootstrap)
chore:
- move structures to strucutres file
- various capitalizations
Diffstat (limited to 'src')
| -rw-r--r-- | src/api.rs | 4 | ||||
| -rw-r--r-- | src/db.rs | 57 | ||||
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/structures.rs | 16 | ||||
| -rw-r--r-- | src/ui.rs | 51 |
5 files changed, 108 insertions, 27 deletions
@@ -2,12 +2,14 @@ use rocket::request::Form; use rocket::response::Redirect; use crate::db::*; +use crate::structures::*; // 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(), }) { @@ -16,5 +18,5 @@ pub fn post(thread: Form<Thread>) -> Redirect { } // Redirect to all posts. - Redirect::to("/threads") + Redirect::to(format!("/board/{}", thread.board)) } @@ -1,11 +1,6 @@ use rusqlite::{params, Connection, Result, Error as SQLError}; -/// The format a valid SQLlite thread entry should have. -#[derive(FromForm, Debug, Serialize, Deserialize)] -pub struct Thread { - pub name: String, - pub comment: String -} +use crate::structures::*; /// Create a new thread in the SQLite database. pub fn new_thread(mut thread: Thread) -> Result<()> { @@ -14,29 +9,65 @@ pub fn new_thread(mut thread: Thread) -> Result<()> { // If the name field in the POST request is empty, user should be "anonymous". if thread.name == "" { - thread.name = "anonymous".to_owned(); + thread.name = "Anonymous".to_owned(); } // Add thread entry to database. db.execute( - "INSERT INTO thread (name, comment) VALUES (?1, ?2)", - params![thread.name, thread.comment] + "INSERT INTO threads (board, name, comment) VALUES (?1, ?2, ?3)", + params![thread.board, thread.name, thread.comment] )?; Ok(()) } /// Get all threads from the SQLite databse. -pub fn get_threads() -> Result<Vec<Thread>, SQLError> { +pub fn _get_all_threads() -> Result<Vec<Thread>, SQLError> { // Open SQLite database file. let db = Connection::open("chan.db")?; // Get all entries from the thread table. - let mut statement = db.prepare("SELECT name, comment FROM thread")?; + let mut statement = db.prepare("SELECT board, name, comment FROM threads")?; let threads = statement.query_map(params![], |row| { Ok(Thread { - name: row.get(0)?, - comment: row.get(1)? + 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")?; + + // 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")?; + + // 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 f5fbd56..c9afc46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -#![feature(proc_macro_hygiene, decl_macro)] +#![feature(proc_macro_hygiene, decl_macro, option_result_contains)] #[macro_use] extern crate rocket; #[macro_use] extern crate serde_derive; @@ -8,6 +8,7 @@ extern crate serde_json; mod api; mod ui; mod db; +mod structures; use rocket_contrib::templates::Template; @@ -19,8 +20,8 @@ fn main() { ]) .mount("/", routes![ ui::index, - ui::make_post, - ui::threads + ui::boards, + ui::board ]) .mount("/api/v1/", routes![ api::post diff --git a/src/structures.rs b/src/structures.rs new file mode 100644 index 0000000..8d0efb3 --- /dev/null +++ b/src/structures.rs @@ -0,0 +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 +} + +/// 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 +} @@ -1,5 +1,4 @@ use rocket_contrib::templates::Template; -use rocket::response::Redirect; use crate::db::*; @@ -10,20 +9,52 @@ pub fn index() -> Template { } #[catch(404)] -pub fn not_found() -> Redirect { - Redirect::to("/") +pub fn not_found() -> Template { + Template::render("404", &()) } // GET: Make a new thread. -#[get("/post")] -pub fn make_post() -> Template { - Template::render("post", &()) -} +// #[get("/post")] +// pub fn make_post() -> Template { +// Template::render("post", &()) +// } // GET: Check out all the threads. -#[get("/threads")] -pub fn threads() -> Template { - let context = get_threads().unwrap(); +// #[get("/threads")] +// pub fn threads() -> Template { +// let context = get_all_threads().unwrap(); +// let threads = serde_json::json!(&context); +// Template::render("threads", threads) +// } + +// 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) +} + +// 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) } |