refactor: clean up db stuff; start comments stuff

This commit is contained in:
Jef Roosens 2024-12-29 19:53:06 +01:00
parent f980115d45
commit fed9c01370
No known key found for this signature in database
GPG key ID: 21FD3D77D56BAF49
9 changed files with 174 additions and 118 deletions

View file

@ -10,7 +10,7 @@ use axum::{
use tera::Context;
use tower_http::set_header::SetResponseHeaderLayer;
use crate::db;
use crate::db::Plant;
const HX_REQUEST_HEADER: &str = "HX-Request";
const HX_HISTORY_RESTORE_HEADER: &str = "HX-History-Restore-Request";
@ -46,7 +46,7 @@ pub fn app(ctx: crate::Context) -> axum::Router {
}
async fn get_index(State(ctx): State<crate::Context>) -> Html<String> {
let plants = tokio::task::spawn_blocking(move || db::list_plants(&ctx.pool))
let plants = tokio::task::spawn_blocking(move || Plant::all(&ctx.pool))
.await
.unwrap()
.unwrap();

View file

@ -7,7 +7,7 @@ use axum::{
};
use tera::Context;
use crate::db;
use crate::db::{self, DbError, Plant};
use super::render_partial;
@ -21,15 +21,21 @@ pub fn app(ctx: crate::Context) -> axum::Router {
async fn get_plant_page(
State(ctx): State<crate::Context>,
headers: HeaderMap,
Path(plant_id): Path<u32>,
Path(plant_id): Path<i32>,
) -> Html<String> {
let plant = tokio::task::spawn_blocking(move || db::get_plant(&ctx.pool, plant_id))
.await
.unwrap()
.unwrap();
let (plant, comments) = tokio::task::spawn_blocking(move || {
let plant = Plant::with_id(&ctx.pool, plant_id)?.unwrap();
let comments = plant.comments(&ctx.pool)?;
Ok::<_, DbError>((plant, comments))
})
.await
.unwrap()
.unwrap();
let mut context = Context::new();
context.insert("plant", &plant);
context.insert("comments", &comments);
let tmpl = if render_partial(&headers) {
"partials/plant_info.html"
@ -44,7 +50,7 @@ async fn post_plant(
State(ctx): State<crate::Context>,
Form(plant): Form<db::NewPlant>,
) -> Html<String> {
let plant = tokio::task::spawn_blocking(move || db::insert_plant(&ctx.pool, &plant))
let plant = tokio::task::spawn_blocking(move || plant.insert(&ctx.pool))
.await
.unwrap()
.unwrap();