From e21bd0a7254d9f1ab6db98c39c5c8ed70203d3d4 Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Mon, 3 Apr 2023 19:21:25 +0200 Subject: [PATCH] Implement deleting chores in the backend --- appinfo/routes.php | 1 + lib/Controller/APIController.php | 19 +++++++++++++++++++ lib/Db/Chore.php | 2 ++ lib/Db/ChoreMapper.php | 17 +++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/appinfo/routes.php b/appinfo/routes.php index 295b92a..c1ffe41 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -24,6 +24,7 @@ return [ ['name' => 'API#getChores', 'url' => '/api/v1.0/team/{teamId}/chores', 'verb' => 'GET'], ['name' => 'API#postChores', 'url' => '/api/v1.0/team/{teamId}/chores', 'verb' => 'POST'], ['name' => 'API#patchChore', 'url' => '/api/v1.0/team/{teamId}/chores/{choreId}', 'verb' => 'PATCH'], + ['name' => 'API#deleteChore', 'url' => '/api/v1.0/team/{teamId}/chores/{choreId}', 'verb' => 'DELETE'], ['name' => 'API#getWorklog', 'url' => '/api/v1.0/team/{teamId}/work', 'verb' => 'GET'], ['name' => 'API#submitWork', 'url' => '/api/v1.0/team/{teamId}/work', 'verb' => 'POST'], diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php index 75a8cca..f9d3cfa 100644 --- a/lib/Controller/APIController.php +++ b/lib/Controller/APIController.php @@ -158,6 +158,25 @@ class APIController extends Controller { } } + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function deleteChore(int $teamId, int $choreId) { + try { + $team = $this->checkAccess($this->userId, $teamId); + $userId = $this->userId; + + // throws DoesNotExistException if chore does not belong to team: + $chore = $this->choreMapper->findById($team->id, $choreId); + + $this->choreMapper->markDeleted($chore->id); + return new DataResponse([], Http::STATUS_NO_CONTENT); + } catch (DoesNotExistException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND);; + } + } + /** * @NoAdminRequired * @NoCSRFRequired diff --git a/lib/Db/Chore.php b/lib/Db/Chore.php index 2a162b9..56cd8df 100644 --- a/lib/Db/Chore.php +++ b/lib/Db/Chore.php @@ -15,12 +15,14 @@ class Chore extends Entity implements JsonSerializable { protected $points; protected $due; protected $repeat; + protected $deleted; public function __construct() { $this->addType('id', 'integer'); $this->addType('teamId', 'integer'); $this->addType('points', 'integer'); $this->addType('due', 'datetimetz_immutable'); + $this->addType('deleted', 'bool'); } public function getDue(): string { diff --git a/lib/Db/ChoreMapper.php b/lib/Db/ChoreMapper.php index 20ce7d3..7309855 100644 --- a/lib/Db/ChoreMapper.php +++ b/lib/Db/ChoreMapper.php @@ -1,6 +1,7 @@ where( $qb->expr()->eq('team_id', $qb->createNamedParameter($teamId)) ) + ->andWhere( + 'deleted = false' + ) ->orderBy('due', 'ASC') ; return $this->findEntities($qb); } + public function markDeleted(int $choreId) { + $qb = $this->db->getQueryBuilder(); + + $qb->update('chores_chores') + ->set('deleted', $qb->expr()->literal(true, IQueryBuilder::PARAM_BOOL)) + ->where( + $qb->expr()->eq('id', $qb->createNamedParameter($choreId)) + ) + ; + + return $this->db->executeStatement($qb->getSQL(), $qb->getParameters()); + } + public function updateDueDate(int $choreId, \DateTimeImmutable $dueDate) { $due = $dueDate->format('c'); $qb = $this->db->getQueryBuilder(); -- 2.45.2