From a68e888654553d35091430b1688a908c1ed44f83 Mon Sep 17 00:00:00 2001 From: Ryan Fletcher Date: Fri, 13 Jul 2018 01:44:46 -0400 Subject: [PATCH] Started CardApiController Signed-off-by: Ryan Fletcher --- appinfo/routes.php | 4 + lib/Controller/CardApiController.php | 125 +++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 lib/Controller/CardApiController.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 881b25390..bedac90ed 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -85,11 +85,15 @@ return [ ['name' => 'board_api#update', 'url' => '/api/v1.0/boards/{boardId}', 'verb' => 'PUT'], ['name' => 'board_api#undo_delete', 'url' => '/api/v1.0/boards/{boardId}/undo_delete', 'verb' => 'POST'], + // TODO: Add in a get for the stack_api ['name' => 'stack_api#index', 'url' => '/api/v1.0/boards/{boardId}/stacks', 'verb' => 'GET'], ['name' => 'stack_api#create', 'url' => '/api/v1.0/boards/{boardId}/stacks', 'verb' => 'POST'], ['name' => 'stack_api#update', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}', 'verb' => 'PUT'], ['name' => 'stack_api#delete', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}', 'verb' => 'DELETE'], + ['name' => 'card_api#get', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}', 'verb' => 'GET'], + ['name' => 'card_api#create', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards', 'verb' => 'POST'], + ['name' => 'board_api#preflighted_cors', 'url' => '/api/v1.0/{path}','verb' => 'OPTIONS', 'requirements' => ['path' => '.+']], ] ]; diff --git a/lib/Controller/CardApiController.php b/lib/Controller/CardApiController.php new file mode 100644 index 000000000..5f2d5d7d9 --- /dev/null +++ b/lib/Controller/CardApiController.php @@ -0,0 +1,125 @@ + + * + * @author Ryan Fletcher + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + namespace OCA\Deck\Controller; + + use OCP\AppFramework\ApiController; + use OCP\AppFramework\Http; + use OCP\AppFramework\Http\DataResponse; + use OCP\IRequest; + + use OCA\Deck\Service\CardService; + + /** + * Class BoardApiController + * + * @package OCA\Deck\Controller + */ +class CardApiController extends ApiController { + private $cardService; + private $userId; + + /** + * @param string $appName + * @param IRequest $request + * @param BoardService $service + * @param $userId + */ + public function __construct($appName, IRequest $request, CardService $cardService, $userId) { + parent::__construct($appName, $request); + $this->cardService = $cardService; + $this->userId = $userId; + } + + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * + * 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); + } + + 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['cardId']) === false) { + return new DataResponse("card id must be a number", HTTP::STATUS_BAD_REQUEST); + } + + $card = $this->cardService->find($this->request->params['cardId']); + + if ($card === false || $card === null) { + return new DataResponse('Card not found', HTTP::STATUS_NOT_FOUND); + } + + return new DataResponse($card, HTTP::STATUS_OK); + } + + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * + * @params $title + * @params $type + * @params $order + * + * Get a specific card. + */ + 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); + } + + if (is_numeric($this->request->params['stackId']) === false) { + return new DataResponse("stack id must be a number", HTTP::STATUS_BAD_REQUEST); + } + + if ($title === false) { + return new DataResponse("title must be provided", HTTP::STATUS_BAD_REQUEST); + } + + if ($type === false) { + return new DataResponse("type must be provided", HTTP::STATUS_BAD_REQUEST); + } + + if (is_numeric($order) === false) { + return new DataResponse("order must be a number", HTTP::STATUS_BAD_REQUEST); + } + + try { + $card = $this->cardService->create($title, $this->request->params['stackId'], $type, $order, $this->userId); + } catch (StatusException $e) { + return new DataResponse($e->getMessage(), HTTP::STATUS_INTERNAL_SERVER_ERROR); + } + + return new DataResponse($card, HTTP::STATUS_OK); + } +} \ No newline at end of file