main
Elnu 2 years ago
parent 0e270e18b9
commit 5b792cd7e5

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
// Master comment type that is stored in database // Master comment type that is stored in database
pub struct Comment { pub struct Comment {

@ -1,6 +1,6 @@
use rusqlite::{params, Connection, Result};
use crate::comment::{Comment, CommentSend}; use crate::comment::{Comment, CommentSend};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use rusqlite::{params, Connection, Result};
pub struct Database { pub struct Database {
conn: Connection, conn: Connection,
@ -17,7 +17,7 @@ impl Database {
text TEXT NOT NULL, text TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)", )",
params![] params![],
)?; )?;
Ok(Self { conn }) Ok(Self { conn })
} }
@ -40,7 +40,7 @@ impl Database {
})? })?
.collect() .collect()
} }
pub fn create_comment(&self, comment: &Comment) -> Result<()> { pub fn create_comment(&self, comment: &Comment) -> Result<()> {
self.conn.execute( self.conn.execute(
"INSERT INTO comment (author, email, text) VALUES (?1, ?2, ?3)", "INSERT INTO comment (author, email, text) VALUES (?1, ?2, ?3)",

@ -8,7 +8,7 @@ use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use std::sync::Mutex; use std::sync::Mutex;
struct AppState { struct AppState {
db: Mutex<Database> db: Mutex<Database>,
} }
#[get("/")] #[get("/")]
@ -34,8 +34,8 @@ async fn post_comment(data: web::Data<AppState>, bytes: web::Bytes) -> impl Resp
}; };
db.create_comment(&comment.to_master()).unwrap(); db.create_comment(&comment.to_master()).unwrap();
HttpResponse::Ok() HttpResponse::Ok()
}, }
Err(_) => HttpResponse::BadRequest().into() Err(_) => HttpResponse::BadRequest().into(),
} }
} }
@ -49,7 +49,7 @@ async fn main() -> Result<(), std::io::Error> {
.service(post_comment) .service(post_comment)
.app_data(state.clone()) .app_data(state.clone())
}) })
.bind(("127.0.0.1", 8080))? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

Loading…
Cancel
Save