From 008ee7b5d7b609172be3e2035df0bbb8fb464b70 Mon Sep 17 00:00:00 2001 From: Leander Date: Mon, 3 Apr 2017 23:28:04 +0200 Subject: [PATCH] T201704032327 --- Config.txt | 7 ++++--- Database.php | 11 ++++++++--- start.php | 10 ++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Config.txt b/Config.txt index 489039f..e16dedd 100644 --- a/Config.txt +++ b/Config.txt @@ -10,6 +10,7 @@ 11102 - User name already exists = start.php = -12101 - 0 questions wanted, no game without a question! -12102 - More questions wanted then existing -12103 - User ID not specified \ No newline at end of file +12101 - User ID not specified +12102 - 0 questions wanted, no game without a question! +12103 - More questions wanted then existing +12104 - User name not existing diff --git a/Database.php b/Database.php index 4d3d543..91cc663 100644 --- a/Database.php +++ b/Database.php @@ -42,6 +42,9 @@ class Database static function create_response($data, $error, $values = null) { $status_message = Database::create_status_code($error, $values); $meta = array('status'=>$error, 'message'=>$status_message); + if($error == 0){ + $data = ''; + } $response_element_json = json_encode(array('meta'=>$meta, 'data'=>$data), JSON_PRETTY_PRINT); return $response_element_json; } @@ -58,11 +61,13 @@ class Database case 11102: return 'User name already exists. Please pick a different name.'; case 12101: - return 'A game without a question is not possible. Please select new game length.'; + return 'User name not specified. This is an internal error.'; case 12102: - return 'We only have '.$values[0].' questions. Please select new game length.'; + return 'A game without a question is not possible. Please select new game length.'; case 12103: - return 'No user ID specified. Sebastian, please fix the app.'; + return 'Sorry, we only have '.$values[0].' questions. Please try again.'; + case 12104: + return 'The specified user name does not exist.'; default: return 'An unknown error occurred.'; } diff --git a/start.php b/start.php index 1f21818..d537b9a 100644 --- a/start.php +++ b/start.php @@ -24,11 +24,11 @@ } if($uid == '') { - $error = 12103; - } else if($length == 0) { $error = 12101; - } else if($length > $questions_count) { + } else if($length == 0) { $error = 12102; + } else if($length > $questions_count) { + $error = 12103; $values = array($questions_count); } else { // Delete existing game for user. @@ -37,7 +37,9 @@ } // Create new game for user. $db->query('INSERT INTO "Games" ("ID") VALUES ($1)', array($uid)); - print_r(pg_last_error()); + if(preg_match('/violates foreign key constraint "Games_ID_fkey"/', pg_last_error()) == 1) { + $error = 12104; + } // Find questions for new game and insert into table. $questions = random_numbers(0, $questions_count-1, $length); foreach($questions as $question) {