parent
59f3ce9ea5
commit
4cfa06f855
@ -0,0 +1,5 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Comment {
|
||||
pub author: Option<String>, // null is Anonymous
|
||||
pub text: String
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
use rusqlite::{params, Connection, Result};
|
||||
use crate::comment::Comment;
|
||||
|
||||
pub struct Database {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new() -> Result<Self> {
|
||||
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<Vec<Comment>> {
|
||||
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(())
|
||||
}
|
||||
}
|
@ -1,59 +1,31 @@
|
||||
use rusqlite::{params, Connection, Result};
|
||||
mod comment;
|
||||
pub use comment::Comment;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Comment {
|
||||
author: Option<String>, // null is Anonymous
|
||||
text: String
|
||||
}
|
||||
|
||||
struct Database {
|
||||
conn: Connection
|
||||
}
|
||||
mod database;
|
||||
pub use database::Database;
|
||||
|
||||
impl Database {
|
||||
fn new() -> Result<Self> {
|
||||
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 })
|
||||
}
|
||||
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
|
||||
use std::sync::{LockResult, Mutex};
|
||||
|
||||
fn get_comments(&self) -> Result<Vec<Comment>> {
|
||||
self.conn
|
||||
.prepare("SELECT author, text FROM comment")?
|
||||
.query_map([], |row| {
|
||||
Ok(Comment {
|
||||
author: row.get(0)?,
|
||||
text: row.get(1)?,
|
||||
})
|
||||
})?
|
||||
.collect()
|
||||
}
|
||||
|
||||
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<AppState>) -> 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
|
||||
}
|
||||
|
Loading…
Reference in new issue