From 26b647ca3f006662676690e6b99bfbfb32167738 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 19 Nov 2020 23:53:52 +0000 Subject: feat, chore (desc) feat: - add example database - ADD BOARDS - 404 page - better ui (bootstrap) chore: - move structures to strucutres file - various capitalizations --- .gitignore | 1 - chan.db | Bin 0 -> 24576 bytes src/api.rs | 4 +++- src/db.rs | 57 ++++++++++++++++++++++++++++++++++----------- src/main.rs | 7 +++--- src/structures.rs | 16 +++++++++++++ src/ui.rs | 51 ++++++++++++++++++++++++++++++++-------- templates/404.html.hbs | 24 +++++++++++++++++++ templates/boards.html.hbs | 18 ++++++++++++++ templates/header.html.hbs | 26 ++++++++++----------- templates/index.html.hbs | 15 ++++++++++++ templates/layout.html.hbs | 28 ++++++++++++++++++++++ templates/post.html.hbs | 35 ---------------------------- templates/threads.html.hbs | 48 ++++++++++++++++++++++++++++++++++++-- 14 files changed, 252 insertions(+), 78 deletions(-) create mode 100644 chan.db create mode 100644 src/structures.rs create mode 100644 templates/404.html.hbs create mode 100644 templates/boards.html.hbs create mode 100644 templates/layout.html.hbs delete mode 100644 templates/post.html.hbs diff --git a/.gitignore b/.gitignore index 2c4918c..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ /target -*.db diff --git a/chan.db b/chan.db new file mode 100644 index 0000000..bd7412e Binary files /dev/null and b/chan.db differ diff --git a/src/api.rs b/src/api.rs index 8d8d2f2..8fd61c5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -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 = "")] pub fn post(thread: Form) -> 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) -> Redirect { } // Redirect to all posts. - Redirect::to("/threads") + Redirect::to(format!("/board/{}", thread.board)) } diff --git a/src/db.rs b/src/db.rs index 7a5a85d..4eea4c7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -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, SQLError> { +pub fn _get_all_threads() -> Result, 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, 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, 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 +} diff --git a/src/ui.rs b/src/ui.rs index 0a054e7..def8d76 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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/")] +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) } diff --git a/templates/404.html.hbs b/templates/404.html.hbs new file mode 100644 index 0000000..5940d43 --- /dev/null +++ b/templates/404.html.hbs @@ -0,0 +1,24 @@ +chan - 404 + +{{> header }} + +

Error 404: Resource not found...

+ +
+⣿⣿⣷⡁⢆⠈⠕⢕⢂⢕⢂⢕⢂⢔⢂⢕⢄⠂⣂⠂⠆⢂⢕⢂⢕⢂⢕⢂⢕⢂
+⣿⣿⣿⡷⠊⡢⡹⣦⡑⢂⢕⢂⢕⢂⢕⢂⠕⠔⠌⠝⠛⠶⠶⢶⣦⣄⢂⢕⢂⢕
+⣿⣿⠏⣠⣾⣦⡐⢌⢿⣷⣦⣅⡑⠕⠡⠐⢿⠿⣛⠟⠛⠛⠛⠛⠡⢷⡈⢂⢕⢂
+⠟⣡⣾⣿⣿⣿⣿⣦⣑⠝⢿⣿⣿⣿⣿⣿⡵⢁⣤⣶⣶⣿⢿⢿⢿⡟⢻⣤⢑⢂
+⣾⣿⣿⡿⢟⣛⣻⣿⣿⣿⣦⣬⣙⣻⣿⣿⣷⣿⣿⢟⢝⢕⢕⢕⢕⢽⣿⣿⣷⣔
+⣿⣿⠵⠚⠉⢀⣀⣀⣈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣗⢕⢕⢕⢕⢕⢕⣽⣿⣿⣿⣿
+⢷⣂⣠⣴⣾⡿⡿⡻⡻⣿⣿⣴⣿⣿⣿⣿⣿⣿⣷⣵⣵⣵⣷⣿⣿⣿⣿⣿⣿⡿
+⢌⠻⣿⡿⡫⡪⡪⡪⡪⣺⣿⣿⣿⣿⣿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃
+⠣⡁⠹⡪⡪⡪⡪⣪⣾⣿⣿⣿⣿⠋⠐⢉⢍⢄⢌⠻⣿⣿⣿⣿⣿⣿⣿⣿⠏⠈
+⡣⡘⢄⠙⣾⣾⣾⣿⣿⣿⣿⣿⣿⡀⢐⢕⢕⢕⢕⢕⡘⣿⣿⣿⣿⣿⣿⠏⠠⠈
+⠌⢊⢂⢣⠹⣿⣿⣿⣿⣿⣿⣿⣿⣧⢐⢕⢕⢕⢕⢕⢅⣿⣿⣿⣿⡿⢋⢜⠠⠈
+⠄⠁⠕⢝⡢⠈⠻⣿⣿⣿⣿⣿⣿⣿⣷⣕⣑⣑⣑⣵⣿⣿⣿⡿⢋⢔⢕⣿⠠⠈
+⠨⡂⡀⢑⢕⡅⠂⠄⠉⠛⠻⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢋⢔⢕⢕⣿⣿⠠⠈
+⠄⠪⣂⠁⢕⠆⠄⠂⠄⠁⡀⠂⡀⠄⢈⠉⢍⢛⢛⢛⢋⢔⢕⢕⢕⣽⣿⣿⠠⠈
+
+ +Return home. diff --git a/templates/boards.html.hbs b/templates/boards.html.hbs new file mode 100644 index 0000000..d86ba2b --- /dev/null +++ b/templates/boards.html.hbs @@ -0,0 +1,18 @@ +chan - Boards + +{{> header }} + +

Boards

+ +
+ +
diff --git a/templates/header.html.hbs b/templates/header.html.hbs index 6057f69..28786e4 100644 --- a/templates/header.html.hbs +++ b/templates/header.html.hbs @@ -1,16 +1,16 @@ - +{{> layout }} -chan +

diff --git a/templates/index.html.hbs b/templates/index.html.hbs index 92d2b35..ef756d0 100644 --- a/templates/index.html.hbs +++ b/templates/index.html.hbs @@ -1,5 +1,9 @@ +chan - Home + {{> header }} +

chan

+
 ⣿⣿⣷⡁⢆⠈⠕⢕⢂⢕⢂⢕⢂⢔⢂⢕⢄⠂⣂⠂⠆⢂⢕⢂⢕⢂⢕⢂⢕⢂
 ⣿⣿⣿⡷⠊⡢⡹⣦⡑⢂⢕⢂⢕⢂⢕⢂⠕⠔⠌⠝⠛⠶⠶⢶⣦⣄⢂⢕⢂⢕
@@ -16,3 +20,14 @@
 ⠨⡂⡀⢑⢕⡅⠂⠄⠉⠛⠻⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢋⢔⢕⢕⣿⣿⠠⠈
 ⠄⠪⣂⠁⢕⠆⠄⠂⠄⠁⡀⠂⡀⠄⢈⠉⢍⢛⢛⢛⢋⢔⢕⢕⢕⣽⣿⣿⠠⠈
 
+ + diff --git a/templates/layout.html.hbs b/templates/layout.html.hbs new file mode 100644 index 0000000..a9c0dcc --- /dev/null +++ b/templates/layout.html.hbs @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + +
diff --git a/templates/post.html.hbs b/templates/post.html.hbs deleted file mode 100644 index 37c5ec6..0000000 --- a/templates/post.html.hbs +++ /dev/null @@ -1,35 +0,0 @@ -{{> header }} - -
- - - - - - - - - - - - - - - - - - - - -
name - - -
comment - -
verification - i'm not a robot -
file - -
-
diff --git a/templates/threads.html.hbs b/templates/threads.html.hbs index 5a656f7..f9f1b85 100644 --- a/templates/threads.html.hbs +++ b/templates/threads.html.hbs @@ -1,13 +1,57 @@ +chan - /{{ this.0.board }}/ + {{> header }} +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + + +
+
+ +
+ {{ #if this }} {{ #each this }} -
+ + +
+
+

{{ this.name }}

+

{{ this.comment }}

+ + +

{{ /each }} {{ else }} -

no posts today

+ {{ /if }} -- cgit v1.2.3