From 3c3ecbbfbbbf355271899d921cad500b32f08e9c Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Mon, 20 Nov 2017 14:06:55 +0100 Subject: [PATCH] DRY. --- src/server/mod.rs | 72 ++++------------------------------------- src/server/splitlist.rs | 35 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 66 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index eb3bc23..92c057d 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -360,30 +360,11 @@ fn tag_one<'mw>( let c: &PgConnection = &req.db_conn(); if let Ok(tag) = tags.filter(slug.eq(tslug)).first::(c) { 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( 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") { - 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::>() - } else { - photos.iter().map(PhotoLink::from).collect::>() - }; + let links = links_by_time(req, photos); return res.ok(|o| templates::tag(o, req, &links, &tag)); } res.not_found("Not a tag") @@ -437,31 +418,12 @@ fn place_one<'mw>( let c: &PgConnection = &req.db_conn(); if let Ok(place) = places.filter(slug.eq(tslug)).first::(c) { use schema::photo_places::dsl::{photo_id, photo_places, place_id}; - use schema::photos::dsl::{date, id}; + use schema::photos::dsl::id; let photos = Photo::query(req.authorized_user().is_some()).filter(id.eq_any( photo_places.select(photo_id).filter(place_id.eq(place.id)), )); - 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(); - 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::>() - } else { - photos.iter().map(PhotoLink::from).collect::>() - }; + let links = links_by_time(req, photos); return res.ok(|o| templates::place(o, req, &links, &place)); } res.not_found("Not a place") @@ -501,7 +463,7 @@ fn person_one<'mw>( let c: &PgConnection = &req.db_conn(); if let Ok(person) = people.filter(slug.eq(tslug)).first::(c) { 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( id.eq_any( photo_people @@ -509,29 +471,7 @@ fn person_one<'mw>( .filter(person_id.eq(person.id)), ), ); - 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(); - 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::>() - } else { - photos.iter().map(PhotoLink::from).collect::>() - }; + let links = links_by_time(req, photos); return res.ok(|o| templates::person(o, req, &links, &person)); } res.not_found("Not a person") diff --git a/src/server/splitlist.rs b/src/server/splitlist.rs index aa1c84e..25f7d79 100644 --- a/src/server/splitlist.rs +++ b/src/server/splitlist.rs @@ -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 nickel::Request; +use nickel_diesel::DieselRequestExtensions; +use schema::photos; + +pub fn links_by_time<'a>( + req: &mut Request, + photos: photos::BoxedQuery<'a, Pg>, +) -> Vec { + 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::>() + } else { + photos.iter().map(PhotoLink::from).collect::>() + } +} pub fn split_to_groups(photos: &[Photo]) -> Option> { if photos.len() < 42 {