From b8321efb5616ebd8e52e72c047aaa4dcd0d7df5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 4 Aug 2023 15:41:35 +0200 Subject: [PATCH] feat: Add delete task API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../TextProcessingApiController.php | 30 +++++++++++++++++++ core/routes.php | 1 + lib/private/TextProcessing/Manager.php | 12 ++++++++ lib/public/TextProcessing/IManager.php | 8 +++++ 4 files changed, 51 insertions(+) diff --git a/core/Controller/TextProcessingApiController.php b/core/Controller/TextProcessingApiController.php index c713a70481c07..e1d7788be2701 100644 --- a/core/Controller/TextProcessingApiController.php +++ b/core/Controller/TextProcessingApiController.php @@ -156,6 +156,36 @@ public function getTask(int $id): DataResponse { } } + /** + * This endpoint allows to delete a scheduled task for a user + * + * @param int $id The id of the task + * + * @return DataResponse|DataResponse + * + * 200: Task returned + * 404: Task not found + */ + #[NoAdminRequired] + public function deleteTask(int $id): DataResponse { + try { + $task = $this->textProcessingManager->getUserTask($id, $this->userId); + + $this->textProcessingManager->deleteTask($task); + + $json = $task->jsonSerialize(); + + return new DataResponse([ + 'task' => $json, + ]); + } catch (NotFoundException $e) { + return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND); + } catch (\RuntimeException $e) { + return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** * This endpoint returns a list of tasks of a user that are related * with a specific appId and optionally with an identifier diff --git a/core/routes.php b/core/routes.php index 90578ee6f0d2f..ad8638e0b1e4e 100644 --- a/core/routes.php +++ b/core/routes.php @@ -149,6 +149,7 @@ ['root' => '/textprocessing', 'name' => 'TextProcessingApi#taskTypes', 'url' => '/tasktypes', 'verb' => 'GET'], ['root' => '/textprocessing', 'name' => 'TextProcessingApi#schedule', 'url' => '/schedule', 'verb' => 'POST'], ['root' => '/textprocessing', 'name' => 'TextProcessingApi#getTask', 'url' => '/task/{id}', 'verb' => 'GET'], + ['root' => '/textprocessing', 'name' => 'TextProcessingApi#deleteTask', 'url' => '/task/{id}', 'verb' => 'DELETE'], ['root' => '/textprocessing', 'name' => 'TextProcessingApi#listTasksByApp', 'url' => '/tasks/app/{appId}', 'verb' => 'GET'], ], ]); diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index c8302f1e8df58..b9cb06c298e25 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -28,6 +28,7 @@ use OC\AppFramework\Bootstrap\Coordinator; use OC\TextProcessing\Db\Task as DbTask; use OCP\IConfig; +use OCP\TextProcessing\Task; use OCP\TextProcessing\Task as OCPTask; use OC\TextProcessing\Db\TaskMapper; use OCP\AppFramework\Db\DoesNotExistException; @@ -177,6 +178,17 @@ public function scheduleTask(OCPTask $task): void { ]); } + /** + * @inheritDoc + */ + public function deleteTask(Task $task): void { + $taskEntity = DbTask::fromPublicTask($task); + $this->taskMapper->delete($taskEntity); + $this->jobList->remove(TaskBackgroundJob::class, [ + 'taskId' => $task->getId() + ]); + } + /** * Get a task from its id * diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php index 00646528e68ac..dec0baba4bb74 100644 --- a/lib/public/TextProcessing/IManager.php +++ b/lib/public/TextProcessing/IManager.php @@ -72,6 +72,14 @@ public function runTask(Task $task): string; */ public function scheduleTask(Task $task) : void; + /** + * Delete a task that has been scheduled before + * + * @param Task $task The task to delete + * @since 27.1.0 + */ + public function deleteTask(Task $task): void; + /** * @param int $id The id of the task * @return Task