fix: Allow optional nullable parameters in the API

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2023-11-08 09:13:08 +01:00
parent c405a5729a
commit 27d5861456
3 changed files with 57 additions and 3 deletions

View File

@@ -25,6 +25,7 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use OCA\Deck\Model\OptionalNullableValue;
use OCA\Deck\Service\AssignmentService; use OCA\Deck\Service\AssignmentService;
use OCA\Deck\Service\CardService; use OCA\Deck\Service\CardService;
use OCP\AppFramework\ApiController; use OCP\AppFramework\ApiController;
@@ -104,7 +105,8 @@ class CardApiController extends ApiController {
* *
* Update a card * Update a card
*/ */
public function update($title, $type, $owner, $description = '', $order = 0, $duedate = null, $archived = null, ?bool $done = null) { public function update($title, $type, $owner, $description = '', $order = 0, $duedate = null, $archived = null) {
$done = array_key_exists('done', $this->request->getParams()) ? new OptionalNullableValue($this->request->getParam('done', null)) : null;
$card = $this->cardService->update($this->request->getParam('cardId'), $title, $this->request->getParam('stackId'), $type, $owner, $description, $order, $duedate, 0, $archived, $done); $card = $this->cardService->update($this->request->getParam('cardId'), $title, $this->request->getParam('stackId'), $type, $owner, $description, $order, $duedate, 0, $archived, $done);
return new DataResponse($card, HTTP::STATUS_OK); return new DataResponse($card, HTTP::STATUS_OK);
} }

View File

@@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*/
namespace OCA\Deck\Model;
/**
* This is a helper abstraction to allow usage of optional parameters
* which hold a nullable value. The actual null value of the parameter
* is used to indicate if it has been set or not. The containing value
* will then still allow having null as a value
*
* Example use case: Have a nullable database column,
* but only update it if it is passed
*
* @template T
*/
class OptionalNullableValue {
/** @var ?T */
private mixed $value;
/** @param ?T $value */
public function __construct(mixed $value) {
$this->value = $value;
}
/** @return ?T */
public function getValue(): mixed {
return $this->value;
}
}

View File

@@ -43,6 +43,7 @@ use OCA\Deck\Event\CardCreatedEvent;
use OCA\Deck\Event\CardDeletedEvent; use OCA\Deck\Event\CardDeletedEvent;
use OCA\Deck\Event\CardUpdatedEvent; use OCA\Deck\Event\CardUpdatedEvent;
use OCA\Deck\Model\CardDetails; use OCA\Deck\Model\CardDetails;
use OCA\Deck\Model\OptionalNullableValue;
use OCA\Deck\NoPermissionException; use OCA\Deck\NoPermissionException;
use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\Notification\NotificationHelper;
use OCA\Deck\StatusException; use OCA\Deck\StatusException;
@@ -294,7 +295,7 @@ class CardService {
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws BadRequestException * @throws BadRequestException
*/ */
public function update($id, $title, $stackId, $type, $owner, $description = '', $order = 0, $duedate = null, $deletedAt = null, $archived = null, $done = null) { public function update($id, $title, $stackId, $type, $owner, $description = '', $order = 0, $duedate = null, $deletedAt = null, $archived = null, ?OptionalNullableValue $done = null) {
$this->cardServiceValidator->check(compact('id', 'title', 'stackId', 'type', 'owner', 'order')); $this->cardServiceValidator->check(compact('id', 'title', 'stackId', 'type', 'owner', 'order'));
$this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT); $this->permissionService->checkPermission($this->cardMapper, $id, Acl::PERMISSION_EDIT);
@@ -345,7 +346,7 @@ class CardService {
$card->setArchived($archived); $card->setArchived($archived);
} }
if ($done !== null) { if ($done !== null) {
$card->setDone($done); $card->setDone($done->getValue());
} }