feat: add plant link to images page
parent
15b2ef3477
commit
18499631ea
|
@ -2,7 +2,7 @@ use chrono::NaiveDate;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::db::{schema::*, DbPool, DbResult, Pagination};
|
use crate::db::{schema::*, DbPool, DbResult, Pagination, Plant};
|
||||||
|
|
||||||
#[derive(Serialize, Queryable, Selectable)]
|
#[derive(Serialize, Queryable, Selectable)]
|
||||||
#[diesel(table_name = images)]
|
#[diesel(table_name = images)]
|
||||||
|
@ -62,8 +62,15 @@ impl Image {
|
||||||
.optional()?)
|
.optional()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn page(pool: &DbPool, page: Pagination, filter: ImageFilter) -> DbResult<Vec<Self>> {
|
pub fn page_with_plants(
|
||||||
let mut query = images::table.select(Self::as_select()).into_boxed();
|
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 {
|
if let Some(plant_id) = filter.plant_id {
|
||||||
query = query.filter(images::dsl::plant_id.eq(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>,
|
Query(filter): Query<db::ImageFilter>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
) -> super::Result<Html<String>> {
|
) -> super::Result<Html<String>> {
|
||||||
let images = tokio::task::spawn_blocking(move || db::Image::page(&ctx.pool, page, filter))
|
let images =
|
||||||
.await
|
tokio::task::spawn_blocking(move || db::Image::page_with_plants(&ctx.pool, page, filter))
|
||||||
.unwrap()?;
|
.await
|
||||||
|
.unwrap()?;
|
||||||
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.insert("images", &images);
|
context.insert("images", &images);
|
||||||
|
|
|
@ -35,3 +35,7 @@
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
{% endmacro %}
|
{% 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>
|
<h1>Images</h1>
|
||||||
{% for image in images %}
|
{% for image in images %}
|
||||||
<article>
|
<article>
|
||||||
<p>Date taken: {{ image.date_taken }}</p>
|
<p>Date taken: {{ image.0.date_taken }}</p>
|
||||||
<p>Note: {{ image.note }}</p>
|
<p>Note: {{ image.0.note }}</p>
|
||||||
<a href="/images/{{ image.id }}/original" target="_blank">
|
<p>Plant: {{ comp_plant::name(plant=image.1) }}</p>
|
||||||
<img src="/images/{{ image.id }}/thumb">
|
<a href="/images/{{ image.0.id }}/original" target="_blank">
|
||||||
|
<img src="/images/{{ image.0.id }}/thumb">
|
||||||
</a>
|
</a>
|
||||||
</article>
|
</article>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
Loading…
Reference in New Issue