From dd1d4246fe3ad0851e7f5635d4842e1d30d1a30e Mon Sep 17 00:00:00 2001 From: Ryan Fletcher Date: Fri, 13 Jul 2018 19:07:08 -0400 Subject: [PATCH] Finished Error handling across the API Signed-off-by: Ryan Fletcher --- lib/Controller/CardApiController.php | 8 +++---- lib/Controller/Helper/ApiHelper.php | 32 ++++++--------------------- lib/Controller/LabelApiController.php | 29 +++++++++++++++--------- lib/Controller/StackApiController.php | 8 +++---- lib/Service/StackService.php | 1 + 5 files changed, 34 insertions(+), 44 deletions(-) diff --git a/lib/Controller/CardApiController.php b/lib/Controller/CardApiController.php index f91f97250..d4b1dce08 100644 --- a/lib/Controller/CardApiController.php +++ b/lib/Controller/CardApiController.php @@ -68,7 +68,7 @@ class CardApiController extends ApiController { * Get a specific card. */ public function get() { - $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], $this->boardService); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } @@ -104,7 +104,7 @@ class CardApiController extends ApiController { */ public function create($title, $type = 'plain', $order = 999) { - $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } @@ -148,7 +148,7 @@ class CardApiController extends ApiController { */ public function update($title, $type, $order, $description = null, $duedate = null, $archive = false, $assignedUserId = 0) { - $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } @@ -217,7 +217,7 @@ class CardApiController extends ApiController { */ public function delete() { - $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } diff --git a/lib/Controller/Helper/ApiHelper.php b/lib/Controller/Helper/ApiHelper.php index 8785dcbe0..81aa003a5 100644 --- a/lib/Controller/Helper/ApiHelper.php +++ b/lib/Controller/Helper/ApiHelper.php @@ -28,40 +28,22 @@ use OCP\AppFramework\Http; class ApiHelper { - public static function boardHasError($boardId, $boardService) { - if (is_numeric($boardId) === false) { - $error['message'] = 'board id must be a number'; + public static function entityHasError($entityId, $entityName, $service) { + if (is_numeric($entityId) === false) { + $error['message'] = $entityName . ' id must be a number'; $error['status'] = HTTP::STATUS_BAD_REQUEST; return $error; } - $board = $boardService->find($boardId); + $entity = $service->find($entityId); - if ($board === false || $board === null) { - $error['message'] = 'board does not exist'; + if ($entity === false || $entity === null) { + $error['message'] = $entityName . ' does not exist'; $error['status'] = HTTP::STATUS_NOT_FOUND; return $error; } return false; - } - - public static function stackHasError($stackId, $stackService) { - if (is_numeric($stackId) === false) { - $error['message'] = 'board id must be a number'; - $error['status'] = HTTP::STATUS_BAD_REQUEST; - return $error; - } - - $stack = $stackService->find($stackId); - - if ($stack === false || $stack === null) { - $error['message'] = 'stack does not exist'; - $error['status'] = HTTP::STATUS_NOT_FOUND; - return $error; - } - - return false; - } + } } \ No newline at end of file diff --git a/lib/Controller/LabelApiController.php b/lib/Controller/LabelApiController.php index c74cd1408..b2617d2f6 100644 --- a/lib/Controller/LabelApiController.php +++ b/lib/Controller/LabelApiController.php @@ -27,8 +27,9 @@ use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; - use OCA\Deck\Service\LabelService; +use OCA\Deck\Service\BoardService; +use OCA\Deck\Controller\Helper\ApiHelper; /** * Class BoardApiController @@ -46,10 +47,12 @@ class LabelApiController extends ApiController { * @param LabelService $service * @param $userId */ - public function __construct($appName, IRequest $request, LabelService $labelService, $userId) { + public function __construct($appName, IRequest $request, LabelService $labelService, BoardService $boardService, $userId) { parent::__construct($appName, $request); $this->labelService = $labelService; + $this->boardService = $boardService; $this->userId = $userId; + $this->apiHelper = new ApiHelper(); } /** @@ -61,9 +64,10 @@ class LabelApiController extends ApiController { */ public function get() { - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); - } + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); + } if (is_numeric($this->request->params['labelId']) === false) { return new DataResponse('label id must be a number', HTTP::STATUS_BAD_REQUEST); @@ -89,8 +93,9 @@ class LabelApiController extends ApiController { */ public function create($title, $color) { - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); } if ($title === false || $title === null) { @@ -121,8 +126,9 @@ class LabelApiController extends ApiController { */ public function update($title, $color) { - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); } if (is_numeric($this->request->params['labelId']) === false) { @@ -155,8 +161,9 @@ class LabelApiController extends ApiController { */ public function delete() { - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); } if (is_numeric($this->request->params['labelId']) === false) { diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index e00fbfa85..cba2383ed 100644 --- a/lib/Controller/StackApiController.php +++ b/lib/Controller/StackApiController.php @@ -66,7 +66,7 @@ class StackApiController extends ApiController { * Return all of the stacks in the specified board. */ public function index() { - $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -95,7 +95,7 @@ class StackApiController extends ApiController { */ public function create($title, $order) { - $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -127,7 +127,7 @@ class StackApiController extends ApiController { */ public function update($title, $order) { - $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); @@ -163,7 +163,7 @@ class StackApiController extends ApiController { */ public function delete() { - $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); + $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); diff --git a/lib/Service/StackService.php b/lib/Service/StackService.php index 72211f730..4d4b086b5 100644 --- a/lib/Service/StackService.php +++ b/lib/Service/StackService.php @@ -89,6 +89,7 @@ class StackService { } } + //TODO: Write this function so we can look up one stack id. public function find($stackId) { throw new \Exception('Not yet implemented'); }