aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-02-20 22:57:33 +0000
committerFuwn <[email protected]>2021-02-20 22:57:33 +0000
commit0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7 (patch)
treec4f1201aeea4fbac52fa033cda8c3f72beb062fa
parentchore: update log (diff)
downloadchan-0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7.tar.xz
chan-0a8126ac5330fc022b6d1b5fb4b9c2fa38028ef7.zip
chore: formatting
-rw-r--r--.gitignore1
-rw-r--r--.idea/.gitignore8
-rw-r--r--src/api.rs26
-rw-r--r--src/db.rs98
-rw-r--r--src/main.rs28
-rw-r--r--src/structures.rs14
-rw-r--r--src/ui.rs58
7 files changed, 121 insertions, 112 deletions
diff --git a/.gitignore b/.gitignore
index ea8c4bf..d81f12e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/target
+/.idea
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..2e06d98
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../../../:\code\rust\git\fuwn\chan\.idea/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/src/api.rs b/src/api.rs
index 8fd61c5..3947452 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -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
diff --git a/src/db.rs b/src/db.rs
index 4eea4c7..a88b2bf 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -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
}
diff --git a/src/ui.rs b/src/ui.rs
index def8d76..d11412b 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -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)
}