committing WIP (improving error validation across the api's)
Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
committed by
Julius Härtl
parent
a388d199dc
commit
2668f6b80c
@@ -28,6 +28,9 @@
|
|||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
|
||||||
|
use OCA\Deck\Controller\Helper\ApiHelper;
|
||||||
|
use OCA\Deck\Service\BoardService;
|
||||||
|
use OCA\Deck\Service\StackService;
|
||||||
use OCA\Deck\Service\CardService;
|
use OCA\Deck\Service\CardService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,7 +40,10 @@
|
|||||||
*/
|
*/
|
||||||
class CardApiController extends ApiController {
|
class CardApiController extends ApiController {
|
||||||
private $cardService;
|
private $cardService;
|
||||||
|
private $boardService;
|
||||||
|
private $stackService;
|
||||||
private $userId;
|
private $userId;
|
||||||
|
private $apiHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $appName
|
* @param string $appName
|
||||||
@@ -45,10 +51,13 @@ class CardApiController extends ApiController {
|
|||||||
* @param CardService $service
|
* @param CardService $service
|
||||||
* @param $userId
|
* @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);
|
parent::__construct($appName, $request);
|
||||||
|
$this->boardService = $boardService;
|
||||||
$this->cardService = $cardService;
|
$this->cardService = $cardService;
|
||||||
|
$this->stackService = $stackService;
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
|
$this->apiHelper = new ApiHelper();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,13 +68,14 @@ class CardApiController extends ApiController {
|
|||||||
* Get a specific card.
|
* Get a specific card.
|
||||||
*/
|
*/
|
||||||
public function get() {
|
public function get() {
|
||||||
|
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], $this->boardService);
|
||||||
if (is_numeric($this->request->params['boardId']) === false) {
|
if ($boardError) {
|
||||||
return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($this->request->params['stackId']) === false) {
|
$stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService);
|
||||||
return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
|
if ($stackError) {
|
||||||
|
return new DataResponse($stackError['message'], $stackError['status']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($this->request->params['cardId']) === false) {
|
if (is_numeric($this->request->params['cardId']) === false) {
|
||||||
@@ -94,12 +104,14 @@ class CardApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function create($title, $type = 'plain', $order = 999) {
|
public function create($title, $type = 'plain', $order = 999) {
|
||||||
|
|
||||||
if (is_numeric($this->request->params['boardId']) === false) {
|
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService);
|
||||||
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) {
|
$stackError = $this->apiHelper->entityHasError($this->request->params['stackId'], 'stack', $this->stackService);
|
||||||
return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
|
if ($stackError) {
|
||||||
|
return new DataResponse($stackError['message'], $stackError['status']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($title === false || $title === null) {
|
if ($title === false || $title === null) {
|
||||||
@@ -136,18 +148,20 @@ class CardApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function update($title, $type, $order, $description = null, $duedate = null, $archive = false, $assignedUserId = 0) {
|
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) {
|
if (is_numeric($this->request->params['cardId']) === false) {
|
||||||
return new DataResponse('card id must be a number', HTTP::STATUS_BAD_REQUEST);
|
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) {
|
if ($title === false || $title === null) {
|
||||||
return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
|
return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -203,18 +217,20 @@ class CardApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function delete() {
|
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) {
|
if (is_numeric($this->request->params['cardId']) === false) {
|
||||||
return new DataResponse('card id must be a number', HTTP::STATUS_BAD_REQUEST);
|
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 {
|
try {
|
||||||
$card = $this->cardService->delete($this->request->params['cardId']);
|
$card = $this->cardService->delete($this->request->params['cardId']);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
|||||||
@@ -28,17 +28,35 @@ use OCP\AppFramework\Http;
|
|||||||
|
|
||||||
class ApiHelper {
|
class ApiHelper {
|
||||||
|
|
||||||
public static function entityHasError($entityId, $entityName, $service) {
|
public static function boardHasError($boardId, $boardService) {
|
||||||
if (is_numeric($entityId) === false) {
|
if (is_numeric($boardId) === false) {
|
||||||
$error['message'] = $entityName . ' id must be a number';
|
$error['message'] = 'board id must be a number';
|
||||||
$error['status'] = HTTP::STATUS_BAD_REQUEST;
|
$error['status'] = HTTP::STATUS_BAD_REQUEST;
|
||||||
return $error;
|
return $error;
|
||||||
}
|
}
|
||||||
|
|
||||||
$entity = $service->find($entityId);
|
$board = $boardService->find($boardId);
|
||||||
|
|
||||||
if ($entity === false || $entity === null) {
|
if ($board === false || $board === null) {
|
||||||
$error['message'] = 'Board does not exist';
|
$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;
|
$error['status'] = HTTP::STATUS_NOT_FOUND;
|
||||||
return $error;
|
return $error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ 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 = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService );
|
$boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService );
|
||||||
|
|
||||||
if ($boardError) {
|
if ($boardError) {
|
||||||
return new DataResponse($boardError['message'], $boardError['status']);
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
@@ -95,7 +95,7 @@ class StackApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function create($title, $order) {
|
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) {
|
if ($boardError) {
|
||||||
return new DataResponse($boardError['message'], $boardError['status']);
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
@@ -127,7 +127,7 @@ class StackApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function update($title, $order) {
|
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) {
|
if ($boardError) {
|
||||||
return new DataResponse($boardError['message'], $boardError['status']);
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
@@ -163,7 +163,7 @@ class StackApiController extends ApiController {
|
|||||||
*/
|
*/
|
||||||
public function delete() {
|
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) {
|
if ($boardError) {
|
||||||
return new DataResponse($boardError['message'], $boardError['status']);
|
return new DataResponse($boardError['message'], $boardError['status']);
|
||||||
|
|||||||
@@ -89,6 +89,10 @@ class StackService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function find($stackId) {
|
||||||
|
throw new \Exception('Not yet implemented');
|
||||||
|
}
|
||||||
|
|
||||||
public function findAll($boardId) {
|
public function findAll($boardId) {
|
||||||
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
|
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
|
||||||
$stacks = $this->stackMapper->findAll($boardId);
|
$stacks = $this->stackMapper->findAll($boardId);
|
||||||
|
|||||||
Reference in New Issue
Block a user