chore: remove underdeveloped comments

This commit is contained in:
Jef Roosens 2025-01-16 14:26:34 +01:00
parent 7b52b803c8
commit d0ecb9357a
Signed by: Jef Roosens
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);