diff options
| author | franck cuny <franck@lumberjaph.net> | 2009-04-05 17:35:16 +0200 |
|---|---|---|
| committer | franck cuny <franck@lumberjaph.net> | 2009-04-05 17:35:16 +0200 |
| commit | 95ba59a81e496a729630176bbee16489a18c5fd3 (patch) | |
| tree | 0d9e5cd313fafb2d889c9fb7425bf85a7d753113 /lib/intentioncloud/Model/Search.pm | |
| parent | 3 lasts clouds, set lang, store last template in session (diff) | |
| download | intention-cloud-95ba59a81e496a729630176bbee16489a18c5fd3.tar.gz | |
check if permalink already exists, don't do query twice
Diffstat (limited to 'lib/intentioncloud/Model/Search.pm')
| -rw-r--r-- | lib/intentioncloud/Model/Search.pm | 30 |
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 }, } ); } |
