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)]
|
mod database;
|
||||||
struct Comment {
|
pub use database::Database;
|
||||||
author: Option<String>, // null is Anonymous
|
|
||||||
text: String
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
conn: Connection
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Database {
|
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
|
||||||
fn new() -> Result<Self> {
|
use std::sync::{LockResult, Mutex};
|
||||||
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 })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_comments(&self) -> Result<Vec<Comment>> {
|
#[get("/comments")]
|
||||||
self.conn
|
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
|
||||||
.prepare("SELECT author, text FROM comment")?
|
let db = &data.db.lock().unwrap();
|
||||||
.query_map([], |row| {
|
/*db.create_comment(&Comment {
|
||||||
Ok(Comment {
|
author: Some("Elnu".to_string()),
|
||||||
author: row.get(0)?,
|
text: "Hello world".to_string()
|
||||||
text: row.get(1)?,
|
}).unwrap();*/
|
||||||
})
|
HttpResponse::Ok().body(format!("{}", db.get_comments().unwrap().get(0).unwrap_or(&Comment { author: None, text: "No comments yet!".to_string() }).text))
|
||||||
})?
|
|
||||||
.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(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
#[actix_web::main]
|
||||||
let db = Database::new()?;
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let comment = Comment {
|
let state = web::Data::new(AppState { db: Mutex::new(Database::new().unwrap()) });
|
||||||
author: Some("Elnu".to_string()), // None for anonymous
|
HttpServer::new(move || {
|
||||||
text: "This is a test comment by yours truly!".to_string(),
|
App::new()
|
||||||
};
|
.service(get_comments)
|
||||||
db.create_comment(&comment)?;
|
.app_data(state.clone())
|
||||||
for comment in db.get_comments()?.iter() {
|
})
|
||||||
println!("Found comment {:?}", comment);
|
.bind(("127.0.0.1", 8080))?
|
||||||
}
|
.run()
|
||||||
Ok(())
|
.await
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue