Create working comments API response

main
Elnu 2 years ago
parent 61234e9c07
commit 91186e5f43

14
Cargo.lock generated

@ -886,6 +886,20 @@ name = "serde"
version = "1.0.138" version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47" checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"

@ -8,5 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
rusqlite = "0.27.0" rusqlite = "0.27.0"
serde = "1" serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"

@ -1,4 +1,4 @@
#[derive(Debug)] #[derive(Debug, serde::Serialize)]
pub struct Comment { pub struct Comment {
pub author: Option<String>, // null is Anonymous pub author: Option<String>, // null is Anonymous
pub text: String pub text: String

@ -14,16 +14,17 @@ struct AppState {
#[get("/comments")] #[get("/comments")]
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 = &data.db.lock().unwrap();
/*db.create_comment(&Comment { HttpResponse::Ok().json(&db.get_comments().unwrap())
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))
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
let state = web::Data::new(AppState { db: Mutex::new(Database::new().unwrap()) }); let db = Database::new().unwrap();
db.create_comment(&Comment {
author: None,
text: "This is anonymous test comment!".to_string(),
}).unwrap();
let state = web::Data::new(AppState { db: Mutex::new(db) });
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.service(get_comments) .service(get_comments)

Loading…
Cancel
Save