diff options
author | Matthew Somerville <matthew@mysociety.org> | 2011-06-09 10:37:41 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2011-06-09 10:37:41 +0100 |
commit | 2f905f6b8f74a5a892b3974e4f0b4076f9b58ddf (patch) | |
tree | 4a491cd4fee20315b08eb1be87f6f51f5b19fa8e /perllib/FixMyStreet/DB/ResultSet/Problem.pm | |
parent | 2e8c9f4ab19f8db068318239f15f2458dd64aa00 (diff) |
Migrate recent_photos to new ResultSet, nice joining with problem_find_nearby procedure.
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet/Problem.pm')
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index ea146cd54..0ca8d1127 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -51,18 +51,53 @@ sub recent { my $key = "recent:$site_key"; my $result = Memcached::get($key); unless ($result) { - $result = $rs->search( { + $result = [ $rs->search( { state => [ 'confirmed', 'fixed' ] }, { columns => [ 'id', 'title' ], order_by => { -desc => 'confirmed' }, rows => 5, - } )->count; + } )->all ]; Memcached::set($key, $result, 3600); } return $result; } +sub recent_photos { + my ( $rs, $num, $lat, $lon, $dist ) = @_; + my $probs; + my $query = { + state => [ 'confirmed', 'fixed' ], + photo => { '!=', undef }, + }; + my $attrs = { + columns => [ 'id', 'title' ], + order_by => { -desc => 'confirmed' }, + rows => $num, + }; + if (defined $lat) { + my $dist2 = $dist; # Create a copy of the variable to stop it being stringified into a locale in the next line! + my $key = "recent_photos:$site_key:$num:$lat:$lon:$dist2"; + $probs = Memcached::get($key); + unless ($probs) { + $attrs->{bind} = [ $lat, $lon, $dist ]; + $attrs->{join} = 'nearby'; + $probs = [ mySociety::Locale::in_gb_locale { + $rs->search( $query, $attrs )->all; + } ]; + Memcached::set($key, $probs, 3600); + } + } else { + my $key = "recent_photos:$site_key:$num"; + $probs = Memcached::get($key); + unless ($probs) { + $probs = [ $rs->search( $query, $attrs )->all ]; + Memcached::set($key, $probs, 3600); + } + } + return $probs; +} + # Problems around a location sub around_map { |