From c51692198d51610387140c515317d109e9b4ae9f Mon Sep 17 00:00:00 2001 From: will Date: Fri, 10 Jul 2020 10:50:21 +0100 Subject: [PATCH 1/2] Enable segments to be deleted --- resources/views/segments/index.blade.php | 5 +++-- resources/views/segments/partials/actions.blade.php | 7 +++++++ routes/web.php | 2 +- .../Controllers/Segments/SegmentsController.php | 10 ++++++++++ src/Models/Segment.php | 5 +++++ src/Repositories/SegmentTenantRepository.php | 13 +++++++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 resources/views/segments/partials/actions.blade.php diff --git a/resources/views/segments/index.blade.php b/resources/views/segments/index.blade.php index a0552ae0..d6e6c74b 100644 --- a/resources/views/segments/index.blade.php +++ b/resources/views/segments/index.blade.php @@ -35,8 +35,9 @@ {{ $segment->subscribers_count }} - {{ __('Edit') }} + + @include('sendportal::segments.partials.actions') + @empty diff --git a/resources/views/segments/partials/actions.blade.php b/resources/views/segments/partials/actions.blade.php new file mode 100644 index 00000000..b16a16d0 --- /dev/null +++ b/resources/views/segments/partials/actions.blade.php @@ -0,0 +1,7 @@ +
+ @csrf + @method('DELETE') + {{ __('Edit') }} + +
diff --git a/routes/web.php b/routes/web.php index a671c7f6..e24b6c22 100644 --- a/routes/web.php +++ b/routes/web.php @@ -82,7 +82,7 @@ }); // Segments. - $appRouter->resource('segments', 'Segments\SegmentsController')->except(['show', 'destroy']); + $appRouter->resource('segments', 'Segments\SegmentsController')->except(['show']); // Workspace User Management. $appRouter->namespace('Workspaces') diff --git a/src/Http/Controllers/Segments/SegmentsController.php b/src/Http/Controllers/Segments/SegmentsController.php index 00f9f31e..7bec30e4 100644 --- a/src/Http/Controllers/Segments/SegmentsController.php +++ b/src/Http/Controllers/Segments/SegmentsController.php @@ -66,4 +66,14 @@ public function update(int $id, SegmentRequest $request): RedirectResponse return redirect()->route('sendportal.segments.index'); } + + /** + * @throws Exception + */ + public function destroy(int $id): RedirectResponse + { + $this->segmentRepository->destroy(auth()->user()->currentWorkspace()->id, $id); + + return redirect()->route('sendportal.segments.index'); + } } diff --git a/src/Models/Segment.php b/src/Models/Segment.php index 8748f8ce..b733a5f7 100644 --- a/src/Models/Segment.php +++ b/src/Models/Segment.php @@ -19,6 +19,11 @@ class Segment extends BaseModel 'subscribers' ]; + public function campaigns(): BelongsToMany + { + return $this->belongsToMany(Campaign::class); + } + /** * The workspace this segment belongs to. */ diff --git a/src/Repositories/SegmentTenantRepository.php b/src/Repositories/SegmentTenantRepository.php index 3a0804a8..c3c04ca8 100644 --- a/src/Repositories/SegmentTenantRepository.php +++ b/src/Repositories/SegmentTenantRepository.php @@ -37,4 +37,17 @@ public function syncSubscribers(Segment $segment, array $subscribers = []) { return $segment->subscribers()->sync($subscribers); } + + /** + * {@inheritDoc} + */ + public function destroy($workspaceId, $id): bool + { + $instance = $this->find($workspaceId, $id); + + $instance->subscribers()->detach(); + $instance->campaigns()->detach(); + + return $instance->delete(); + } } From 8d12c17e78bc677fa5533e7d6bd912ce48f72661 Mon Sep 17 00:00:00 2001 From: will Date: Fri, 10 Jul 2020 16:23:33 +0100 Subject: [PATCH 2/2] Added test --- tests/Feature/Segments/SegmentsControllerTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Feature/Segments/SegmentsControllerTest.php b/tests/Feature/Segments/SegmentsControllerTest.php index 2046f2ce..006f687c 100644 --- a/tests/Feature/Segments/SegmentsControllerTest.php +++ b/tests/Feature/Segments/SegmentsControllerTest.php @@ -101,5 +101,19 @@ function a_segment_is_updateable_by_an_authenticated_user() ]); } + /* @test */ + public function a_segment_can_be_deleted() + { + [$workspace, $user] = $this->createUserAndWorkspace(); + + $segment = factory(Segment::class)->create(['workspace_id' => $workspace->id]); + + $response = $this->actingAs($user) + ->delete(route('sendportal.segments.destroy', $segment->id)); + $response->assertRedirect(); + $this->assertDatabaseMissing('segments', [ + 'id' => $segment->id, + ]); + } }