From d0ecb9357aba5d28e8d0d3d5a9a44c93303e0b5a Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 16 Jan 2025 14:26:34 +0100 Subject: [PATCH] chore: remove underdeveloped comments --- src/db/comment.rs | 37 ------------------------------- src/db/mod.rs | 2 -- src/db/plant.rs | 10 +-------- src/main.rs | 3 +-- src/migrations/002_comments.sql | 5 ----- src/server/comments.rs | 21 ------------------ src/server/mod.rs | 2 -- src/server/plants.rs | 6 ++--- templates/components/comment.html | 22 ------------------ templates/updates/comment_li.html | 2 -- templates/views/plant.html | 4 ---- 11 files changed, 4 insertions(+), 110 deletions(-) delete mode 100644 src/db/comment.rs delete mode 100644 src/migrations/002_comments.sql delete mode 100644 src/server/comments.rs delete mode 100644 templates/components/comment.html delete mode 100644 templates/updates/comment_li.html diff --git a/src/db/comment.rs b/src/db/comment.rs deleted file mode 100644 index 1530922..0000000 --- a/src/db/comment.rs +++ /dev/null @@ -1,37 +0,0 @@ -use r2d2_sqlite::rusqlite::{self, Row}; -use serde::{Deserialize, Serialize}; - -use super::{DbError, DbPool}; - -#[derive(Serialize, Deserialize)] -pub struct Comment { - id: i64, - plant_id: i64, - comment: String, -} - -#[derive(Deserialize)] -pub struct NewComment { - plant_id: i64, - comment: String, -} - -impl Comment { - pub fn from_row(row: &Row<'_>) -> Result { - Ok(Self { - id: row.get(0)?, - plant_id: row.get(1)?, - comment: row.get(2)?, - }) - } -} - -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 19a05b5..17e00c8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,4 +1,3 @@ -mod comment; mod event; mod models; mod plant; @@ -10,7 +9,6 @@ use r2d2_sqlite::{rusqlite, SqliteConnectionManager}; use std::{error::Error, fmt}; -pub use comment::{Comment, NewComment}; pub use event::{Event, EventType, NewEvent, EVENT_TYPES}; pub use plant::{NewPlant, Plant}; pub use session::Session; diff --git a/src/db/plant.rs b/src/db/plant.rs index 3b55c1d..1ee9f75 100644 --- a/src/db/plant.rs +++ b/src/db/plant.rs @@ -1,7 +1,7 @@ use r2d2_sqlite::rusqlite::{self, Row}; use serde::{Deserialize, Serialize}; -use super::{Comment, DbError, DbPool, Event}; +use super::{DbError, DbPool, Event}; #[derive(Serialize)] pub struct Plant { @@ -48,14 +48,6 @@ impl Plant { } } - pub fn comments(&self, pool: &DbPool) -> Result, DbError> { - let conn = pool.get()?; - 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?) - } - pub fn events(&self, pool: &DbPool) -> Result, DbError> { let conn = pool.get()?; let mut stmt = conn.prepare("select * from events where plant_id = $1")?; diff --git a/src/main.rs b/src/main.rs index 1ac2fb1..4ffd7e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,9 @@ use r2d2_sqlite::SqliteConnectionManager; use tera::Tera; use tower_http::compression::CompressionLayer; -const MIGRATIONS: [&str; 5] = [ +const MIGRATIONS: [&str; 4] = [ include_str!("migrations/000_initial.sql"), include_str!("migrations/001_plants.sql"), - include_str!("migrations/002_comments.sql"), include_str!("migrations/003_events.sql"), include_str!("migrations/004_auth.sql"), ]; diff --git a/src/migrations/002_comments.sql b/src/migrations/002_comments.sql deleted file mode 100644 index 1df20f8..0000000 --- a/src/migrations/002_comments.sql +++ /dev/null @@ -1,5 +0,0 @@ -create table comments ( - id integer primary key, - plant_id integer references plants (id), - comment text not null -); diff --git a/src/server/comments.rs b/src/server/comments.rs deleted file mode 100644 index fb2510a..0000000 --- a/src/server/comments.rs +++ /dev/null @@ -1,21 +0,0 @@ -use axum::{extract::State, response::Html, routing::post, Form, Router}; -use tera::Context; - -use crate::db::{Comment, NewComment}; - -pub fn app() -> axum::Router { - Router::new().route("/", post(post_comment)) -} - -async fn post_comment( - State(ctx): State, - Form(comment): Form, -) -> super::Result> { - 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)?)) -} diff --git a/src/server/mod.rs b/src/server/mod.rs index 91f4396..cd66418 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,5 +1,4 @@ mod auth; -mod comments; mod error; mod events; mod plants; @@ -55,7 +54,6 @@ pub fn render_view( pub fn app(ctx: crate::Context, static_dir: impl AsRef) -> axum::Router { let router = Router::new() .nest("/plants", plants::app()) - .nest("/comments", comments::app()) .nest("/events", events::app()) .layer(middleware::from_fn_with_state( ctx.clone(), diff --git a/src/server/plants.rs b/src/server/plants.rs index ef93579..223a9c8 100644 --- a/src/server/plants.rs +++ b/src/server/plants.rs @@ -26,10 +26,9 @@ async fn get_plant_page( let plant = Plant::by_id(&ctx.pool, plant_id)?; if let Some(plant) = plant { - let comments = plant.comments(&ctx.pool)?; let events = plant.events(&ctx.pool)?; - Ok::<_, DbError>(Some((plant, comments, events))) + Ok::<_, DbError>(Some((plant, events))) } else { Ok(None) } @@ -38,10 +37,9 @@ async fn get_plant_page( .unwrap()?; match res { - Some((plant, comments, events)) => { + Some((plant, events)) => { let mut context = Context::new(); context.insert("plant", &plant); - context.insert("comments", &comments); context.insert("events", &events); context.insert("event_types", &db::EVENT_TYPES); diff --git a/templates/components/comment.html b/templates/components/comment.html deleted file mode 100644 index 1b83844..0000000 --- a/templates/components/comment.html +++ /dev/null @@ -1,22 +0,0 @@ -{% macro li(comment) %} -
  • {{ comment.comment }}
  • -{% endmacro li %} - -{% macro list(comments) %} -
    -
      - {% for comment in comments %} - {{ self::li(comment=comment) }} - {% endfor %} -
    -
    -{% endmacro list %} - -{% macro form(plant_id, target="#comments > ul") %} -
    - - -
    - -
    -{% endmacro form %} diff --git a/templates/updates/comment_li.html b/templates/updates/comment_li.html deleted file mode 100644 index c8d15af..0000000 --- a/templates/updates/comment_li.html +++ /dev/null @@ -1,2 +0,0 @@ -{% import "components/comment.html" as comp_comment %} -{{ comp_comment::li(comment=comment) }} diff --git a/templates/views/plant.html b/templates/views/plant.html index 2968584..7510068 100644 --- a/templates/views/plant.html +++ b/templates/views/plant.html @@ -1,11 +1,7 @@ {% import "components/event.html" as comp_event %} {% import "components/plant.html" as comp_plant %} -{% import "components/comment.html" as comp_comment %} {{ comp_plant::info(plant=plant) }}

    Events

    {{ comp_event::list(events=events) }} {{ comp_event::form(plant_id=plant.id) }} -

    Comments

    -{{ comp_comment::list(comments=comments) }} -{{ comp_comment::form(plant_id=plant.id) }}