From 245fa7576b4a2d848435f4eee3d7f41cbf042e8a Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Tue, 6 Oct 2015 04:50:34 +0200
Subject: removing show works now as well


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 7937643..6730127 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -198,12 +198,18 @@ sub check_log_exists
   return (1, 'OK', 'log exists');
 }
 
+sub get_log_table_name
+{
+  my ($ctx, $logname) = @_;
+  $logname=~s/ /_/g;
+  return $ctx->{'dbh'}->quote_identifier($logname . '_LOG');
+}
+
 sub create_log_table
 {
   my ($ctx, $logname) = @_;
 
-  $logname=~s/ /_/g;
-  $logname = $ctx->{'dbh'}->quote_identifier($logname . '_LOG');
+  $logname = get_log_table_name($ctx, $logname);
   my $sql = qq{
     create table if not exists $logname
     (ID INT NOT NULL PRIMARY KEY,
@@ -265,8 +271,7 @@ sub fill_log_table
   my $logname = shift;
   my @carts = @_;
 
-  $logname=~s/ /_/g;
-  $logname = $ctx->{'dbh'}->quote_identifier($logname . '_LOG');
+  $logname = get_log_table_name($ctx, $logname);
   my $sql = qq{insert into $logname (ID, COUNT, START_TIME, CART_NUMBER, TRANS_TYPE) values (?, ?, ?, ?, ?);};
 
   my $sth = $ctx->{'dbh'}->prepare($sql)
@@ -283,6 +288,19 @@ sub fill_log_table
   return ($cnt+1, 'OK', 'success');
 }
 
+sub drop_log_table
+{
+  my ($ctx, $logname) = @_;
+
+  $logname = get_log_table_name($ctx, $logname);
+  my $sql = qq{drop table $logname;};
+
+  $ctx->{'dbh'}->do($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  return (1, 'OK', 'success');
+}
+
 ###########################  TOKEN handling  ###########################
 
 sub get_token
@@ -971,8 +989,7 @@ sub get_show_carts
     return (undef, 'ERROR', "Log with name '$log' does not exist")
   }
 
-  $log=~s/ /_/g;
-  $log = $ctx->{'dbh'}->quote_identifier($log . '_LOG');
+  $log = get_log_table_name($ctx, $log);
   my $sql = qq{select COUNT,CART_NUMBER from $log order by COUNT;};
 
   my $sth = $ctx->{'dbh'}->prepare($sql)
@@ -1113,6 +1130,55 @@ sub create_show_dropbox
   return ($cnt, 'OK', 'success');
 }
 
+sub remove_show
+{
+  my ($ctx, $showid) = @_;
+
+  my ($title, $logname, $status, $errorstring) = get_show_title_and_log($ctx, $showid);
+  unless (defined $title && defined $logname) {
+    return (undef, $status, $errorstring);
+  }
+
+  my @actions = ({
+      # Delete Dropbox
+      sql => qq{delete from DROPBOXES where TO_CART = ?;},
+      param => $showid,
+      name => 'dropboxes',
+      cnt => 0
+    }, {
+      # Delete Macro Carts
+      sql => qq{delete from CART where NUMBER = ?;},
+      param => $showid,
+      name => 'macro carts',
+      cnt => 0
+    }, {
+      # Delete Log Entry
+      sql => qq{delete from LOGS where NAME = ?;},
+      param => $logname,
+      name => 'log entries',
+      cnt => 0
+    });
+
+  for my $href (@actions) {
+    my $sth = $ctx->{'dbh'}->prepare($href->{sql})
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+    delete($href->{sql});
+
+    $href->{cnt} = $sth->execute($href->{param})
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+    $sth->finish();
+  }
+
+  (my $cnt, $status, $errorstring) = drop_log_table($ctx, $logname);
+  unless (defined $cnt) {
+    return (undef, $status, $errorstring);
+  }
+  push @actions, { name => 'log tables', cnt => $cnt };
+
+  return @actions;
+}
+
 ###########################  MUSICPOOL handling  ###########################
 
 sub get_musicpools_cart_range
diff --git a/utils/rd-show b/utils/rd-show
index 3d54630..0ad2426 100755
--- a/utils/rd-show
+++ b/utils/rd-show
@@ -260,7 +260,14 @@ sub remove
 {
   my ($ctx, $show_id) = @_;
 
-  print "removing show " . $show_id . ", not yet implemented!\n";
+  my @results = RHRD::rddb::remove_show($ctx, $show_id);
+  if(!defined $results[0] && defined $results[2]) {
+    print STDERR "$results[2]\n";
+    return 1;
+  }
+  for my $href (@results) {
+    print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
+  }
   return 0;
 }
 
-- 
cgit v0.10.2