API controller implementation
This commit is contained in:
parent
06efbfd45e
commit
a11b640496
@ -85,7 +85,7 @@ class Boards extends BaseController {
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name updated board ' . $update->name,
|
||||
json_encode($board), json_encode($update),
|
||||
'board', $board->id);
|
||||
'board', $update->id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success',
|
||||
|
@ -29,6 +29,8 @@ class Columns extends BaseController {
|
||||
$this->logger->addError('Add Column: ', [$column]);
|
||||
$this->apiJson->addAlert('error', 'Error adding column. ' .
|
||||
'Please try again.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
|
103
src/api/controllers/Tasks.php
Normal file
103
src/api/controllers/Tasks.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
use RedBeanPHP\R;
|
||||
|
||||
class Tasks extends BaseController {
|
||||
|
||||
public function getTask($request, $response, $args) {
|
||||
$task = new Task($this->container, (int)$args['id']);
|
||||
|
||||
if ($task->id === 0) {
|
||||
$this->logger->addError('Attemt to load task ' .
|
||||
$args['id'] . ' failed.');
|
||||
$this->apiJson->addAlert('error', 'No task found for ID ' .
|
||||
$args['id'] . '.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addData($task);
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function addTask($request, $response, $args) {
|
||||
$task = new Task($this->container);
|
||||
$task->loadFromJson($request->getBody());
|
||||
|
||||
if (!$task->save()) {
|
||||
$this->logger->addError('Add Task: ', [$task]);
|
||||
$this->apiJson->addAlert('error', 'Error adding task. ' .
|
||||
'Please check your entries and try again.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name added task ' . $task.name . '.',
|
||||
'', json_encode($task), 'task', $task->id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success', 'Task ' .
|
||||
$task->title . ' added.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function updateTask($request, $response, $args) {
|
||||
$task = new Task($this->container, (int)$args['id']);
|
||||
$update = new Task($this->container);
|
||||
$update->loadFromJson($request->getBody());
|
||||
|
||||
if ($task->id !== $update->id) {
|
||||
$this->logger->addError('Update Task: ', [$task, $update]);
|
||||
$this->apiJson->addAlert('error', 'Error updating task ' .
|
||||
$task->title . '. Please try again.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$update->save();
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name updated task ' . $task->title,
|
||||
json_encode($task), json_encode($update),
|
||||
'task', $update->id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success', 'Task ' .
|
||||
$update->title . ' updated.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function removeTask($request, $response, $args) {
|
||||
$id = (int)$args['id'];
|
||||
$task = new Task($this->container, $id);
|
||||
|
||||
if ($task->id !== $id) {
|
||||
$this->logger->addError('Remove Task: ', [$task]);
|
||||
$this->apiJson->addAlert('error', 'Error removing task. ' .
|
||||
'No task found for ID ' . $id . '.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$before = $task;
|
||||
$task->delete();
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name removed task ' . $before->title,
|
||||
json_encode($before), '', 'task', $id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success',
|
||||
'Task ' . $before->title . ' removed.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,123 @@
|
||||
use RedBeanPHP\R;
|
||||
|
||||
class Users extends BaseController {
|
||||
|
||||
public function getAllUsers($request, $response, $args) {
|
||||
$userBeans = R::findAll('user');
|
||||
|
||||
if (count($userBeans)) {
|
||||
$this->apiJson->setSuccess();
|
||||
|
||||
foreach($userBeans as $bean) {
|
||||
$user = new User($this->container);
|
||||
$user->loadFromBean($bean);
|
||||
|
||||
$this->apiJson->addData($user);
|
||||
}
|
||||
} else {
|
||||
$this->logger->addInfo('No users in database.');
|
||||
$this->apiJson->addAlert('info', 'No users in database.');
|
||||
}
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function getUser($request, $response, $args) {
|
||||
$user = new User($this->container, (int)$args['id']);
|
||||
|
||||
if ($user->id === 0) {
|
||||
$this->logger->addError('Attempt to load user ' . $args['id'] .
|
||||
' failed.');
|
||||
$this->apiJson->addAlert('error', 'No user found for ID ' .
|
||||
$args['id'] . '.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addData($user);
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function addUser($request, $response, $args) {
|
||||
$user = new User($this->container);
|
||||
$user->loadFromJson($request->getBody());
|
||||
|
||||
if (!$user->save()) {
|
||||
$this->logger->addError('Add User: ', [$user]);
|
||||
$this->apiJson->addAlert('error', 'Error adding user. ' .
|
||||
'Please check your entries and try again.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name added user ' . $user->username . '.',
|
||||
'', json_encode($user), 'user', $user->id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success',
|
||||
'User ' . $user->username . ' added.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function updateUser($request, $response, $args) {
|
||||
$user = new User($this->container, (int)$args['id']);
|
||||
|
||||
$update = new User($this->container);
|
||||
$update->loadFromJson($request->getBody());
|
||||
|
||||
if ($user->id !== $update->id) {
|
||||
$this->logger->addError('Update User: ', [$user, $update]);
|
||||
$this->apiJson->addAlert('error', 'Error updating user. ' .
|
||||
'Please check your entries and try again.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$update->save();
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name updated user ' . $update->username,
|
||||
json_encode($user), json_encode($update),
|
||||
'user', $update->id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success',
|
||||
'User ' . $update->username . ' updated.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
public function removeUser($request, $response, $args) {
|
||||
$id = (int)$args['id'];
|
||||
$user = new User($this->container, $id);
|
||||
|
||||
if ($user->id !== $id) {
|
||||
$this->logger->addError('Remove User: ', [$user]);
|
||||
$this->apiJson->addAlert('error', 'Error removing user. ' .
|
||||
'No user found for ID ' . $id . '.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
||||
$before = $user;
|
||||
$user->delete();
|
||||
|
||||
// TODO: Get existing user to log user_id and name
|
||||
$this->dbLogger->logChange($this->container, 0,
|
||||
'$user->name removed user ' . $before->username,
|
||||
json_encode($before), '', 'user', $id);
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success',
|
||||
'User ' . $before->username . ' removed.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,28 +23,28 @@ $app->get ('/columns/{id}', 'Columns:getColumn');
|
||||
$app->post ('/columns', 'Columns:addColumn');
|
||||
$app->post ('/columns/{id}', 'Columns:updateColumn');
|
||||
$app->delete('/columns/{id}', 'Columns:removeColumn');
|
||||
/*
|
||||
$app->get ('/items/{id}', 'Items:getItem');
|
||||
$app->post ('/items', 'Items:addItem');
|
||||
$app->post ('/items/{id}', 'Items:updateItem');
|
||||
$app->delete('/items/{id}', 'Items:removeItem');
|
||||
|
||||
$app->get ('/tasks/{id}', 'Tasks:getTask');
|
||||
$app->post ('/tasks', 'Tasks:addTask');
|
||||
$app->post ('/tasks/{id}', 'Tasks:updateTask');
|
||||
$app->delete('/tasks/{id}', 'Tasks:removeTask');
|
||||
|
||||
$app->get ('/comments/{id}', 'Comments:getComment');
|
||||
$app->post ('/comments', 'Comments:addComment');
|
||||
$app->post ('/comments/{id}', 'Comments:updateComment');
|
||||
$app->delete('/comments/{id}', 'Comments:removeComment');
|
||||
*/
|
||||
|
||||
$app->get ('/attachments/{id}', 'Attachments:getAttachment');
|
||||
$app->post ('/attachments', 'Attachments:addAttachment');
|
||||
$app->post ('/attachments/{id}', 'Attachments:updateAttachment');
|
||||
$app->delete('/attachments/{id}', 'Attachments:removeAttachment');
|
||||
/*
|
||||
|
||||
$app->get ('/users', 'Users:getAllUsers');
|
||||
$app->get ('/users/{id}', 'Users:getUser');
|
||||
$app->post ('/users', 'Users:addUser');
|
||||
$app->post ('/users/{id}', 'Users:updateUser');
|
||||
$app->delete('/users/{id}', 'Users:removeUser');
|
||||
|
||||
/*
|
||||
$app->post('/authenticate', 'Users:authenticate');
|
||||
$app->post('/login', 'Users:login');
|
||||
$app->post('/logout', 'Users:logout');
|
||||
|
Reference in New Issue
Block a user