Initial Boards API implementation

Created initial Activity model and DbLogger class for logging changes to
the database (to be used in activity displays). Includes initial unit
tests for Boards implementation.
This commit is contained in:
Matthew Ross 2016-04-23 15:17:37 -04:00
parent 7a59216451
commit cbcf132955
8 changed files with 170 additions and 8 deletions

View File

@ -1,12 +1,15 @@
<?php
abstract class BaseController {
protected $apiJson;
protected $logger;
protected $dbLogger;
protected $container;
public function __construct($container) {
$this->apiJson = new ApiJson();
$this->logger = $container->get('logger');
$this->dbLogger = new DbLogger();
$this->container = $container;
}

View File

@ -39,12 +39,78 @@ class Boards extends BaseController {
}
public function addBoard($request, $response, $args) {
$board = Board::fromJson($this->container, $request->getBody());
$board->save();
if ($board->id === 0) {
$this->logger->addError('Add Board: ', [$board]);
$this->apiJson->addAlert('error', 'Error adding board. ' .
'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,
' added board ' . $board->name, '', $board, 'board', $board->id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success',
'Board ' . $board->name . ' added.');
return $this->jsonResponse($response);
}
public function updateBoard($request, $response, $args) {
$board = new Board($this->container, (int)$args['id']);
$update = Board::fromJson($request->getBody());
if ($board->id !== $update->id) {
$this->logger->addError('Update Board: ', [$board, $update]);
$this->apiJson->addAlert('error', 'Error updating board. ' .
'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,
' updated board ' . $update->name, $board, $update,
'board', $board->id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success',
'Board ' . $update->name . ' updated.');
return $this->jsonResponse($response);
}
public function removeBoard($request, $response, $args) {
$id = (int)$args['id'];
$board = new Board($this->container, $id);
if ($board->id !== $id) {
$this->logger->addError('Remove Board: ', [$board]);
$this->apiJson->addAlert('error', 'Error removing board. ' .
'No board found for ID ' . $id . '.');
return $this->jsonResponse($response);
}
$before = $board;
$board->delete();
// TODO: Get existing user to log user_id and name
$this->dbLogger->logChange($this->container, 0,
' removed board ' . $before->name, $before, '', 'board', $id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success',
'Board ' . $before->name . ' updated.');
return $this->jsonResponse($response);
}
}

View File

@ -0,0 +1,29 @@
<?php
class Activity extends BaseModel {
public $id = 0;
public $user_id = '';
public $log_text ='';
public $before = '';
public $after = '';
public $item_type = '';
public $item_id = 0;
public function __construct($container, $id = 0) {
parent::__construct('activity', $id, $container);
}
public static function fromBean($container, $bean) {
$instance = new self($container, 0);
$instance->loadFromBean($bean);
return $instance;
}
public function updateBean() {
}
public function loadFromBean($bean) {
}
}

View File

@ -15,8 +15,13 @@ abstract class BaseModel {
public function save() {
$this->updateBean();
R::store($this->bean);
$this->loadFromBean($this->bean);
try {
R::store($this->bean);
$this->loadFromBean($this->bean);
} catch (Exception $ex) {
$this->logger->addError('Save Error: ', [$this->bean]);
}
}
public function delete() {

View File

@ -22,11 +22,21 @@ class Board extends BaseModel {
return $instance;
}
public static function fromJson($container, $json) {
$instance = new self($container, 0, true);
$instance->loadFromJson($json);
return $instance;
}
public function updateBean() {
}
public function loadFromBean($bean) {
}
public function loadFromJson($json) {
}
}

View File

@ -0,0 +1,18 @@
<?php
class DbLogger {
public static function logChange($container, $user_id, $log_text, $before,
$after, $item_type, $item_id) {
$activity = new Activity($container);
$activity->user_id = $user_id;
$activity->log_text = $log_text;
$activity->before = $before;
$activity->after = $after;
$activity->item_type = $item_type;
$activity->item_id = $item_id;
$activity->save();
}
}

View File

@ -2,6 +2,7 @@
require_once 'Mocks.php';
class BoardsTest extends PHPUnit_Framework_TestCase {
private $boards;
public static function setupBeforeClass() {
try {
@ -15,19 +16,19 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
}
}
public function testGetAllBoards() {
$boards = new Boards(new ContainerMock());
public function setup() {
$this->boards = new Boards(new ContainerMock());
}
public function testGetAllBoards() {
$expected = new ApiJson();
$expected->addAlert('info', 'No boards in database.');
$this->assertEquals($expected,
$boards->getAllBoards(null, new ResponseMock(), null));
$this->boards->getAllBoards(null, new ResponseMock(), null));
}
public function testGetBoard() {
$boards = new Boards(new ContainerMock());
$expected = new ApiJson();
$expected->addAlert('error', 'No board found for ID 1.');
@ -35,7 +36,29 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
$args['id'] = '1';
$this->assertEquals($expected,
$boards->getBoard(null, new ResponseMock(), $args));
$this->boards->getBoard(null, new ResponseMock(), $args));
}
public function testAddBoard() {
$expected = new ApiJson();
$expected->addAlert('error', 'Error adding board. ' .
'Please check your entries and try again.');
$this->assertEquals($expected,
$this->boards->addBoard(new RequestMock(),
new ResponseMock(), null));
}
public function testRemoveBoard() {
$expected = new ApiJson();
$expected->addAlert('error', 'Error removing board. ' .
'No board found for ID 1.');
$args = [];
$args['id'] = '1';
$this->assertEquals($expected,
$this->boards->removeBoard(null, new ResponseMock(), $args));
}
}

View File

@ -27,6 +27,14 @@ class ContainerMock {
}
class RequestMock {
public function getBody() {
return '{}';
}
}
class ResponseMock {
public function withJson($apiJson) {