From cc69935a88349c0ef01eb7e8e937dd65898c165e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 29 Dec 2024 20:11:38 +0100 Subject: [PATCH] feat: add comments --- src/db/comment.rs | 18 ++++++++++++++++++ src/db/mod.rs | 2 +- src/db/plant.rs | 2 +- src/main.rs | 4 ++++ src/server/comments.rs | 26 ++++++++++++++++++++++++++ src/server/mod.rs | 4 +++- src/templates/partials/comment_li.html | 1 + src/templates/partials/plant_info.html | 3 ++- 8 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/server/comments.rs create mode 100644 src/templates/partials/comment_li.html diff --git a/src/db/comment.rs b/src/db/comment.rs index 876be31..f865584 100644 --- a/src/db/comment.rs +++ b/src/db/comment.rs @@ -1,6 +1,8 @@ use r2d2_sqlite::rusqlite::{self, Row}; use serde::{Deserialize, Serialize}; +use super::{DbError, DbPool}; + #[derive(Serialize, Deserialize)] pub struct Comment { id: i32, @@ -8,6 +10,12 @@ pub struct Comment { comment: String, } +#[derive(Deserialize)] +pub struct NewComment { + plant_id: i32, + comment: String, +} + impl Comment { pub fn from_row(row: &Row<'_>) -> Result { Ok(Self { @@ -17,3 +25,13 @@ impl Comment { }) } } + +impl NewComment { + pub fn insert(self, pool: &DbPool) -> Result { + let conn = pool.get()?; + + let mut stmt = + conn.prepare("insert into comments (plant_id, comment) values ($1, $2) returning *")?; + Ok(stmt.query_row((self.plant_id, self.comment), Comment::from_row)?) + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index 94a5e99..69a1a13 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -5,7 +5,7 @@ use r2d2_sqlite::{rusqlite, SqliteConnectionManager}; use std::{error::Error, fmt}; -pub use comment::Comment; +pub use comment::{Comment, NewComment}; pub use plant::{NewPlant, Plant}; pub type DbPool = r2d2::Pool; diff --git a/src/db/plant.rs b/src/db/plant.rs index e7119ae..1b4faed 100644 --- a/src/db/plant.rs +++ b/src/db/plant.rs @@ -43,7 +43,7 @@ impl Plant { pub fn comments(&self, pool: &DbPool) -> Result, DbError> { let conn = pool.get()?; - let mut stmt = conn.prepare("select * from plant_comments where plant_id = $1")?; + let mut stmt = conn.prepare("select * from comments where plant_id = $1")?; let comments: Result, _> = stmt.query_map((self.id,), Comment::from_row)?.collect(); Ok(comments?) diff --git a/src/main.rs b/src/main.rs index 8a87b6c..09d541c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,10 @@ fn load_templates() -> Tera { "partials/plant_info.html", include_str!("templates/partials/plant_info.html"), ), + ( + "partials/comment_li.html", + include_str!("templates/partials/comment_li.html"), + ), ]) .unwrap(); diff --git a/src/server/comments.rs b/src/server/comments.rs new file mode 100644 index 0000000..ad41a44 --- /dev/null +++ b/src/server/comments.rs @@ -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, + Form(comment): Form, +) -> Html { + 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(), + ) +} diff --git a/src/server/mod.rs b/src/server/mod.rs index dff6395..02c77f0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -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)) diff --git a/src/templates/partials/comment_li.html b/src/templates/partials/comment_li.html new file mode 100644 index 0000000..af278d6 --- /dev/null +++ b/src/templates/partials/comment_li.html @@ -0,0 +1 @@ +
  • {{ comment.comment }}
  • diff --git a/src/templates/partials/plant_info.html b/src/templates/partials/plant_info.html index dd10fab..c42a65d 100644 --- a/src/templates/partials/plant_info.html +++ b/src/templates/partials/plant_info.html @@ -12,7 +12,8 @@ {% endfor %} -
    + +