Better error handling in StackApiController
Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
committed by
Julius Härtl
parent
7d9fc83dc9
commit
a14ca3d1f5
@@ -39,6 +39,7 @@ use OCA\Deck\Service\StackService;
|
|||||||
*/
|
*/
|
||||||
class StackApiController extends ApiController {
|
class StackApiController extends ApiController {
|
||||||
|
|
||||||
|
private $boardService;
|
||||||
private $service;
|
private $service;
|
||||||
private $userInfo;
|
private $userInfo;
|
||||||
|
|
||||||
@@ -47,9 +48,10 @@ class StackApiController extends ApiController {
|
|||||||
* @param IRequest $request
|
* @param IRequest $request
|
||||||
* @param StackService $service
|
* @param StackService $service
|
||||||
*/
|
*/
|
||||||
public function __construct($appName, IRequest $request, StackService $service) {
|
public function __construct($appName, IRequest $request, StackService $service, BoardService $boardService) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
$this->service = $service;
|
$this->service = $service;
|
||||||
|
$this->boardService = $boardService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,17 +61,17 @@ class StackApiController extends ApiController {
|
|||||||
*
|
*
|
||||||
* Return all of the stacks in the specified board.
|
* Return all of the stacks in the specified board.
|
||||||
*/
|
*/
|
||||||
public function index() {
|
public function index() {
|
||||||
|
$boardError = boardHasError($this->request->params['boardId']);
|
||||||
|
|
||||||
// validation check against the id.
|
if ($boardError) {
|
||||||
if (is_numeric($this->request->params['boardId']) === false) {
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
return new DataResponse("board id must be a number", HTTP::STATUS_BAD_REQUEST);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$stacks = $this->service->findAll($this->request->params['boardId']);
|
|
||||||
|
|
||||||
|
$stacks = $this->service->findAll($this->request->params['boardId']);
|
||||||
|
|
||||||
if ($stacks === false || $stacks === null) {
|
if ($stacks === false || $stacks === null) {
|
||||||
return new DataResponse("No Stacks Found", HTTP::STATUS_NOT_FOUND);
|
return new DataResponse('No Stacks Found', HTTP::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DataResponse($stacks, HTTP::STATUS_OK);
|
return new DataResponse($stacks, HTTP::STATUS_OK);
|
||||||
@@ -87,13 +89,14 @@ class StackApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function create($title, $order) {
|
public function create($title, $order) {
|
||||||
|
|
||||||
// validation check against the id.
|
$boardError = boardHasError($this->request->params['boardId']);
|
||||||
if (is_numeric($this->request->params['boardId']) === false) {
|
|
||||||
return new DataResponse("board id must be a number", HTTP::STATUS_BAD_REQUEST);
|
if ($boardError) {
|
||||||
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($order) === false) {
|
if (is_numeric($order) === false) {
|
||||||
return new DataResponse("order must be a number", HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse('order must be a number', HTTP::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -118,52 +121,77 @@ class StackApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function update($title, $order) {
|
public function update($title, $order) {
|
||||||
|
|
||||||
if (is_numeric($this->request->params['boardId']) === false) {
|
$boardError = boardHasError($this->request->params['boardId']);
|
||||||
return new DataResponse("board id must be a number", HTTP::STATUS_BAD_REQUEST);
|
|
||||||
|
if ($boardError) {
|
||||||
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($this->request->params['stackId']) === false) {
|
if (is_numeric($this->request->params['stackId']) === false) {
|
||||||
return new DataResponse("stack id must be a number", HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($order) === false) {
|
if (is_numeric($order) === false) {
|
||||||
return new DataResponse("order must be a number", HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse('order must be a number', HTTP::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$stack = $this->service->update(
|
$stack = $this->service->update($this->request->params['stackId'], $title, $this->request->params['boardId'], $order);
|
||||||
$this->request->params['stackId'],
|
|
||||||
$title,
|
if ($stack === false || $stack === null) {
|
||||||
$this->request->params['boardId'],
|
return new DataResponse('Stack not found', HTTP::STATUS_NOT_FOUND);
|
||||||
$order);
|
}
|
||||||
} catch (StatusException $e) {
|
|
||||||
$errorMessage['error'] = $e->getMessage();
|
return new DataResponse($stack, HTTP::STATUS_OK);
|
||||||
return new DataResponse($errorMessage, HTTP::STATUS_INTERNAL_SERVER_ERROR);
|
} catch (StatusException $e) {
|
||||||
|
return new DataResponse($e->getMessage(), HTTP::STATUS_INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DataResponse($stack, HTTP::STATUS_OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
* @CORS
|
* @CORS
|
||||||
* @NoCSRFRequired
|
* @NoCSRFRequired
|
||||||
*
|
*
|
||||||
* Delete the stack specified by $this->request->params['stackId']. Return the board that was deleted.
|
* Delete the stack specified by $this->request->params['stackId'].
|
||||||
*/
|
*/
|
||||||
public function delete() {
|
public function delete() {
|
||||||
|
|
||||||
|
$boardError = boardHasError($this->request->params['boardId']);
|
||||||
|
|
||||||
|
if ($boardError) {
|
||||||
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
|
}
|
||||||
|
|
||||||
if (is_numeric($this->request->params['stackId']) === false) {
|
if (is_numeric($this->request->params['stackId']) === false) {
|
||||||
return new DataResponse("stack id must be a number", HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stack = $this->service->delete($this->request->params['stackId']);
|
$stack = $this->service->delete($this->request->params['stackId']);
|
||||||
|
|
||||||
if ($stack == false || $stack == null) {
|
if ($stack == false || $stack == null) {
|
||||||
return new DataResponse("Stack Not Found", HTTP::STATUS_NOT_FOUND);
|
return new DataResponse('Stack Not Found', HTTP::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DataResponse($stack, HTTP::STATUS_OK);
|
return new DataResponse($stack, HTTP::STATUS_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function boardHasError($boardId) {
|
||||||
|
if (is_numeric($boardId) === false) {
|
||||||
|
$error['message'] = 'Board id must be a number';
|
||||||
|
$error['status'] = HTTP::STATUS_BAD_REQUEST;
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
|
||||||
|
$board = $this->boardService->find($boardId);
|
||||||
|
|
||||||
|
if ($board === false || $board === null) {
|
||||||
|
$error['message'] = 'Board does not exist';
|
||||||
|
$error['status'] = HTTP::STATUS_NOT_FOUND;
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user