feat: add plant link to images page
parent
15b2ef3477
commit
18499631ea
|
@ -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<Vec<Self>> {
|
||||
let mut query = images::table.select(Self::as_select()).into_boxed();
|
||||
pub fn page_with_plants(
|
||||
pool: &DbPool,
|
||||
page: Pagination,
|
||||
filter: ImageFilter,
|
||||
) -> DbResult<Vec<(Self, Plant)>> {
|
||||
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));
|
||||
|
|
|
@ -66,9 +66,10 @@ async fn get_images(
|
|||
Query(filter): Query<db::ImageFilter>,
|
||||
headers: HeaderMap,
|
||||
) -> super::Result<Html<String>> {
|
||||
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);
|
||||
|
|
|
@ -35,3 +35,7 @@
|
|||
<input type="submit">
|
||||
</form>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro name(plant) %}
|
||||
<a hx-get="/plants/{{ plant.id }}" hx-target="#content" hx-push-url="true">{{ plant.name }} (<em>{{ plant.species }}</em>)</a>
|
||||
{% endmacro %}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
{% import "components/plant.html" as comp_plant %}
|
||||
|
||||
<h1>Images</h1>
|
||||
{% for image in images %}
|
||||
<article>
|
||||
<p>Date taken: {{ image.date_taken }}</p>
|
||||
<p>Note: {{ image.note }}</p>
|
||||
<a href="/images/{{ image.id }}/original" target="_blank">
|
||||
<img src="/images/{{ image.id }}/thumb">
|
||||
<p>Date taken: {{ image.0.date_taken }}</p>
|
||||
<p>Note: {{ image.0.note }}</p>
|
||||
<p>Plant: {{ comp_plant::name(plant=image.1) }}</p>
|
||||
<a href="/images/{{ image.0.id }}/original" target="_blank">
|
||||
<img src="/images/{{ image.0.id }}/thumb">
|
||||
</a>
|
||||
</article>
|
||||
{% endfor %}
|
||||
|
|
Loading…
Reference in New Issue