feat: add comments
This commit is contained in:
parent
fed9c01370
commit
cc69935a88
8 changed files with 56 additions and 4 deletions
26
src/server/comments.rs
Normal file
26
src/server/comments.rs
Normal 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(),
|
||||
)
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue