From c3c7bf2d9a33d0cf7ef67bbbae6a414ca6f645a4 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Wed, 30 Apr 2014 15:33:44 +1200 Subject: [PATCH] Bug 9048 - fix quote editor under plack The ajax responder for the quote editor was using the wrong error codes. These have been fixed. Also, a small fixup to get rid of some annoying warnings. To test: * Under plack, * Add/edit/delete a quote. * Make sure that things don't crash. --- tools/quotes/quotes_ajax.pl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tools/quotes/quotes_ajax.pl b/tools/quotes/quotes_ajax.pl index da5dc01..5f44a7e 100755 --- a/tools/quotes/quotes_ajax.pl +++ b/tools/quotes/quotes_ajax.pl @@ -48,43 +48,44 @@ my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!! print $cgi->header('application/json; charset=utf-8'); -if ($params->{'action'} eq 'add') { +my $action = $params->{'action'} || 'get'; +if ($action eq 'add') { my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);'); $sth->execute($params->{'source'}, $params->{'text'}); if ($sth->err) { warn sprintf('Database returned the following error: %s', $sth->errstr); - exit 0; + exit 1; } my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;'); $sth->execute($new_quote_id); print to_json($sth->fetchall_arrayref, {utf8 =>1}); - exit 1; + exit 0; } -elsif ($params->{'action'} eq 'edit') { +elsif ($action eq 'edit') { my $aaData = []; my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1] = ? WHERE id = ?;"); $sth->execute($params->{'value'}, $params->{'id'}); if ($sth->err) { warn sprintf('Database returned the following error: %s', $sth->errstr); - exit 0; + exit 1; } $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;"); $sth->execute($params->{'id'}); $aaData = $sth->fetchrow_array(); print Encode::encode('utf8', $aaData); - exit 1; + exit 0; } -elsif ($params->{'action'} eq 'delete') { +elsif ($action eq 'delete') { my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;"); $sth->execute($params->{'id'}); if ($sth->err) { warn sprintf('Database returned the following error: %s', $sth->errstr); - exit 0; + exit 1; } - exit 1; + exit 0; } else { my $aaData = []; @@ -97,7 +98,7 @@ else { $sth->execute(); if ($sth->err) { warn sprintf('Database returned the following error: %s', $sth->errstr); - exit 0; + exit 1; } $aaData = $sth->fetchall_arrayref; -- 1.8.3.2