aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
blob: f6d425efae0159d64087a490a87c2807e28b73d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use rusqlite::{params, Connection, Result, Error as SQLError};

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")?;

	// If the name field in the POST request is empty, user's name 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]
	)?;

	Ok(())
}

/// Get all threads from the SQLite database.
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 board, name, comment FROM threads")?;
	statement.query_map(params![], |row| {
		Ok(Thread {
			board: row.get(0)?,
			name: row.get(1)?,
			comment: row.get(2)?
		})
	})?.collect()
}

/// Get all boards from the SQLite database.
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")?;
	statement.query_map(params![], |row| {
		Ok(Board {
			tag: row.get(0)?,
			name: row.get(1)?,
			nsfw: row.get(2)?,
			disabled: row.get(3)?
		})
	})?.collect()
}

/// Get all threads from a specific board within the SQLite database.
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 = (?)")?;
	statement.query_map(params![board], |row| {
		Ok(Thread {
			board: row.get(0)?,
			name: row.get(1)?,
			comment: row.get(2)?
		})
	})?.collect()
}