commit bffd44d01aee033d0c9937f8da1e625530267fe0
parent fbfc906f27e5503870f69bdc8f46b91362544564
Author: MTRNord <mtrnord1@gmail.com>
Date: Wed, 18 Mar 2015 19:46:44 +0100
prepare DBHandler
Diffstat:
2 files changed, 12 insertions(+), 70 deletions(-)
diff --git a/core/api/v1/DbHandler.class.php b/core/api/v1/DbHandler.class.php
@@ -211,7 +211,7 @@ class DbHandler {
* @param String $user_id user id to whom task belongs to
* @param String $task task text
*/
- public function createTask($user_id, $task) {
+ public function createPost($user_id, $task) {
$stmt = $this->conn->prepare("INSERT INTO tasks(task) VALUES(?)");
$stmt->bind_param("s", $task);
$result = $stmt->execute();
@@ -239,7 +239,7 @@ class DbHandler {
* Fetching single task
* @param String $task_id id of the task
*/
- public function getTask($task_id, $user_id) {
+ public function getPosts($task_id, $user_id) {
$stmt = $this->conn->prepare("SELECT t.id, t.task, t.status, t.created_at from tasks t, user_tasks ut WHERE t.id = ? AND ut.task_id = t.id AND ut.user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
if ($stmt->execute()) {
@@ -252,25 +252,12 @@ class DbHandler {
}
/**
- * Fetching all user tasks
- * @param String $user_id id of the user
- */
- public function getAllUserTasks($user_id) {
- $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
- $stmt->bind_param("i", $user_id);
- $stmt->execute();
- $tasks = $stmt->get_result();
- $stmt->close();
- return $tasks;
- }
-
- /**
* Updating task
* @param String $task_id id of the task
* @param String $task task text
* @param String $status task status
*/
- public function updateTask($user_id, $task_id, $task, $status) {
+ public function updatePost($user_id, $task_id, $task, $status) {
$stmt = $this->conn->prepare("UPDATE tasks t, user_tasks ut set t.task = ?, t.status = ? WHERE t.id = ? AND t.id = ut.task_id AND ut.user_id = ?");
$stmt->bind_param("siii", $task, $status, $task_id, $user_id);
$stmt->execute();
@@ -283,7 +270,7 @@ class DbHandler {
* Deleting a task
* @param String $task_id id of the task to delete
*/
- public function deleteTask($user_id, $task_id) {
+ public function deletePost($user_id, $task_id) {
$stmt = $this->conn->prepare("DELETE t FROM tasks t, user_tasks ut WHERE t.id = ? AND ut.task_id = t.id AND ut.user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
$stmt->execute();
@@ -292,20 +279,6 @@ class DbHandler {
return $num_affected_rows > 0;
}
- /* ------------- `user_tasks` table method ------------------ */
-
- /**
- * Function to assign a task to user
- * @param String $user_id id of the user
- * @param String $task_id id of the task
- */
- public function createUserTask($user_id, $task_id) {
- $stmt = $this->conn->prepare("INSERT INTO user_tasks(user_id, task_id) values(?, ?)");
- $stmt->bind_param("ii", $user_id, $task_id);
- $result = $stmt->execute();
- $stmt->close();
- return $result;
- }
}
diff --git a/core/api/v1/api.php b/core/api/v1/api.php
@@ -152,7 +152,7 @@ $app->get('/posts/:id', function($task_id) {
$db = new DbHandler();
// fetch task
- $result = $db->getTask($task_id, $user_id);
+ $result = $db->getPosts($task_id, $user_id);
if ($result != NULL) {
$response["error"] = false;
@@ -162,10 +162,9 @@ $app->get('/posts/:id', function($task_id) {
$response["createdAt"] = $result["created_at"];
echoRespnse(200, $response);
} else {
- echo $response;
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
- echoRespnse(200, $response);
+ echoRespnse(404, $response);
}
});
@@ -181,7 +180,7 @@ $app->get('/posts/', function() {
$db = new DbHandler();
// fetch task
- $result = $db->getTask($task_id, $user_id);
+ $result = $db->getPosts(Null, $user_id);
if ($result != NULL) {
$response["error"] = false;
@@ -191,10 +190,9 @@ $app->get('/posts/', function() {
$response["createdAt"] = $result["created_at"];
echoRespnse(200, $response);
} else {
- echo $response;
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
- echoRespnse(200, $response);
+ echoRespnse(404, $response);
}
});
@@ -202,35 +200,6 @@ $app->get('/posts/', function() {
* ------------------------ METHODS WITH AUTHENTICATION ------------------------
*/
-/**
- * Listing all tasks of particual user
- * method GET
- * url /tasks
- */
-$app->get('/tasks', 'authenticate', function() {
- global $user_id;
- $response = array();
- $db = new DbHandler();
-
- // fetching all user tasks
- $result = $db->getAllUserTasks($user_id);
-
- $response["error"] = false;
- $response["tasks"] = array();
-
- // looping through result and preparing tasks array
- while ($task = $result->fetch_assoc()) {
- $tmp = array();
- $tmp["id"] = $task["id"];
- $tmp["task"] = $task["task"];
- $tmp["status"] = $task["status"];
- $tmp["createdAt"] = $task["created_at"];
- array_push($response["tasks"], $tmp);
- }
-
- echoRespnse(200, $response);
- });
-
/**
* Creating new Post in db
@@ -238,7 +207,7 @@ $app->get('/tasks', 'authenticate', function() {
* params - name
* url - /addpost/
*/
-$app->Post('/addpost', 'authenticate', function() use ($app) {
+$app->Post('/createposts', 'authenticate', function() use ($app) {
// check for required params
verifyRequiredParams(array('task'));
@@ -249,7 +218,7 @@ $app->Post('/addpost', 'authenticate', function() use ($app) {
$db = new DbHandler();
// creating new task
- $task_id = $db->createTask($user_id, $task);
+ $task_id = $db->createPost($user_id, $task);
if ($task_id != NULL) {
$response["error"] = false;
@@ -281,7 +250,7 @@ $app->put('/posts/:id', 'authenticate', function($task_id) use($app) {
$response = array();
// updating task
- $result = $db->updateTask($user_id, $task_id, $task, $status);
+ $result = $db->updatePost($user_id, $task_id, $task, $status);
if ($result) {
// task updated successfully
$response["error"] = false;
@@ -304,7 +273,7 @@ $app->delete('/posts/:id', 'authenticate', function($task_id) use($app) {
$db = new DbHandler();
$response = array();
- $result = $db->deleteTask($user_id, $task_id);
+ $result = $db->deletePost($user_id, $task_id);
if ($result) {
// task deleted successfully
$response["error"] = false;