summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2009-04-05 17:35:16 +0200
committerfranck cuny <franck@lumberjaph.net>2009-04-05 17:35:16 +0200
commit95ba59a81e496a729630176bbee16489a18c5fd3 (patch)
tree0d9e5cd313fafb2d889c9fb7425bf85a7d753113
parent3 lasts clouds, set lang, store last template in session (diff)
downloadintention-cloud-95ba59a81e496a729630176bbee16489a18c5fd3.tar.gz
check if permalink already exists, don't do query twice
-rw-r--r--lib/intentioncloud/Model/Search.pm30
1 files changed, 18 insertions, 12 deletions
diff --git a/lib/intentioncloud/Model/Search.pm b/lib/intentioncloud/Model/Search.pm
index a3a36c8..5a27b2b 100644
--- a/lib/intentioncloud/Model/Search.pm
+++ b/lib/intentioncloud/Model/Search.pm
@@ -8,6 +8,20 @@ use URI::Escape;
 
 sub search {
     my ( $self, $c ) = @_;
+
+    my $search = $c->req->params->{ q } . " ";
+    $c->stash( query => $search, dt => DateTime->now );
+
+    $c->stash(cloud_permalink => $c->stash->{dt}->ymd( '/' ) . '/'
+            . $c->req->params->{ engine } . '/'
+            . uri_escape( $c->req->params->{ q } ) );
+    my $cloud = $c->model( 'DB::Search' )
+        ->find( { permalink => $c->stash->{ cloud_permalink } } );
+    if ( $cloud ) {
+        $c->stash( cloud => $cloud->cloud );
+        return;
+    }
+
     if ( $c->req->params->{ engine } eq 'google' ) {
         $self->google_search( $c );
     }
@@ -19,8 +33,6 @@ sub google_search {
 
     my $cloud   = HTML::TagCloud->new();
     my $suggest = WebService::Google::Suggest->new();
-    my $search  = $c->req->params->{ q } . " ";
-    $c->stash( query => $search );
 
     my $suggests = 0;
     foreach ( $suggest->complete( $search ) ) {
@@ -30,7 +42,7 @@ sub google_search {
         }
     }
     if ( $suggests ) {
-        $c->stash( cloud => $cloud->html_and_css());
+        $c->stash( cloud => $cloud->html_and_css() );
     } else {
         $c->stash( no_suggest => "no suggestions :(" );
     }
@@ -38,19 +50,13 @@ sub google_search {
 
 sub save_search {
     my ( $self, $c ) = @_;
-    my $dt = DateTime->now;
-
-    my $permalink
-        = $dt->ymd( '/' ) . '/'
-        . $c->req->params->{ engine } . '/'
-        . uri_escape( $c->req->params->{ q } );
 
     $c->model( 'DB::Search' )->create(
         {   engine       => $c->req->params->{ engine },
-            cloud        => $c->stash->{ suggest },
-            date_created => $dt,
+            cloud        => $c->stash->{ cloud },
+            date_created => $c->stash->{dt},
             query        => $c->req->params->{ q },
-            permalink    => $permalink,
+            permalink    => $c->stash->{ cloud_permalink },
         }
     );
 }