Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add delete task API
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and julien-nc committed Aug 7, 2023
commit fca1c309a025e34cc5d635766796d9e5b4f9386b
30 changes: 30 additions & 0 deletions core/Controller/TextProcessingApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Http::STATUS_OK, array{task: CoreTextProcessingTask}, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 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
Expand Down
1 change: 1 addition & 0 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
]);
Expand Down
12 changes: 12 additions & 0 deletions lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
8 changes: 8 additions & 0 deletions lib/public/TextProcessing/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down