chore: remove underdeveloped comments

image-uploads
Jef Roosens 2025-01-16 14:26:34 +01:00
parent 7b52b803c8
commit d0ecb9357a
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
11 changed files with 4 additions and 110 deletions

View File

@ -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<Self, rusqlite::Error> {
Ok(Self {
id: row.get(0)?,
plant_id: row.get(1)?,
comment: row.get(2)?,
})
}
}
impl NewComment {
pub fn insert(self, pool: &DbPool) -> Result<Comment, DbError> {
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)?)
}
}

View File

@ -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;

View File

@ -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<Vec<Comment>, DbError> {
let conn = pool.get()?;
let mut stmt = conn.prepare("select * from comments where plant_id = $1")?;
let comments: Result<Vec<_>, _> = stmt.query_map((self.id,), Comment::from_row)?.collect();
Ok(comments?)
}
pub fn events(&self, pool: &DbPool) -> Result<Vec<Event>, DbError> {
let conn = pool.get()?;
let mut stmt = conn.prepare("select * from events where plant_id = $1")?;

View File

@ -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"),
];

View File

@ -1,5 +0,0 @@
create table comments (
id integer primary key,
plant_id integer references plants (id),
comment text not null
);

View File

@ -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<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)?))
}

View File

@ -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<Path>) -> 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(),

View File

@ -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);

View File

@ -1,22 +0,0 @@
{% macro li(comment) %}
<li>{{ comment.comment }}</li>
{% endmacro li %}
{% macro list(comments) %}
<div id="comments">
<ul id="comments_ul">
{% for comment in comments %}
{{ self::li(comment=comment) }}
{% endfor %}
</ul>
</div>
{% endmacro list %}
{% macro form(plant_id, target="#comments > ul") %}
<form hx-post="/comments" hx-target="{{ target }}" hx-swap="beforeend">
<input type="hidden" id="plant_id" name="plant_id" value="{{ plant_id }}">
<label for="comment">Comment:</label>
<textarea id="comment" name="comment" rows=4></textarea></br>
<input type="submit">
</form>
{% endmacro form %}

View File

@ -1,2 +0,0 @@
{% import "components/comment.html" as comp_comment %}
{{ comp_comment::li(comment=comment) }}

View File

@ -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) }}
<h3>Events</h3>
{{ comp_event::list(events=events) }}
{{ comp_event::form(plant_id=plant.id) }}
<h3>Comments</h3>
{{ comp_comment::list(comments=comments) }}
{{ comp_comment::form(plant_id=plant.id) }}