Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Delete segment #36

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions resources/views/segments/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
</a>
</td>
<td>{{ $segment->subscribers_count }}</td>
<td><a class="btn btn-sm btn-light"
href="{{ route('sendportal.segments.edit', $segment->id) }}">{{ __('Edit') }}</a></td>
<td>
@include('sendportal::segments.partials.actions')
</td>
</tr>
@empty
<tr>
Expand Down
7 changes: 7 additions & 0 deletions resources/views/segments/partials/actions.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form action="{{ route('sendportal.segments.destroy', $segment->id) }}" method="POST">
@csrf
@method('DELETE')
<a href="{{ route('sendportal.segments.edit', $segment->id) }}"
class="btn btn-sm btn-light">{{ __('Edit') }}</a>
<button type="submit" class="btn btn-sm btn-light">{{ __('Delete') }}</button>
</form>
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions src/Http/Controllers/Segments/SegmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
5 changes: 5 additions & 0 deletions src/Models/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Segment extends BaseModel
'subscribers'
];

public function campaigns(): BelongsToMany
{
return $this->belongsToMany(Campaign::class);
}

/**
* The workspace this segment belongs to.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Repositories/SegmentTenantRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
14 changes: 14 additions & 0 deletions tests/Feature/Segments/SegmentsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}