Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2012/01/18

More Gripes and Misunderstandings about Dancer

It has been suggested that I'm not using MVC separation, which is fair, as I don't really see MVC separation. I found the POD for Dancer::Plugin::Database, and have modified my package to use it. weather is the old one, weather2 is the new one using the plugin.

  1. package TestApp ;  
  2. use Dancer ':syntax' ;  
  3. use Dancer::Plugin::Database ;  
  4. use lib '/home/jacoby/lib' ;  
  5. use MyDB 'db_connect' ;  
  6. use DateTime ;  
  7. use Data::Dumper ;  
  8.   
  9. our $VERSION = '0.1' ;  
  10.   
  11. get '/' => sub {  
  12.     template 'index' ;  
  13.     } ;  
  14.    
  15. get '/weather' => sub {  
  16.     my $dbh = db_connect() ;  
  17.     my $sql = <<SQL;  
  18.     # Last Eight Hours  
  19.     SELECT  time , AVG( temp_f )  
  20.     FROM weather WHERE zip = "47909"  
  21.     AND HOUR( TIMEDIFF( SYSDATE() , time ) ) < 9  
  22.     GROUP BY HOUR(time)  
  23.     ORDER BY time  
  24. SQL  
  25.     my $hr = $dbh->selectall_arrayref( $sql ) or croak $dbh->errstr ;  
  26.     my @hr ;  
  27.     for my $point ( @$hr ) {  
  28.         my ( $time, $temp ) = @$point ;  
  29.         my @time = split /\D+/ ,  $time ;  
  30.         my $dt = DateTime->new(  
  31.             year  => $time[0] ,  
  32.             month => $time[1] ,  
  33.             day   => $time[2] ,  
  34.             hour => $time[3] ,  
  35.             minute => $time[4] ,  
  36.             second => $time[5] ,  
  37.             ) ;  
  38.         my $data ;  
  39.         $data->{ time } = $dt->format_cldr( 'h:mm a' ) ;  
  40.         $data->{ temp } = sprintf '%.1f', $temp ;  
  41.         next if $data->{ time } !~ /00/ ;  
  42.         push @hr, $data ;  
  43.         }  
  44.     my $string = 'Temperature in West Lafayette for the last 8 hours' ;  
  45.     my $hr_ptr = \@hr ;  
  46.     template 'weather' , {  
  47.         string => $string ,  
  48.         weather => $hr_ptr  
  49.         } ;  
  50.     } ;  
  51.   
  52. get '/weather2' => sub {  
  53.     my $string = 'Temperature in West Lafayette for the last 8 hours' ;  
  54.     my $sth = database->prepare(  
  55.         'SELECT  time , AVG( temp_f ) temp  
  56.         FROM weather WHERE zip = "47909"  
  57.         AND HOUR( TIMEDIFF( SYSDATE() , time ) ) < 9  
  58.         GROUP BY HOUR(time)  
  59.         ORDER BY time '  
  60.         ) ;  
  61.     $sth->execute( ) ;  
  62.     template 'weather' , {  
  63.         string => $string ,  
  64.         xxx => $sth->fetchall_arrayref( ) ,  
  65.         } ;  
  66.     } ;  
  67.   
  68. true ;  

I have been pointed to http://search.cpan.org/~xsawyerx/Dancer-1.3091/lib/Dancer/Tutorial.pod, and, beyond my wanting to be more specific in the munging of the data, weather is very similar to "Our First Route Handler". (How I store some data is not necessarily how I want to display some data.)

weather2 uses http://search.cpan.org/~bigpresh/Dancer-Plugin-Database-1.60/lib/Dancer/Plugin/Database.pm and you can see that there's much less code in there. The stuff I put into  MyDB exists in Dancer::Plugin::Database, and I probably could mess with the data before sending it on, instead of hooking up $sth->fetchall_arrayref() to the output.

The big thing I see here, which I could do something about with weather but probably not with the plugin-using weather2, is the SQL code being here. I would much rather shove that SQL off into a library somewhere, which I've done elsewhere, than have it at this point. But maybe, that's an aspect of me not really getting the MVC thing yet.

No comments:

Post a Comment