Create working comments API response

This commit is contained in:
Elnu 2022-07-08 13:00:25 -07:00
parent 61234e9c07
commit 91186e5f43
4 changed files with 23 additions and 8 deletions

View file

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

View file

@ -14,16 +14,17 @@ struct AppState {
#[get("/comments")]
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
let db = &data.db.lock().unwrap();
/*db.create_comment(&Comment {
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))
HttpResponse::Ok().json(&db.get_comments().unwrap())
}
#[actix_web::main]
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 || {
App::new()
.service(get_comments)