Add timestamps, remove dead code

main
Elnu 2 years ago
parent 82e70178d5
commit 0e270e18b9

59
Cargo.lock generated

@ -165,7 +165,7 @@ dependencies = [
"serde_urlencoded", "serde_urlencoded",
"smallvec", "smallvec",
"socket2", "socket2",
"time", "time 0.3.11",
"url", "url",
] ]
@ -300,6 +300,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time 0.1.44",
"winapi",
]
[[package]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.4.0" version = "0.4.0"
@ -313,7 +326,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05" checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05"
dependencies = [ dependencies = [
"percent-encoding", "percent-encoding",
"time", "time 0.3.11",
"version_check", "version_check",
] ]
@ -469,7 +482,7 @@ checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
] ]
[[package]] [[package]]
@ -689,10 +702,29 @@ checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys", "windows-sys",
] ]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.13.1" version = "1.13.1"
@ -858,6 +890,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85127183a999f7db96d1a976a309eebbfb6ea3b0b400ddd8340190129de6eb7a" checksum = "85127183a999f7db96d1a976a309eebbfb6ea3b0b400ddd8340190129de6eb7a"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"chrono",
"fallible-iterator", "fallible-iterator",
"fallible-streaming-iterator", "fallible-streaming-iterator",
"hashlink", "hashlink",
@ -983,6 +1016,7 @@ name = "soudan"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"chrono",
"md5", "md5",
"rusqlite", "rusqlite",
"serde", "serde",
@ -1001,6 +1035,17 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.11" version = "0.3.11"
@ -1153,6 +1198,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

@ -7,8 +7,9 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
rusqlite = "0.27.0" rusqlite = { version = "0.27.0", features = ["chrono"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
validator = "0.15.0" validator = "0.15.0"
md5 = "0.7.0" md5 = "0.7.0"
chrono = "0.4.19"

@ -1,11 +1,12 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use chrono::NaiveDateTime;
// Master comment type that is stored in database // Master comment type that is stored in database
#[derive(Deserialize, Serialize)]
pub struct Comment { pub struct Comment {
pub author: Option<String>, // None/null is Anonymous pub author: Option<String>, // None/null is Anonymous
pub email: Option<String>, pub email: Option<String>,
pub text: String pub text: String,
pub timestamp: Option<NaiveDateTime>,
} }
impl Comment { impl Comment {
@ -17,6 +18,7 @@ impl Comment {
None => None, None => None,
}, },
text: self.text.clone(), text: self.text.clone(),
timestamp: self.timestamp.unwrap().timestamp(),
} }
} }
} }
@ -26,7 +28,8 @@ impl Comment {
pub struct CommentSend { pub struct CommentSend {
pub author: Option<String>, pub author: Option<String>,
pub gravatar: Option<String>, pub gravatar: Option<String>,
pub text: String pub text: String,
pub timestamp: i64,
} }
// Comment type received containing new comment data // Comment type received containing new comment data
@ -43,6 +46,7 @@ impl CommentReceive {
author: self.author.clone(), author: self.author.clone(),
email: self.email.clone(), email: self.email.clone(),
text: self.text.clone(), text: self.text.clone(),
timestamp: None,
} }
} }
} }

@ -1,5 +1,6 @@
use rusqlite::{params, Connection, Result}; use rusqlite::{params, Connection, Result};
use crate::comment::{Comment, CommentSend}; use crate::comment::{Comment, CommentSend};
use chrono::NaiveDateTime;
pub struct Database { pub struct Database {
conn: Connection, conn: Connection,
@ -10,33 +11,23 @@ impl Database {
let conn = Connection::open_in_memory()?; let conn = Connection::open_in_memory()?;
conn.execute( conn.execute(
"CREATE TABLE comment ( "CREATE TABLE comment (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
email TEXT, email TEXT,
author TEXT, author TEXT,
text TEXT NOT NULL text TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)", )",
params![] params![]
)?; )?;
Ok(Self { conn }) Ok(Self { conn })
} }
pub fn get_comments(&self) -> Result<Vec<Comment>> {
self.conn
.prepare("SELECT author, email, text FROM comment")?
.query_map([], |row| {
Ok(Comment {
author: row.get(0)?,
email: row.get(1)?,
text: row.get(2)?,
})
})?
.collect()
}
pub fn get_send_comments(&self) -> Result<Vec<CommentSend>> { pub fn get_send_comments(&self) -> Result<Vec<CommentSend>> {
self.conn self.conn
.prepare("SELECT author, email, text FROM comment")? .prepare("SELECT author, email, text, timestamp FROM comment")?
.query_map([], |row| { .query_map([], |row| {
let timestamp: NaiveDateTime = row.get(3)?;
let timestamp = timestamp.timestamp();
Ok(CommentSend { Ok(CommentSend {
author: row.get(0)?, author: row.get(0)?,
gravatar: match row.get::<usize, Option<String>>(1)? { gravatar: match row.get::<usize, Option<String>>(1)? {
@ -44,6 +35,7 @@ impl Database {
None => None, None => None,
}, },
text: row.get(2)?, text: row.get(2)?,
timestamp: timestamp,
}) })
})? })?
.collect() .collect()

@ -13,7 +13,10 @@ struct AppState {
#[get("/")] #[get("/")]
async fn get_comments(data: web::Data<AppState>) -> impl Responder { async fn get_comments(data: web::Data<AppState>) -> impl Responder {
let db = &data.db.lock().unwrap(); let db = match data.db.lock() {
Ok(db) => db,
Err(_) => return HttpResponse::InternalServerError().into(),
};
HttpResponse::Ok().json(&db.get_send_comments().unwrap()) HttpResponse::Ok().json(&db.get_send_comments().unwrap())
} }

Loading…
Cancel
Save