Skip to content

Commit eb6b7a5

Browse files
committed
update event delete logic to soft delete --makes more sense
1 parent 92f5b2c commit eb6b7a5

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

app/src/Controller/Api/EventsController.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace App\Controller\Api;
55

6+
use App\Model\Entity\Event;
67
use Cake\Datasource\Exception\RecordNotFoundException;
78

89
class EventsController extends ApiController
@@ -145,7 +146,6 @@ public function update()
145146
public function delete(string $id)
146147
{
147148
$this->validateRequest('DELETE');
148-
// can do this in the cakephp5 middleware but to make it simple
149149
if (!$this->authenticate()) {
150150
return;
151151
}
@@ -162,20 +162,22 @@ public function delete(string $id)
162162
return;
163163
}
164164

165-
if (!$this->Events->delete($event)) {
165+
$event->set('status', Event::STATUS_CANCELLED);
166+
167+
if ($this->Events->save($event)) {
166168
$this->buildResponse([
167-
'success' => false,
168-
'message' => 'Unable to delete event',
169-
'errors' => $event->getErrors(),
170-
], 400);
169+
'success' => true,
170+
'message' => 'Event has been cancelled.',
171+
'errors' => null,
172+
], 200);
171173

172174
return;
173175
}
174176

175177
$this->buildResponse([
176-
'success' => true,
177-
'message' => 'Event deleted successfully',
178-
'errors' => null,
179-
], 200);
178+
'success' => false,
179+
'message' => 'Unable to cancel event.',
180+
'errors' => $event->getErrors(),
181+
], 400);
180182
}
181183
}

app/tests/TestCase/Controller/Api/EventsControllerTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace App\Test\TestCase\Controller\Api;
55

6+
use App\Model\Entity\Event;
67
use App\Model\Table\EventsTable;
78
use Cake\TestSuite\IntegrationTestTrait;
89
use Cake\TestSuite\TestCase;
@@ -125,7 +126,7 @@ public function testGetEvents(): void
125126
}
126127

127128
/**
128-
* Test deleting events (DELETE request)
129+
* Test cancelling events (DELETE request / soft delete)
129130
*/
130131
public function testDeleteEvent(): void
131132
{
@@ -147,20 +148,22 @@ public function testDeleteEvent(): void
147148
$this->post('/api/events/create', json_encode($createData));
148149
$response = json_decode((string)$this->_response->getBody(), true);
149150
$eventId = $response['event']['id'];
150-
151151
$this->configRequest([
152152
'headers' => [
153153
'Authorization' => self::HOMERS_FIXTURE_TOKEN,
154154
'Accept' => 'application/json',
155155
],
156156
]);
157-
158157
$this->delete('/api/events/delete/' . $eventId);
159158
$this->assertResponseCode(200);
160159
$this->assertContentType('application/json');
160+
161161
$response = json_decode((string)$this->_response->getBody(), true);
162162
$this->assertTrue($response['success']);
163-
$this->assertSame('Event deleted successfully', $response['message']);
163+
$this->assertSame('Event has been cancelled.', $response['message']);
164+
165+
$event = $this->Events->get($eventId);
166+
$this->assertSame(Event::STATUS_CANCELLED, $event->status);
164167
}
165168

166169
/**

0 commit comments

Comments
 (0)