1
0
Fork 0
mirror of https://github.com/Alamantus/Lexiconga.git synced 2025-07-08 16:44:16 +02:00

Provide some more explicit errors

This commit is contained in:
Robbie Antenesse 2018-06-17 14:52:58 -06:00
parent 9be58f5e72
commit 61b5525778
4 changed files with 36 additions and 13 deletions

View file

@ -1,19 +1,27 @@
<?php
class Db {
private $dbh;
public $last_error_info;
function __construct() {
$this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password');
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->last_error_info = null;
}
public function execute ($query, $params = array()) {
$stmt = $this->dbh->prepare($query);
return $stmt->execute($params);
if ($stmt->execute($params)) {
$this->last_error_info = null;
return true;
}
$this->last_error_info = $stmt->errorInfo();
return false;
}
public function query ($query, $params = array()) {
$stmt = $this->dbh->prepare($query);
$stmt->execute($params);
$this->last_error_info = $stmt->errorInfo();
return $stmt;
}

View file

@ -27,8 +27,8 @@ class Dictionary {
}
public function create ($user) {
$insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES ($user, " . time() . ")";
$insert_dictionary = $this->db->execute($insert_dictionary_query);
$insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES (?, ?)";
$insert_dictionary = $this->db->execute($insert_dictionary_query, array($user, time()));
if ($insert_dictionary === true) {
$new_dictionary_id = $this->db->lastInsertId();
@ -42,10 +42,16 @@ VALUES ($new_dictionary_id, ?, ?)";
if ($insert_linguistics === true) {
return $this->changeCurrent($user, $new_dictionary_id);
} else {
return array(
'error' => '"INSERT INTO dictionary_linguistics" failed: ' . $this->db->last_error_info[2],
);
}
}
return false;
return array(
'error' => '"INSERT INTO dictionaries" failed: ' . $this->db->last_error_info[2],
);
}
public function changeCurrent ($user, $dictionary) {

View file

@ -47,7 +47,7 @@ class User {
public function create ($email, $password, $user_data) {
$insert_user_query = 'INSERT INTO users (email, password, public_name, username, allow_email, created_on)
VALUES (?, ?, ?, ?, ?, '. time() .')';
VALUES (?, ?, ?, ?, ?, ?)';
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$insert_user = $this->db->execute($insert_user_query, array(
@ -56,13 +56,16 @@ VALUES (?, ?, ?, ?, ?, '. time() .')';
$user_data['publicName'] !== '' ? $user_data['publicName'] : null,
$user_data['username'] !== '' ? $user_data['username'] : null,
$user_data['allowEmail'] ? 1 : 0,
time(),
));
if ($insert_user === true) {
$new_user_id = $this->db->lastInsertId();
$new_dictionary = $this->dictionary->create($new_user_id);
if ($new_dictionary !== false) {
if (isset($new_dictionary['error'])) {
return $new_dictionary;
} else {
return array(
'token' => $this->generateUserToken($new_user_id, $new_dictionary),
'user' => $this->getUserData($new_user_id),
@ -70,7 +73,9 @@ VALUES (?, ?, ?, ?, ?, '. time() .')';
}
}
return false;
return array(
'error' => '"INSERT INTO users" failed: ' . $this->db->last_error_info[2],
);
}
public function setUserData ($token, $user_data) {
@ -119,14 +124,18 @@ VALUES (?, ?, ?, ?, ?, '. time() .')';
if ($user_data !== false) {
$id = $user_data->id;
$new_dictionary = $this->dictionary->create($id);
if ($new_dictionary !== false) {
if (!isset($new_dictionary['error'])) {
return array(
'token' => $this->generateUserToken($id, $new_dictionary),
'dictionary' => $this->getCurrentDictionary($token),
);
} else {
return $new_dictionary;
}
}
return false;
return array(
'error' => 'invalid token',
);
}
public function changeCurrentDictionary ($token, $dictionary_hash) {

View file

@ -34,14 +34,14 @@ switch ($action) {
$user = new User();
if (!$user->emailExists($request['email'])) {
$user_data = $user->create($request['email'], $request['password'], $request['userData']);
if ($user_data !== false) {
if (!isset($user_data['error'])) {
return Response::json(array(
'data' => $user_data,
'error' => false,
), 201);
}
return Response::json(array(
'data' => 'Could not create account: database error',
'data' => 'Could not create account: ' . $user_data['error'],
'error' => true,
), 500);
}
@ -127,14 +127,14 @@ switch ($action) {
if ($token !== false) {
$user = new User();
$new_data = $user->createNewDictionary($token);
if ($new_data !== false) {
if (!isset($new_data['error'])) {
return Response::json(array(
'data' => $new_data,
'error' => false,
), 200);
}
return Response::json(array(
'data' => 'Could not create dictionary: incorrect data',
'data' => 'Could not create dictionary: ' . $new_data['error'],
'error' => true,
), 401);
}