From 1e67ddbedf6481e3e8650a498679eafd710230ca Mon Sep 17 00:00:00 2001 From: Edmund von der Burg Date: Fri, 20 May 2011 17:33:34 +0100 Subject: port json.cgi code to Catalyst --- perllib/FixMyStreet/App/Controller/JSON.pm | 53 ++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 10 deletions(-) (limited to 'perllib/FixMyStreet/App/Controller/JSON.pm') diff --git a/perllib/FixMyStreet/App/Controller/JSON.pm b/perllib/FixMyStreet/App/Controller/JSON.pm index a0e5490cb..c437aafc0 100644 --- a/perllib/FixMyStreet/App/Controller/JSON.pm +++ b/perllib/FixMyStreet/App/Controller/JSON.pm @@ -47,23 +47,56 @@ sub json : Path : Args(0) { return; } + # check that the dates are sane + if ( $start_dt > $end_dt ) { + $c->stash->{error} = 'Start date after end date'; + return; + } + # check that the type is supported unless ( $type eq 'new_problems' || $type eq 'fixed_problems' ) { $c->stash->{error} = 'Invalid type supplied'; return; } - my $response = $c->stash->{response} ||= {}; - - # elsif ( $type eq 'new_problems' ) { - # $problems = Problems::created_in_interval( $start_date, $end_date ); - # } - # elsif ( $type eq 'fixed_problems' ) { - # $problems = Problems::fixed_in_interval( $start_date, $end_date ); - # } - + # query the database + $c->stash->{response} = + $type eq 'new_problems' + ? Problems::created_in_interval( $start_date, $end_date ) + : Problems::fixed_in_interval( $start_date, $end_date ); } +# If we convert this code to be fully DBIC based then the following snippet is a +# good start. The roadblock to doing it fully is the 'site_restriction' in the +# SQL which is currently provided as SQL, rather than something that could be +# easily added to the DBIC query. The hardest cobrand to change would be the +# cities - so perhaps do it after we know wether that needs to be kept or not. +# +# my $state = +# $type eq 'new_problems' ? 'confirmed' +# : $type eq 'fixed_problems' ? 'fixed_problems' +# : die; +# +# my $one_day = DateTime::Duration->new( days => 1 ); +# +# my $problems = $c->model('DB::Problem')->search( +# { +# created => { +# '>=' => $start_dt, +# '<=' => $end_dt + $one_day, +# }, +# state => $state, +# # ------ add is site_restriction here ------- +# }, +# { +# columns => [ +# 'id', 'title', 'council', 'category', +# 'detail', 'name', 'anonymous', 'confirmed', +# 'whensent', 'service', +# ] +# } +# ); + sub end : Private { my ( $self, $c ) = @_; @@ -73,7 +106,7 @@ sub end : Private { : $c->stash->{response}; $c->res->content_type('application/json; charset=utf-8'); - $c->res->body( encode_json($response) ); + $c->res->body( encode_json( $response || {} ) ); } __PACKAGE__->meta->make_immutable; -- cgit v1.2.3