From 9754445900d0b517d0382616648094c6d1c0055e Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Wed, 20 May 2015 21:51:50 +0200
Subject: [PATCH] add fav count to !stats

---
 lib/utils/db/stats.pm | 27 ++++++++++++++++++++-------
 lib/utils/print.pm    |  9 ++++++++-
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/lib/utils/db/stats.pm b/lib/utils/db/stats.pm
index 59e9cad..5d9139a 100644
--- a/lib/utils/db/stats.pm
+++ b/lib/utils/db/stats.pm
@@ -14,6 +14,7 @@ has 'date'      => (is => 'ro', isa => 'Str');
 has 'count'     => (is => 'ro', isa => 'Int');
 has 'senders'   => (is => 'ro', isa => 'HashRef[Str]');
 has 'channels'  => (is => 'ro', isa => 'HashRef[Str]');
+has 'fav'       => (is => 'ro', isa => 'Int');
 
 
 around 'BUILDARGS' => sub {
@@ -33,7 +34,7 @@ around 'BUILDARGS' => sub {
         from playbot_chan
         where content = ? and chan like \'#%\'
         order by date
-        ');
+    ');
 
     $sth->execute($id);
     while (my $row = $sth->fetch) {
@@ -49,15 +50,27 @@ around 'BUILDARGS' => sub {
         $count++;
     }
 
+    my $fav;
+    $sth = $dbh->prepare('
+        select count(*)
+        from playbot_fav
+        where id = ?
+    ');
+    $sth->execute($id);
+    while (my $row = $sth->fetch) {
+        $fav++;
+    }
+
     $dbh->commit;
 
     return $class->$orig(
-        count     => $count,
-        sender    => $sender,
-        chan      => $chan,
-        date      => $date,
-        senders   => $senders,
-        channels  => $channels
+        count       => $count,
+        sender      => $sender,
+        chan        => $chan,
+        date        => $date,
+        senders     => $senders,
+        channels    => $channels,
+        fav         => $fav,
     );
 };
 
diff --git a/lib/utils/print.pm b/lib/utils/print.pm
index cdf151e..bfdd1f3 100644
--- a/lib/utils/print.pm
+++ b/lib/utils/print.pm
@@ -55,6 +55,7 @@ sub stats {
     my $line1; 
     my $line2;
     my $line3;
+    my $line4;
 
     $line1 .= 'Posté la 1re fois par '.$stats->sender;
     $line1 .= ' le '.$stats->date;
@@ -84,7 +85,13 @@ sub stats {
         }
     }
 
-    return ($line1, $line2, $line3);
+    if ($stats->fav) {
+        $line4 = "Sauvegardé dans ses favoris par ";
+        $line4 .= $stats->fav.' personne';
+        $line4 .= 's' if ($stats->fav > 1);
+    }
+
+    return ($line1, $line2, $line3, $line4);
 }
 
 1;
-- 
GitLab