From c30580e5297fdb94da44b6f13b101da00d59e925 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 19 Aug 2011 01:32:27 +0200 Subject: Simplify template and request handling --- cgi/index.pl | 118 +++++++++++++++++++++++------------------------------------ 1 file changed, 45 insertions(+), 73 deletions(-) diff --git a/cgi/index.pl b/cgi/index.pl index 69e4698..3f79200 100644 --- a/cgi/index.pl +++ b/cgi/index.pl @@ -25,29 +25,24 @@ sub get_results_for { return @{$results}; } -get '/' => sub { +sub handle_request { my $self = shift; - my $station = $self->param('station'); + my $station = $self->stash('station'); - $self->stash( 'version', $VERSION ); + $self->stash( departures => [] ); + $self->stash( title => 'db-fakedisplay' ); + $self->stash( version => $VERSION ); if ( not $station ) { - return $self->render; + $self->render('multi'); + return; } - $self->redirect_to("/${station}"); -} => 'index'; - -get '/:station' => sub { - my $self = shift; - my $station = $self->stash('station'); my @departures; my @results = get_results_for($station); - $self->stash( 'version', $VERSION ); - if ( not @results ) { - $self->render( 'index', error => "Got no results for '$station'", ); + $self->render( 'multi', error => "Got no results for '$station'" ); return; } @@ -71,75 +66,22 @@ get '/:station' => sub { version => $VERSION, title => "departures for ${station}" ); -}; +} -get '/multi/:station' => sub { +get '/_redirect' => sub { my $self = shift; - my $station = $self->stash('station'); + my $station = $self->param('station'); $self->redirect_to("/${station}"); }; +get '/' => \&handle_request; +get '/:station' => \&handle_request; +get '/multi/:station' => \&handle_request; + app->start(); __DATA__ -@@ index.html.ep - - - - DB Fakedisplay - - - - -
-

-DB-Fakedisplay displays the next departures at a DB station, just like the big -LC display in the station itself. -

- -<% if (my $error = stash 'error') { %> - Error: <%= $error %>
-<% } %> -<%= form_for index => begin %> -

- Station name:
- <%= text_field 'station' %>
- <%= submit_button 'Display' %> -

-<% end %> - -

-(For example: "Koeln Hbf" or "Essen West") -

- -

-This is db-fakedisplay -v<%= $version %> -

- -
- - - - @@ multi.html.ep @@ -236,6 +178,8 @@ v<%= $version %> +% if (@{$departures}) { +
% my $i = 0; % for my $departure (@{$departures}) { @@ -279,6 +223,34 @@ v<%= $version %>
+% } +% else { + +

+DB-Fakedisplay displays the next departures at a DB station, just like the big +LC display in the station itself. +

+ +% } + +
+ +<% if (my $error = stash 'error') { %> +

+ Error: <%= $error %>
+

+<% } %> + +<%= form_for _redirect => begin %> +

+ Station name: + <%= text_field 'station' %> + <%= submit_button 'Display' %> +

+<% end %> + +
+
db-fakedisplay v<%= $version %> -- cgit v1.2.3