diff --git a/lib/BurguillosInfo/Controller/Page.pm b/lib/BurguillosInfo/Controller/Page.pm index 959705c..b4d1895 100644 --- a/lib/BurguillosInfo/Controller/Page.pm +++ b/lib/BurguillosInfo/Controller/Page.pm @@ -126,6 +126,8 @@ sub post { $self->render( template => '404', status => 404 ); return; } + my $interest = BurguillosInfo::Interest->new(app => $self->app); + $interest->increment_post_interest($self, $slug); my $current_category = $categories->{ $post->{category} }; my $base_url = $self->config('base_url'); $self->stash( diff --git a/lib/BurguillosInfo/Controller/Search.pm b/lib/BurguillosInfo/Controller/Search.pm index ede1dc5..41d7660 100644 --- a/lib/BurguillosInfo/Controller/Search.pm +++ b/lib/BurguillosInfo/Controller/Search.pm @@ -18,21 +18,30 @@ my $index_utils = BurguillosInfo::IndexUtils->new; my $search_cache = {}; +sub _render_search( $self, $embedded, $query ) { + my $interest = BurguillosInfo::Interest->new( app => $self->app ); + $interest->increment_search_interest( $self, $query ); + my $searchObjects = $search_cache->{$query}; + $searchObjects = [ grep { $self->filterSearch($_) } @$searchObjects ]; + $search_cache->{$query} = $searchObjects; + return $self->render( + template => 'page/search', + searchObjects => $search_cache->{$query}, + embedded => $embedded, + query => $query, + ); +} + sub search_user($self) { - my $ua = Mojo::UserAgent->new; - my $query = $self->param('q'); + my $ua = Mojo::UserAgent->new; + my $query = $self->param('q'); my $embedded = $self->param('e'); - my $base_url = $self->config('base_url'); - if (defined $query && !$query) { - $self->redirect_to($base_url.'/search.html'); + my $base_url = $self->config('base_url'); + if ( defined $query && !$query ) { + $self->redirect_to( $base_url . '/search.html' ); } if ( defined $search_cache->{$query} ) { - return $self->render( - template => 'page/search', - searchObjects => $search_cache->{$query}, - embedded => $embedded, - query => $query, - ); + return $self->_render_search( $embedded, $query ); } my $config = $self->config; my $search_backend = $config->{search_backend}; @@ -49,15 +58,8 @@ sub search_user($self) { } my $ok = $output->{ok}; my $reason = $output->{reason}; - my $searchObjects = $output->{searchObjects} || []; - $searchObjects = [ grep { $self->filterSearch($_) } @$searchObjects ]; - $search_cache->{$query} = $searchObjects; - return $self->render( - template => 'page/search', - searchObjects => $search_cache->{$query}, - embedded => $embedded, - query => $query, - ); + $search_cache->{$query} = $output->{searchObjects}; + return $self->_render_search( $embedded, $query ); } sub search ($self) { @@ -100,7 +102,7 @@ sub filterSearch( $self, $searchObject ) { my $url = $searchObject->{url}; my ( $posts_by_categories, $posts ) = BurguillosInfo::Posts->Retrieve; my $slug; - my $interest = BurguillosInfo::Interest->new(app => $self->app); + my $interest = BurguillosInfo::Interest->new( app => $self->app ); if ( $url =~ m{^/posts/([^/]+?)(?:\?.*)?$} ) { $slug = $1; if ( !defined $posts->{$slug} ) { @@ -109,7 +111,7 @@ sub filterSearch( $self, $searchObject ) { } if ( $url =~ m{^/producto?/([^/]+?)(?:\?.*)?$} ) { $slug = $1; - $interest->set_product_interest_searched($self, $slug); + $interest->set_product_interest_searched( $self, $slug ); } return 1; } diff --git a/lib/BurguillosInfo/DB/Migrations.pm b/lib/BurguillosInfo/DB/Migrations.pm index c88a2e6..3738e5c 100644 --- a/lib/BurguillosInfo/DB/Migrations.pm +++ b/lib/BurguillosInfo/DB/Migrations.pm @@ -73,6 +73,10 @@ sub MIGRATIONS { term TEXT NOT NULL, FOREIGN KEY (id_cookie) REFERENCES interest_cookies(id) );', + 'ALTER TABLE interest_posts ADD column count BIGINT NOT NULL;', + 'ALTER TABLE interest_posts ADD CONSTRAINT interest_post_unique_constraint UNIQUE (id_cookie, slug);', + 'ALTER TABLE interest_searches ADD column count BIGINT NOT NULL;', + 'ALTER TABLE interest_searches ADD CONSTRAINT interest_search_unique_constraint UNIQUE (id_cookie, term);', ); } diff --git a/lib/BurguillosInfo/Interest.pm b/lib/BurguillosInfo/Interest.pm index e841be5..839f04c 100644 --- a/lib/BurguillosInfo/Interest.pm +++ b/lib/BurguillosInfo/Interest.pm @@ -28,12 +28,20 @@ sub _build__dbh($self) { sub _get_interest_cookie( $self, $c ) { my $cookie_value = $c->cookie( $self->_cookie_name, ); + say $cookie_value; if ( !defined $cookie_value ) { $cookie_value = create_uuid_hex(); + say $cookie_value; + } + eval { $self->_dbh->do( ' INSERT INTO interest_cookies (cookie_value) -VALUES (?);', {}, $cookie_value ); +VALUES (?); + ', {}, $cookie_value ); + }; + if ($@) { + # warn $@; } $c->cookie( $self->_cookie_name, @@ -41,13 +49,55 @@ VALUES (?);', {}, $cookie_value ); { expires => time + 3600 * 24 * 390, samesite => 'Lax', - secure => 1, + ( + $c->config('base_url') =~ /https/ + ? ( secure => 1, ) + : () + ), } ); return $cookie_value; } +sub increment_search_interest( $self, $c, $term ) { + my $cookie_value = $self->_get_interest_cookie($c); + my $dbh = $self->_dbh; + $dbh->do( ' +INSERT INTO interest_searches ( + id_cookie, + term, + count + ) +SELECT id, ?, 1 + FROM interest_cookies + WHERE cookie_value = ? +ON CONFLICT (id_cookie, term) +DO UPDATE SET + count + = interest_searches.count + 1; +', {}, $term, $cookie_value ); +} + +sub increment_post_interest( $self, $c, $slug ) { + my $cookie_value = $self->_get_interest_cookie($c); + my $dbh = $self->_dbh; + $dbh->do( ' +INSERT INTO interest_posts ( + id_cookie, + slug, + count + ) +SELECT id, ?, 1 + FROM interest_cookies + WHERE cookie_value = ? +ON CONFLICT (id_cookie, slug) +DO UPDATE SET + count + = interest_posts.count + 1; +', {}, $slug, $cookie_value ); +} + sub _set_product_interest( $self, $c, $slug, $interest_value ) { my $cookie_value = $self->_get_interest_cookie($c); my $dbh = $self->_dbh; @@ -61,12 +111,12 @@ SELECT id, ?, ? FROM interest_cookies WHERE cookie_value = ? ON CONFLICT (id_cookie, slug) - DO UPDATE SET - max_interest - = GREATEST( - EXCLUDED.max_interest, - interest_products.max_interest - ); +DO UPDATE SET + max_interest + = GREATEST( + EXCLUDED.max_interest, + interest_products.max_interest + ); ', {}, $interest_value, $slug, $cookie_value ); }