diff --git a/lib/Controller/CardApiController.php b/lib/Controller/CardApiController.php index c754dd84c..f91f97250 100644 --- a/lib/Controller/CardApiController.php +++ b/lib/Controller/CardApiController.php @@ -28,6 +28,9 @@ use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; + use OCA\Deck\Controller\Helper\ApiHelper; + use OCA\Deck\Service\BoardService; + use OCA\Deck\Service\StackService; use OCA\Deck\Service\CardService; /** @@ -37,7 +40,10 @@ */ class CardApiController extends ApiController { private $cardService; + private $boardService; + private $stackService; private $userId; + private $apiHelper; /** * @param string $appName @@ -45,10 +51,13 @@ class CardApiController extends ApiController { * @param CardService $service * @param $userId */ - public function __construct($appName, IRequest $request, CardService $cardService, $userId) { + public function __construct($appName, IRequest $request, CardService $cardService, BoardService $boardService, StackService $stackService, $userId) { parent::__construct($appName, $request); + $this->boardService = $boardService; $this->cardService = $cardService; + $this->stackService = $stackService; $this->userId = $userId; + $this->apiHelper = new ApiHelper(); } /** @@ -58,14 +67,15 @@ class CardApiController extends ApiController { * * Get a specific card. */ - public function get() { - - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + public function get() { + $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); } - if (is_numeric($this->request->params['stackId']) === false) { - return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); + $stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService); + if ($stackError) { + return new DataResponse($stackError['message'], $stackError['status']); } if (is_numeric($this->request->params['cardId']) === false) { @@ -94,12 +104,14 @@ class CardApiController extends ApiController { */ public function create($title, $type = 'plain', $order = 999) { - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); } - if (is_numeric($this->request->params['stackId']) === false) { - return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); + $stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService); + if ($stackError) { + return new DataResponse($stackError['message'], $stackError['status']); } if ($title === false || $title === null) { @@ -136,17 +148,19 @@ 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); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); + } + + $stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService); + if ($stackError) { + return new DataResponse($stackError['message'], $stackError['status']); + } + if (is_numeric($this->request->params['cardId']) === false) { return new DataResponse('card id must be a number', HTTP::STATUS_BAD_REQUEST); - } - - if (is_numeric($this->request->params['stackId']) === false) { - return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); - } - - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); - } + } if ($title === false || $title === null) { return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST); @@ -203,17 +217,19 @@ class CardApiController extends ApiController { */ public function delete() { + $boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); + if ($boardError) { + return new DataResponse($boardError['message'], $boardError['status']); + } + + $stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService); + if ($stackError) { + return new DataResponse($stackError['message'], $stackError['status']); + } + if (is_numeric($this->request->params['cardId']) === false) { return new DataResponse('card id must be a number', HTTP::STATUS_BAD_REQUEST); - } - - if (is_numeric($this->request->params['stackId']) === false) { - return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); - } - - if (is_numeric($this->request->params['boardId']) === false) { - return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); - } + } try { $card = $this->cardService->delete($this->request->params['cardId']); diff --git a/lib/Controller/Helper/ApiHelper.php b/lib/Controller/Helper/ApiHelper.php index f8f16828b..8785dcbe0 100644 --- a/lib/Controller/Helper/ApiHelper.php +++ b/lib/Controller/Helper/ApiHelper.php @@ -28,17 +28,35 @@ use OCP\AppFramework\Http; class ApiHelper { - public static function entityHasError($entityId, $entityName, $service) { - if (is_numeric($entityId) === false) { - $error['message'] = $entityName . ' id must be a number'; + public static function boardHasError($boardId, $boardService) { + if (is_numeric($boardId) === false) { + $error['message'] = 'board id must be a number'; $error['status'] = HTTP::STATUS_BAD_REQUEST; return $error; } - $entity = $service->find($entityId); + $board = $boardService->find($boardId); - if ($entity === false || $entity === null) { - $error['message'] = 'Board does not exist'; + if ($board === false || $board === null) { + $error['message'] = 'board 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; } diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index cba2383ed..e00fbfa85 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->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $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->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $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->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $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->entityHasError( $this->request->params['boardId'], 'board', $this->boardService ); + $boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); diff --git a/lib/Service/StackService.php b/lib/Service/StackService.php index b64766e79..72211f730 100644 --- a/lib/Service/StackService.php +++ b/lib/Service/StackService.php @@ -89,6 +89,10 @@ class StackService { } } + public function find($stackId) { + throw new \Exception('Not yet implemented'); + } + public function findAll($boardId) { $this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ); $stacks = $this->stackMapper->findAll($boardId);