From 72f472403b40014d432a9f74624b8f697039dac3 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Tue, 17 Feb 2015 16:00:59 +0100
Subject: [PATCH] Sam::commands::edt uses the ORM

---
 Sam/commands/edit.pm   | 31 ++++++++++++++++---------------
 Sam/commands/parser.pm |  2 +-
 2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/Sam/commands/edit.pm b/Sam/commands/edit.pm
index 221a80d..197f912 100644
--- a/Sam/commands/edit.pm
+++ b/Sam/commands/edit.pm
@@ -5,13 +5,15 @@ our @ISA = qw(Exporter);
 our @EXPORT_OK = qw(exec);
 
 use Regexp::Common qw(URI);
+use Sam::Chan;
+use Sam::Event;
 
 our $dbh;
 our $log;
 our $irc;
 
 sub exec {
-    my ($id, $command, $arg, $chan) = @_;
+    my ($id, $command, $arg, $chan_name) = @_;
 
     my $saveCommit = $dbh->{AutoCommit};
     my $saveError = $dbh->{RaiseError};
@@ -23,23 +25,22 @@ sub exec {
             die "Ce n'est pas une URL valide";
         }
         else {
-            eval {
-                $sth = $dbh->prepare_cached("UPDATE sam SET link = ? WHERE id = ?");
-                $sth->execute($arg, $id);
-
-                if ($sth->rows == 0) {
-                    die "Aucune soirée trouvée avec cet identifiant";
-                }
-
-                $dbh->commit;
-            };
+            my $event = Sam::Event->new(id => $id);
+            unless ($event->load(speculative => 1)) {
+                die "aucune soirée trouvée avec cet identifiant";
+            }
 
-            if ($@) {
-                $log->error("$@");
-                $dbh->rollback;
+            my $chan = Sam::Chan->new(name => $chan_name);
+            unless ($chan->load(speculative => 1)) {
+                die "ce chan n'est pas encore configuré";
+            }
 
-                die $@;
+            unless ($event->calendar_id == $chan->calendar_id) {
+                die "cette soirée ne fait pas partie du calendrier associé à ce chan";
             }
+
+            $event->link($arg);
+            $event->save();
         }
     }
     else {
diff --git a/Sam/commands/parser.pm b/Sam/commands/parser.pm
index 3824e49..4024c0e 100644
--- a/Sam/commands/parser.pm
+++ b/Sam/commands/parser.pm
@@ -85,7 +85,7 @@ sub exec {
         my $arg = $3;
 
         eval {
-            Sam::commands::edit::exec($id, $command, $arg, $chan);
+            Sam::commands::edit::exec($id, $command, $arg, $chan->[0]);
         };
 
         if ($@) {
-- 
GitLab