Delete Submission

Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>
This commit is contained in:
Jonas Rittershofer 2020-04-30 12:02:23 +02:00
commit 8a96e16413
6 changed files with 84 additions and 6 deletions

View file

@ -714,4 +714,31 @@ class ApiController extends Controller {
return new Http\JSONResponse([]);
}
/**
* @NoAdminRequired
*/
public function deleteSubmission(int $id): Http\JSONResponse {
$this->logger->debug('Delete Submission: {id}', [
'id' => $id,
]);
try {
$submission = $this->submissionMapper->findById($id);
$form = $this->formMapper->findById($submission->getFormId());
} catch (IMapperException $e) {
$this->logger->debug('Could not find form or submission');
return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST);
}
if ($form->getOwnerId() !== $this->userId) {
$this->logger->debug('This form is not owned by the current user');
return new Http\JSONResponse([], Http::STATUS_FORBIDDEN);
}
// Delete submission (incl. Answers)
$this->submissionMapper->delete($submission);
return new Http\JSONResponse($id);
}
}

View file

@ -64,6 +64,24 @@ class SubmissionMapper extends QBMapper {
return $this->findEntities($qb);
}
/**
* @param Integer $id
* @return Submission
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
*/
public function findById(int $id): Submission {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
/**
* @param int $formId
* @throws DoesNotExistException if not found