feat: add comments

This commit is contained in:
Jef Roosens 2024-12-29 20:11:38 +01:00
parent fed9c01370
commit cc69935a88
No known key found for this signature in database
GPG key ID: 21FD3D77D56BAF49
8 changed files with 56 additions and 4 deletions

26
src/server/comments.rs Normal file
View file

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

View file

@ -1,3 +1,4 @@
mod comments;
mod plants;
use axum::{
@ -29,7 +30,8 @@ pub fn app(ctx: crate::Context) -> axum::Router {
let mut router = Router::new()
.route("/", get(get_index))
.with_state(ctx.clone())
.nest("/plants", plants::app(ctx.clone()));
.nest("/plants", plants::app(ctx.clone()))
.nest("/comments", comments::app(ctx.clone()));
for (name, content) in crate::STATIC_FILES {
router = router.route(&format!("/static/{}", name), get(content))