From 8d17fc61507014d72694fff2d0721d03ba2a02a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20J=C3=B6rg?= Date: Fri, 5 Jul 2019 00:57:46 +0200 Subject: [PATCH] Refactoring: Collect stray SQL stuff #15: Act::Tag --- lib/Act/Data.pm | 38 +++++++++++++++++++++++++++++++++++++- lib/Act/Tag.pm | 21 +++------------------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lib/Act/Data.pm b/lib/Act/Data.pm index a8d3490b3..3e2cf686e 100644 --- a/lib/Act/Data.pm +++ b/lib/Act/Data.pm @@ -397,6 +397,32 @@ sub participations ($user_id) { } +# ---------------------------------------------------------------------- +# From Act::Tag +sub find_tagged ($conference,$type,$tags) { + my $sth = sql( + 'SELECT DISTINCT tagged_id FROM tags' + . ' WHERE conf_id = ? AND type = ?' + . ' AND tag IN (' . join(',', ('?') x @$tags) . ')', + $conference, $type, @$tags ); + my @result = map { $_->[0] } @{$sth->fetchall_arrayref([])}; + return \@result; +} + +sub find_tags ($conference,$type,$filter) { + my $sql = 'SELECT tag, COUNT(tag) FROM tags' + . ' WHERE conf_id = ? AND type = ?'; + my @values = ( $conference, $type ); + if ($filter) { + $sql .= ' AND tagged_id IN (' . join(',',('?') x @$filter) . ')'; + push @values, @$filter; + } + $sql .= ' GROUP BY tag ORDER BY tag'; + my $sth = sql($sql, @values); + return $sth->fetchall_arrayref([]); +} + + # ---------------------------------------------------------------------- # From Act::News # The following queries doesn't technically pass a conference, but news @@ -639,6 +665,16 @@ evaluated attributes of a yet-to-be-written class Act::Visitor. Returns a reference to an array holding the numerical user ids of users who announced to attend the talk C<$talk_id> at C<$conference>. +=head3 $listref = Act::Data::find_tagged($conference,$type,$tags) + +Returns a reference of tag ids for the given conference, +tag type, and tags in C<@$tags>. + +=head3 ACt::Data::find:tags($conference,$type,$filter) + +Returns an array reference to array references containing tags and +their count each. + =head2 Queries about users =head3 Act::Data::store_token(token,$email,$data) @@ -709,7 +745,7 @@ orders. =head1 CAVEATS -There are no automated tests for these functions yet. This is bad. +The Act test suite doesn't exercise all of these functions. This is bad. =head1 AUTHOR diff --git a/lib/Act/Tag.pm b/lib/Act/Tag.pm index fcfd77ead..6f4092ddd 100644 --- a/lib/Act/Tag.pm +++ b/lib/Act/Tag.pm @@ -59,29 +59,14 @@ sub update_tags sub find_tagged { my ($class, %args) = @_; - my @tags = @{ $args{tags} }; - my $sth = sql( - 'SELECT DISTINCT tagged_id FROM tags' - . ' WHERE conf_id = ? AND type = ?' - . ' AND tag IN (' . join(',', ('?') x @tags) . ')', - $args{conf_id}, $args{type}, @tags ); - my $result = $sth->fetchall_arrayref([]); - return sort map $_->[0], @$result; + my $result = Act::Data::find_tagged(@args{qw(conf_id type tags)}); + return sort @$result; } sub find_tags { my ($class, %args) = @_; return [] if $args{filter} && !@{$args{filter}}; - my $SQL = 'SELECT tag, COUNT(tag) FROM tags' - . ' WHERE conf_id = ? AND type = ?'; - my @values = ( $args{conf_id}, $args{type} ); - if ($args{filter}) { - $SQL .= ' AND tagged_id IN (' . join(',',('?') x @{$args{filter}}) . ')'; - push @values, @{$args{filter}}; - } - $SQL .= ' GROUP BY tag ORDER BY tag'; - my $sth = sql($SQL, @values); - return $sth->fetchall_arrayref([]); + return Act::Data::find_tags($args{conf_id}, $args{type}, $args{filter}); } sub get_cloud {