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 @@
+
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();
+ }
}
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,
+ ]);
+ }
}