diff --git a/backend/Db.php b/backend/Db.php index 008e20a..babbaab 100644 --- a/backend/Db.php +++ b/backend/Db.php @@ -1,11 +1,16 @@ dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password'); $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } + public function execute ($query, $params = array()) { + $stmt = $this->dbh->prepare($query); + return $stmt->execute($params); + } + public function query ($query, $params = array()) { $stmt = $this->dbh->prepare($query); $stmt->execute($params); diff --git a/backend/Response.php b/backend/Response.php index 28ed296..7f9986f 100644 --- a/backend/Response.php +++ b/backend/Response.php @@ -1,8 +1,14 @@ key = 'ˈkɑːn.læŋ.ɪŋ_4eva'; } diff --git a/backend/User.php b/backend/User.php index 18afd73..59fcdf7 100644 --- a/backend/User.php +++ b/backend/User.php @@ -1,11 +1,11 @@ db = new Db(); $this->token = new Token(); } @@ -32,63 +32,58 @@ class User { return false; } + public function emailExists ($email) { + $query = 'SELECT * FROM users WHERE email=?'; + $user = $this->db->query($query, array($email)); + return $user->rowCount() > 0; + } + public function create ($email, $password) { $insert_user_query = 'INSERT INTO users (email, password) VALUES (?, ?)'; $password_hash = password_hash($password, PASSWORD_DEFAULT); - // Use a transaction to make sure all pieces are created successfully. - $this->db->dbh->beginTransaction(); - - $insert_user = $this->db->query($insert_user_query, array($email, $password_hash)); + $insert_user = $this->db->execute($insert_user_query, array($email, $password_hash)); if ($insert_user === true) { $new_user_id = $this->db->lastInsertId(); $token = $this->createDictionary($new_user_id); if ($token !== false) { - if ($this->db->dbh->commit()) { - return $token; - } + return $token; } } - $this->db->dbh->rollBack(); return false; } public function createDictionary ($user) { - $this->db->dbh->beginTransaction(); - $insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)"; - $insert_dictionary = $this->db->query($insert_dictionary_query); + $insert_dictionary = $this->db->execute($insert_dictionary_query); if ($insert_dictionary === true) { $new_dictionary_id = $this->db->lastInsertId(); $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)"; - $insert_linguistics = $this->db->query($insert_dictionary_query); + $insert_linguistics = $this->db->execute($insert_linguistics_query); if ($insert_linguistics === true) { if ($this->changeCurrentDictionary($user, $new_dictionary_id)) { - if ($this->db->dbh->commit()) { - $user_data = array( - 'id' => $user, - 'isMember' => $this->hasMembership($user['id']), - 'dictionary' => $new_dictionary_id, - ); - return $this->token->encode($user_data); - } + $user_data = array( + 'id' => $user, + 'isMember' => $this->hasMembership($user), + 'dictionary' => $new_dictionary_id, + ); + return $this->token->encode($user_data); } } } - $this->db->dbh->rollBack(); return false; } public function changeCurrentDictionary ($user, $dictionary) { $update_query = 'UPDATE users SET current_dictionary=? WHERE id=?'; - $update = $this->db->query($update_query, array($user, $dictionary)); + $update = $this->db->query($update_query, array($dictionary, $user)); if ($update->rowCount() > 0) { return true; } diff --git a/backend/index.php b/backend/index.php index bcf96e0..5a3c841 100644 --- a/backend/index.php +++ b/backend/index.php @@ -2,48 +2,61 @@ require_once('./Response.php'); require_once('./User.php'); -$action = $_POST['action']; -$token = $_POST['token']; +$inputJSON = file_get_contents('php://input'); +$request= json_decode($inputJSON, true); + +$action = isset($request['action']) ? $request['action'] : ''; +$token = isset($request['token']) ? $request['token'] : ''; switch ($action) { case 'login': { - if ($_POST['email'] && $_POST['password']) { + if (isset($request['email']) && isset($request['password'])) { $user = new User(); - $token = $user->logIn($_POST['email'], $_POST['password']); + $token = $user->logIn($request['email'], $request['password']); if ($token !== false) { - return Response::out(array( + return Response::json(array( 'data' => $token, 'error' => false, ), 200); } - return Response::out(array( + return Response::json(array( 'data' => 'Could not log in: incorrect data', 'error' => true, - ), 400); + ), 401); } - return Response::out(array( + return Response::json(array( 'data' => 'Could not log in: required information missing', 'error' => true, - ), 500); + ), 400); } case 'create-account': { - if ($_POST['email'] && $_POST['password']) { + if (isset($request['email']) && isset($request['password'])) { $user = new User(); - $token = $user->create($_POST['email'], $_POST['password']); - if ($token !== false) { - return Response::out(array( - 'data' => $token, - 'error' => false, - ), 200); + if (!$user->emailExists($request['email'])) { + $token = $user->create($request['email'], $request['password']); + if ($token !== false) { + return Response::json(array( + 'data' => $token, + 'error' => false, + ), 201); + } + return Response::json(array( + 'data' => 'Could not create account: database error', + 'error' => true, + ), 500); } - return Response::out(array( - 'data' => 'Could not create account: incorrect data', + return Response::json(array( + 'data' => 'Could not create account: duplicate email', 'error' => true, - ), 400); + ), 403); } - return Response::out(array( + return Response::json(array( 'data' => 'Could not create account: required information missing', 'error' => true, - ), 500); + ), 400); + } + + default: { + return Response::html('Hi!'); } } \ No newline at end of file