diff --git a/src/comment.rs b/src/comment.rs new file mode 100644 index 0000000..771cfef --- /dev/null +++ b/src/comment.rs @@ -0,0 +1,5 @@ +#[derive(Debug)] +pub struct Comment { + pub author: Option, // null is Anonymous + pub text: String +} diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..93a06a1 --- /dev/null +++ b/src/database.rs @@ -0,0 +1,41 @@ +use rusqlite::{params, Connection, Result}; +use crate::comment::Comment; + +pub struct Database { + conn: Connection, +} + +impl Database { + pub fn new() -> Result { + let conn = Connection::open_in_memory()?; + conn.execute( + "CREATE TABLE comment ( + id INTEGER PRIMARY KEY, + author TEXT, + text TEXT NOT NULL + )", + params![] + )?; + Ok(Self { conn }) + } + + pub fn get_comments(&self) -> Result> { + self.conn + .prepare("SELECT author, text FROM comment")? + .query_map([], |row| { + Ok(Comment { + author: row.get(0)?, + text: row.get(1)?, + }) + })? + .collect() + } + + pub fn create_comment(&self, comment: &Comment) -> Result<()> { + self.conn.execute( + "INSERT INTO comment (author, text) VALUES (?1, ?2)", + params![&comment.author, &comment.text], + )?; + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 135e30c..a85ed70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,59 +1,31 @@ -use rusqlite::{params, Connection, Result}; +mod comment; +pub use comment::Comment; -#[derive(Debug)] -struct Comment { - author: Option, // null is Anonymous - text: String -} - -struct Database { - conn: Connection -} - -impl Database { - fn new() -> Result { - let conn = Connection::open_in_memory()?; - conn.execute( - "CREATE TABLE comment ( - id INTEGER PRIMARY KEY, - author TEXT, - text TEXT NOT NULL - )", - params![] - )?; - Ok(Self { conn }) - } +mod database; +pub use database::Database; - fn get_comments(&self) -> Result> { - self.conn - .prepare("SELECT author, text FROM comment")? - .query_map([], |row| { - Ok(Comment { - author: row.get(0)?, - text: row.get(1)?, - }) - })? - .collect() - } +use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; +use std::sync::{LockResult, Mutex}; - fn create_comment(&self, comment: &Comment) -> Result<()> { - self.conn.execute( - "INSERT INTO comment (author, text) VALUES (?1, ?2)", - params![&comment.author, &comment.text], - )?; - Ok(()) - } +#[get("/comments")] +async fn get_comments(data: web::Data) -> impl Responder { + let db = &data.db.lock().unwrap(); + /*db.create_comment(&Comment { + author: Some("Elnu".to_string()), + text: "Hello world".to_string() + }).unwrap();*/ + HttpResponse::Ok().body(format!("{}", db.get_comments().unwrap().get(0).unwrap_or(&Comment { author: None, text: "No comments yet!".to_string() }).text)) } -fn main() -> Result<()> { - let db = Database::new()?; - let comment = Comment { - author: Some("Elnu".to_string()), // None for anonymous - text: "This is a test comment by yours truly!".to_string(), - }; - db.create_comment(&comment)?; - for comment in db.get_comments()?.iter() { - println!("Found comment {:?}", comment); - } - Ok(()) +#[actix_web::main] +async fn main() -> Result<(), std::io::Error> { + let state = web::Data::new(AppState { db: Mutex::new(Database::new().unwrap()) }); + HttpServer::new(move || { + App::new() + .service(get_comments) + .app_data(state.clone()) + }) + .bind(("127.0.0.1", 8080))? + .run() + .await }