diff --git a/Cargo.lock b/Cargo.lock index 4e42bd5..85ae352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,6 +351,7 @@ dependencies = [ "diesel_migrations", "futures", "image", + "libsqlite3-sys", "mime", "rand", "serde", @@ -1088,6 +1089,7 @@ version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" dependencies = [ + "cc", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index d26e04f..eb5d537 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,12 @@ chrono = { version = "0.4.39", features = ["serde"] } clap = { version = "4.5.26", features = ["derive", "env"] } diesel = { version = "2.2.6", features = ["sqlite", "returning_clauses_for_sqlite_3_35", "r2d2", "chrono"] } diesel_migrations = { version = "2.2.0", features = ["sqlite"] } +# Forces diesel to include a bundled version of Sqlite +libsqlite3-sys = {version = ">=0.17.2, <0.31.0", features = ["bundled"]} futures = "0.3.31" image = { version = "0.25.5", default-features = false, features = ["gif", "jpeg", "png"] } mime = "0.3.17" rand = "0.8.5" -# this dependency is needed soly because the r2d2_sqlite crate doesn't export -# the 'chrono' feature flag serde = { version = "1.0.217", features = ["derive"] } tera = "1.20.0" tokio = { version = "1.42.0", features = ["full"] } diff --git a/src/db/models/image.rs b/src/db/models/image.rs index 159f1a0..539537f 100644 --- a/src/db/models/image.rs +++ b/src/db/models/image.rs @@ -2,7 +2,7 @@ use chrono::NaiveDate; use diesel::prelude::*; use serde::{Deserialize, Serialize}; -use crate::db::{schema::*, DbPool, DbResult, Pagination}; +use crate::db::{schema::*, DbPool, DbResult, Pagination, Plant}; #[derive(Serialize, Queryable, Selectable)] #[diesel(table_name = images)] @@ -62,8 +62,15 @@ impl Image { .optional()?) } - pub fn page(pool: &DbPool, page: Pagination, filter: ImageFilter) -> DbResult> { - let mut query = images::table.select(Self::as_select()).into_boxed(); + pub fn page_with_plants( + pool: &DbPool, + page: Pagination, + filter: ImageFilter, + ) -> DbResult> { + let mut query = images::table + .inner_join(plants::table) + .select((Self::as_select(), Plant::as_select())) + .into_boxed(); if let Some(plant_id) = filter.plant_id { query = query.filter(images::dsl::plant_id.eq(plant_id)); diff --git a/src/server/images.rs b/src/server/images.rs index 957f587..15e8894 100644 --- a/src/server/images.rs +++ b/src/server/images.rs @@ -66,9 +66,10 @@ async fn get_images( Query(filter): Query, headers: HeaderMap, ) -> super::Result> { - let images = tokio::task::spawn_blocking(move || db::Image::page(&ctx.pool, page, filter)) - .await - .unwrap()?; + let images = + tokio::task::spawn_blocking(move || db::Image::page_with_plants(&ctx.pool, page, filter)) + .await + .unwrap()?; let mut context = Context::new(); context.insert("images", &images); diff --git a/templates/components/plant.html b/templates/components/plant.html index 32432b0..a654bb5 100644 --- a/templates/components/plant.html +++ b/templates/components/plant.html @@ -35,3 +35,7 @@ {% endmacro %} + +{% macro name(plant) %} +{{ plant.name }} ({{ plant.species }}) +{% endmacro %} diff --git a/templates/views/images.html b/templates/views/images.html index 972c23a..d70cca0 100644 --- a/templates/views/images.html +++ b/templates/views/images.html @@ -1,10 +1,13 @@ +{% import "components/plant.html" as comp_plant %} +

Images

{% for image in images %} {% endfor %}