DRY.
This commit is contained in:
parent
2460e26e64
commit
3c3ecbbfbb
@ -360,30 +360,11 @@ fn tag_one<'mw>(
|
|||||||
let c: &PgConnection = &req.db_conn();
|
let c: &PgConnection = &req.db_conn();
|
||||||
if let Ok(tag) = tags.filter(slug.eq(tslug)).first::<Tag>(c) {
|
if let Ok(tag) = tags.filter(slug.eq(tslug)).first::<Tag>(c) {
|
||||||
use schema::photo_tags::dsl::{photo_id, photo_tags, tag_id};
|
use schema::photo_tags::dsl::{photo_id, photo_tags, tag_id};
|
||||||
use schema::photos::dsl::{date, id};
|
use schema::photos::dsl::id;
|
||||||
let photos = Photo::query(req.authorized_user().is_some()).filter(
|
let photos = Photo::query(req.authorized_user().is_some()).filter(
|
||||||
id.eq_any(photo_tags.select(photo_id).filter(tag_id.eq(tag.id))),
|
id.eq_any(photo_tags.select(photo_id).filter(tag_id.eq(tag.id))),
|
||||||
);
|
);
|
||||||
let photos = if let Some(from_date) = query_date(req, "from") {
|
let links = links_by_time(req, photos);
|
||||||
photos.filter(date.ge(from_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = if let Some(to_date) = query_date(req, "to") {
|
|
||||||
photos.filter(date.le(to_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = photos.order(date.desc().nulls_last()).load(c).unwrap();
|
|
||||||
let links = if let Some(groups) = split_to_groups(&photos) {
|
|
||||||
let path = req.path_without_query().unwrap_or("/");
|
|
||||||
groups
|
|
||||||
.iter()
|
|
||||||
.map(|g| PhotoLink::for_group(g, path))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
} else {
|
|
||||||
photos.iter().map(PhotoLink::from).collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
return res.ok(|o| templates::tag(o, req, &links, &tag));
|
return res.ok(|o| templates::tag(o, req, &links, &tag));
|
||||||
}
|
}
|
||||||
res.not_found("Not a tag")
|
res.not_found("Not a tag")
|
||||||
@ -437,31 +418,12 @@ fn place_one<'mw>(
|
|||||||
let c: &PgConnection = &req.db_conn();
|
let c: &PgConnection = &req.db_conn();
|
||||||
if let Ok(place) = places.filter(slug.eq(tslug)).first::<Place>(c) {
|
if let Ok(place) = places.filter(slug.eq(tslug)).first::<Place>(c) {
|
||||||
use schema::photo_places::dsl::{photo_id, photo_places, place_id};
|
use schema::photo_places::dsl::{photo_id, photo_places, place_id};
|
||||||
use schema::photos::dsl::{date, id};
|
use schema::photos::dsl::id;
|
||||||
let photos =
|
let photos =
|
||||||
Photo::query(req.authorized_user().is_some()).filter(id.eq_any(
|
Photo::query(req.authorized_user().is_some()).filter(id.eq_any(
|
||||||
photo_places.select(photo_id).filter(place_id.eq(place.id)),
|
photo_places.select(photo_id).filter(place_id.eq(place.id)),
|
||||||
));
|
));
|
||||||
let photos = if let Some(from_date) = query_date(req, "from") {
|
let links = links_by_time(req, photos);
|
||||||
photos.filter(date.ge(from_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = if let Some(to_date) = query_date(req, "to") {
|
|
||||||
photos.filter(date.le(to_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = photos.order(date.desc().nulls_last()).load(c).unwrap();
|
|
||||||
let links = if let Some(groups) = split_to_groups(&photos) {
|
|
||||||
let path = req.path_without_query().unwrap_or("/");
|
|
||||||
groups
|
|
||||||
.iter()
|
|
||||||
.map(|g| PhotoLink::for_group(g, path))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
} else {
|
|
||||||
photos.iter().map(PhotoLink::from).collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
return res.ok(|o| templates::place(o, req, &links, &place));
|
return res.ok(|o| templates::place(o, req, &links, &place));
|
||||||
}
|
}
|
||||||
res.not_found("Not a place")
|
res.not_found("Not a place")
|
||||||
@ -501,7 +463,7 @@ fn person_one<'mw>(
|
|||||||
let c: &PgConnection = &req.db_conn();
|
let c: &PgConnection = &req.db_conn();
|
||||||
if let Ok(person) = people.filter(slug.eq(tslug)).first::<Person>(c) {
|
if let Ok(person) = people.filter(slug.eq(tslug)).first::<Person>(c) {
|
||||||
use schema::photo_people::dsl::{person_id, photo_id, photo_people};
|
use schema::photo_people::dsl::{person_id, photo_id, photo_people};
|
||||||
use schema::photos::dsl::{date, id};
|
use schema::photos::dsl::id;
|
||||||
let photos = Photo::query(req.authorized_user().is_some()).filter(
|
let photos = Photo::query(req.authorized_user().is_some()).filter(
|
||||||
id.eq_any(
|
id.eq_any(
|
||||||
photo_people
|
photo_people
|
||||||
@ -509,29 +471,7 @@ fn person_one<'mw>(
|
|||||||
.filter(person_id.eq(person.id)),
|
.filter(person_id.eq(person.id)),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
let photos = if let Some(from_date) = query_date(req, "from") {
|
let links = links_by_time(req, photos);
|
||||||
photos.filter(date.ge(from_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = if let Some(to_date) = query_date(req, "to") {
|
|
||||||
photos.filter(date.le(to_date))
|
|
||||||
} else {
|
|
||||||
photos
|
|
||||||
};
|
|
||||||
let photos = photos
|
|
||||||
.order(date.desc().nulls_last())
|
|
||||||
.load::<Photo>(c)
|
|
||||||
.unwrap();
|
|
||||||
let links = if let Some(groups) = split_to_groups(&photos) {
|
|
||||||
let path = req.path_without_query().unwrap_or("/");
|
|
||||||
groups
|
|
||||||
.iter()
|
|
||||||
.map(|g| PhotoLink::for_group(g, path))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
} else {
|
|
||||||
photos.iter().map(PhotoLink::from).collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
return res.ok(|o| templates::person(o, req, &links, &person));
|
return res.ok(|o| templates::person(o, req, &links, &person));
|
||||||
}
|
}
|
||||||
res.not_found("Not a person")
|
res.not_found("Not a person")
|
||||||
|
@ -1,4 +1,39 @@
|
|||||||
|
use super::PhotoLink;
|
||||||
|
use super::views_by_date::query_date;
|
||||||
|
use diesel::pg::{Pg, PgConnection};
|
||||||
|
use diesel::prelude::*;
|
||||||
use models::Photo;
|
use models::Photo;
|
||||||
|
use nickel::Request;
|
||||||
|
use nickel_diesel::DieselRequestExtensions;
|
||||||
|
use schema::photos;
|
||||||
|
|
||||||
|
pub fn links_by_time<'a>(
|
||||||
|
req: &mut Request,
|
||||||
|
photos: photos::BoxedQuery<'a, Pg>,
|
||||||
|
) -> Vec<PhotoLink> {
|
||||||
|
let c: &PgConnection = &req.db_conn();
|
||||||
|
use schema::photos::dsl::date;
|
||||||
|
let photos = if let Some(from_date) = query_date(req, "from") {
|
||||||
|
photos.filter(date.ge(from_date))
|
||||||
|
} else {
|
||||||
|
photos
|
||||||
|
};
|
||||||
|
let photos = if let Some(to_date) = query_date(req, "to") {
|
||||||
|
photos.filter(date.le(to_date))
|
||||||
|
} else {
|
||||||
|
photos
|
||||||
|
};
|
||||||
|
let photos = photos.order(date.desc().nulls_last()).load(c).unwrap();
|
||||||
|
if let Some(groups) = split_to_groups(&photos) {
|
||||||
|
let path = req.path_without_query().unwrap_or("/");
|
||||||
|
groups
|
||||||
|
.iter()
|
||||||
|
.map(|g| PhotoLink::for_group(g, path))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
} else {
|
||||||
|
photos.iter().map(PhotoLink::from).collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn split_to_groups(photos: &[Photo]) -> Option<Vec<&[Photo]>> {
|
pub fn split_to_groups(photos: &[Photo]) -> Option<Vec<&[Photo]>> {
|
||||||
if photos.len() < 42 {
|
if photos.len() < 42 {
|
||||||
|
Loading…
Reference in New Issue
Block a user