(iv) Create a PUT method to update a specific task.
public function tasks_put() { if(! $this->put('title')) { $this->response(array('error' => 'Task title is required'), 400); } $data = array( 'title' => $this->put('title'), 'status' => $this->put('status') ); $this->task_model->update_task($this->put('id'), $data); $message = array('success' => $this->put('title').' Updated!'); $this->response($message, 200); }
This method will update task title and its status, whether it is completed or incomplete.
(v) Create a DELETE method to delete tasks.
public function tasks_delete($id=NULL) { if($id == NULL) { $message = array('error' => 'Missing delete data: id'); $this->response($message, 400); } else { $this->task_model->delete_task($id); $message = array('id' => $id, 'message' => 'DELETED!'); $this->response($message, 200); } }
Basically our CRUD functions are ready. You may try the API using CURL in command prompt. I’m using POSTMAN, a chrome plugin to test the API.
Feel free to drop your questions or comments in the comment section below.
To be continued: Part 2