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 {
|
|
||||||
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 })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_comments(&self) -> Result<Vec<Comment>> {
|
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
|
||||||
self.conn
|
use std::sync::{LockResult, Mutex};
|
||||||
.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<()> {
|
#[get("/comments")]
|
||||||
self.conn.execute(
|
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
|
||||||
"INSERT INTO comment (author, text) VALUES (?1, ?2)",
|
let db = &data.db.lock().unwrap();
|
||||||
params![&comment.author, &comment.text],
|
/*db.create_comment(&Comment {
|
||||||
)?;
|
author: Some("Elnu".to_string()),
|
||||||
Ok(())
|
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<()> {
|
#[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