Skip to content

Commit fca1c30

Browse files
juliusknorrjulien-nc
authored andcommitted
feat: Add delete task API
Signed-off-by: Julius Härtl <[email protected]>
1 parent 05a6a79 commit fca1c30

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

core/Controller/TextProcessingApiController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ public function getTask(int $id): DataResponse {
156156
}
157157
}
158158

159+
/**
160+
* This endpoint allows to delete a scheduled task for a user
161+
*
162+
* @param int $id The id of the task
163+
*
164+
* @return DataResponse<Http::STATUS_OK, array{task: CoreTextProcessingTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
165+
*
166+
* 200: Task returned
167+
* 404: Task not found
168+
*/
169+
#[NoAdminRequired]
170+
public function deleteTask(int $id): DataResponse {
171+
try {
172+
$task = $this->textProcessingManager->getUserTask($id, $this->userId);
173+
174+
$this->textProcessingManager->deleteTask($task);
175+
176+
$json = $task->jsonSerialize();
177+
178+
return new DataResponse([
179+
'task' => $json,
180+
]);
181+
} catch (NotFoundException $e) {
182+
return new DataResponse(['message' => $this->l->t('Task not found')], Http::STATUS_NOT_FOUND);
183+
} catch (\RuntimeException $e) {
184+
return new DataResponse(['message' => $this->l->t('Internal error')], Http::STATUS_INTERNAL_SERVER_ERROR);
185+
}
186+
}
187+
188+
159189
/**
160190
* This endpoint returns a list of tasks of a user that are related
161191
* with a specific appId and optionally with an identifier

core/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
['root' => '/textprocessing', 'name' => 'TextProcessingApi#taskTypes', 'url' => '/tasktypes', 'verb' => 'GET'],
150150
['root' => '/textprocessing', 'name' => 'TextProcessingApi#schedule', 'url' => '/schedule', 'verb' => 'POST'],
151151
['root' => '/textprocessing', 'name' => 'TextProcessingApi#getTask', 'url' => '/task/{id}', 'verb' => 'GET'],
152+
['root' => '/textprocessing', 'name' => 'TextProcessingApi#deleteTask', 'url' => '/task/{id}', 'verb' => 'DELETE'],
152153
['root' => '/textprocessing', 'name' => 'TextProcessingApi#listTasksByApp', 'url' => '/tasks/app/{appId}', 'verb' => 'GET'],
153154
],
154155
]);

lib/private/TextProcessing/Manager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OC\AppFramework\Bootstrap\Coordinator;
2929
use OC\TextProcessing\Db\Task as DbTask;
3030
use OCP\IConfig;
31+
use OCP\TextProcessing\Task;
3132
use OCP\TextProcessing\Task as OCPTask;
3233
use OC\TextProcessing\Db\TaskMapper;
3334
use OCP\AppFramework\Db\DoesNotExistException;
@@ -177,6 +178,17 @@ public function scheduleTask(OCPTask $task): void {
177178
]);
178179
}
179180

181+
/**
182+
* @inheritDoc
183+
*/
184+
public function deleteTask(Task $task): void {
185+
$taskEntity = DbTask::fromPublicTask($task);
186+
$this->taskMapper->delete($taskEntity);
187+
$this->jobList->remove(TaskBackgroundJob::class, [
188+
'taskId' => $task->getId()
189+
]);
190+
}
191+
180192
/**
181193
* Get a task from its id
182194
*

lib/public/TextProcessing/IManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function runTask(Task $task): string;
7272
*/
7373
public function scheduleTask(Task $task) : void;
7474

75+
/**
76+
* Delete a task that has been scheduled before
77+
*
78+
* @param Task $task The task to delete
79+
* @since 27.1.0
80+
*/
81+
public function deleteTask(Task $task): void;
82+
7583
/**
7684
* @param int $id The id of the task
7785
* @return Task

0 commit comments

Comments
 (0)