From 793e5c5f9883a83d66e87ea974546380d121d048 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 18 Nov 2020 21:16:45 +0000 Subject: repo: :star: --- src/db.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/db.rs (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..7a5a85d --- /dev/null +++ b/src/db.rs @@ -0,0 +1,43 @@ +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 +} + +/// 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")?; + + // 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 thread (name, comment) VALUES (?1, ?2)", + params![thread.name, thread.comment] + )?; + + Ok(()) +} + +/// Get all threads from the SQLite databse. +pub fn get_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 threads = statement.query_map(params![], |row| { + Ok(Thread { + name: row.get(0)?, + comment: row.get(1)? + }) + })?; + threads.collect() +} -- cgit v1.2.3