Add: delete activity
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2023-02-25 19:12:07 +01:00
parent b303373b73
commit 8e382126fd
3 changed files with 106 additions and 4 deletions

View File

@ -2,14 +2,34 @@
namespace App\Activity\Actions;
use App\Activity;
use App\Member\Membership;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
class DestroyAction
{
use AsAction;
public function handle()
public function handle(Activity $activity): void
{
// ...
$activity->subactivities()->sync([]);
$activity->delete();
}
public function asController(Activity $activity): RedirectResponse
{
if (Membership::where('activity_id', $activity->id)->count()) {
throw ValidationException::withMessages(['activity' => 'Tätigkeit besitzt noch Mitglieder.']);
}
if ($activity->hasNami) {
throw ValidationException::withMessages(['activity' => 'Tätigkeit ist in NaMi.']);
}
$this->handle($activity);
return redirect()->route('activity.index');
}
}

View File

@ -1,5 +1,16 @@
<template>
<div class="pb-6">
<popup heading="Bitte bestätigen" v-if="deleting !== null">
<div>
<p class="mt-4">
Diese Aktivität löschen?
</p>
<div class="grid grid-cols-2 gap-3 mt-6">
<a href="#" @click.prevent="remove" class="text-center btn btn-danger">Löschen</a>
<a href="#" @click.prevent="deleting = null" class="text-center btn btn-primary">Abbrechen</a>
</div>
</div>
</popup>
<table cellspacing="0" cellpadding="0" border="0" class="custom-table custom-table-sm table">
<thead>
<th>Name</th>
@ -13,7 +24,7 @@
<i-link :href="activity.links.edit" class="inline-flex btn btn-warning btn-sm" v-tooltip="`bearbeiten`"><svg-sprite src="pencil"></svg-sprite></i-link>
<i-link
href="#"
@click.prevent="$emit('remove')"
@click.prevent="deleting = activity"
class="inline-flex btn btn-danger btn-sm"
v-tooltip="`Entfernen`"
><svg-sprite src="trash"></svg-sprite
@ -35,8 +46,30 @@ import indexHelpers from '../../mixins/indexHelpers';
export default {
mixins: [indexHelpers],
data: function() {
return {
deleting: null,
};
},
methods: {
remove() {
var _self = this;
this.$inertia.delete(this.deleting.links.destroy, {
preserveState: true,
onSuccess(page) {
_self.inner = page.props.data;
_self.deleting = null;
}
});
}
},
components: {
popup: () => import('../../components/Popup.vue'),
},
mixins: [indexHelpers],
};
</script>

View File

@ -0,0 +1,49 @@
<?php
namespace Tests\Feature\Activity;
use App\Activity;
use App\Member\Member;
use App\Member\Membership;
use App\Subactivity;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class DestroyTest extends TestCase
{
use DatabaseTransactions;
public function testItDeletesAnActivity(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->hasAttached(Subactivity::factory())->create();
$response = $this->delete(route('activity.destroy', ['activity' => $activity]));
$response->assertRedirect('/activity');
$this->assertDatabaseCount('activities', 0);
}
public function testItCannotDeleteAnActivityThatHasMemberships(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->create();
Member::factory()->defaults()->has(Membership::factory()->for($activity))->create();
$response = $this->delete(route('activity.destroy', ['activity' => $activity]));
$response->assertSessionHasErrors(['activity' => 'Tätigkeit besitzt noch Mitglieder.']);
}
public function testItCannotDeleteActivityInNami(): void
{
$this->login()->loginNami();
$activity = Activity::factory()->inNami(66)->create();
$response = $this->delete(route('activity.destroy', ['activity' => $activity]));
$response->assertSessionHasErrors(['activity' => 'Tätigkeit ist in NaMi.']);
}
}