Skip to content

Commit

Permalink
Refactoring: Collect stray SQL stuff #15: Act::Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldJoerg committed Jul 4, 2019
1 parent 432e40e commit 8d17fc6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
38 changes: 37 additions & 1 deletion lib/Act/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 3 additions & 18 deletions lib/Act/Tag.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 8d17fc6

Please sign in to comment.