calathea/src/server/comments.rs

22 lines
631 B
Rust

use axum::{extract::State, response::Html, routing::post, Form, Router};
use tera::Context;
use crate::db::{Comment, NewComment};
pub fn app() -> axum::Router<crate::Context> {
Router::new().route("/", post(post_comment))
}
async fn post_comment(
State(ctx): State<crate::Context>,
Form(comment): Form<NewComment>,
) -> super::Result<Html<String>> {
let comment = tokio::task::spawn_blocking(move || comment.insert(&ctx.pool))
.await
.unwrap()?;
let mut context = Context::new();
context.insert("comment", &comment);
Ok(Html(ctx.tera.render("updates/comment_li.html", &context)?))
}