From b9c0abe49e50f8539baffddf7b27357a1a92b1d4 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 4 Mar 2023 12:09:25 +0100 Subject: [PATCH] Add filterable switch for activity --- app/Activity.php | 2 +- app/Activity/Actions/ActivityStoreAction.php | 1 + app/Activity/Actions/ActivityUpdateAction.php | 6 ++++-- app/Activity/Actions/CreateAction.php | 4 ++-- app/Activity/Resources/ActivityResource.php | 3 ++- resources/js/views/activity/VForm.vue | 7 +++++-- tests/Feature/Activity/CreateTest.php | 2 +- tests/Feature/Activity/EditTest.php | 4 ++-- tests/Feature/Activity/UpdateTest.php | 13 +++++++++++-- 9 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/Activity.php b/app/Activity.php index cb7ef443..2ab00d8f 100644 --- a/app/Activity.php +++ b/app/Activity.php @@ -21,6 +21,7 @@ class Activity extends Model public $casts = [ 'nami_id' => 'integer', + 'is_filterable' => 'boolean', ]; public function sluggable(): array @@ -49,5 +50,4 @@ class Activity extends Model { return $this->belongsToMany(Subactivity::class); } - } diff --git a/app/Activity/Actions/ActivityStoreAction.php b/app/Activity/Actions/ActivityStoreAction.php index 5afc40eb..22b5eb00 100644 --- a/app/Activity/Actions/ActivityStoreAction.php +++ b/app/Activity/Actions/ActivityStoreAction.php @@ -30,6 +30,7 @@ class ActivityStoreAction { return [ 'name' => 'required|max:255', + 'is_filterable' => 'present|boolean', 'subactivities' => 'present|array', ]; } diff --git a/app/Activity/Actions/ActivityUpdateAction.php b/app/Activity/Actions/ActivityUpdateAction.php index 1f45ead2..ce7f7f5f 100644 --- a/app/Activity/Actions/ActivityUpdateAction.php +++ b/app/Activity/Actions/ActivityUpdateAction.php @@ -22,7 +22,7 @@ class ActivityUpdateAction */ public function handle(Activity $activity, array $payload): void { - DB::transaction(function() use ($activity, $payload) { + DB::transaction(function () use ($activity, $payload) { $activity->update($payload); $activity->subactivities()->sync($payload['subactivities']); }); @@ -35,7 +35,8 @@ class ActivityUpdateAction { return [ 'name' => 'required|max:255', - 'subactivities' => 'present|array' + 'is_filterable' => 'present|boolean', + 'subactivities' => 'present|array', ]; } @@ -52,6 +53,7 @@ class ActivityUpdateAction /** * @todo handle this with a model event on the pivot model + * * @param Payload $payload */ private function validateNami(Activity $activity, array $payload): void diff --git a/app/Activity/Actions/CreateAction.php b/app/Activity/Actions/CreateAction.php index 547265db..ce60a787 100644 --- a/app/Activity/Actions/CreateAction.php +++ b/app/Activity/Actions/CreateAction.php @@ -2,7 +2,6 @@ namespace App\Activity\Actions; -use App\Activity; use App\Activity\Resources\ActivityResource; use Inertia\Inertia; use Inertia\Response; @@ -18,8 +17,9 @@ class CreateAction 'meta' => ActivityResource::meta(), 'data' => [ 'name' => '', + 'is_filterable' => false, 'subactivities' => [], - ] + ], ]); } } diff --git a/app/Activity/Resources/ActivityResource.php b/app/Activity/Resources/ActivityResource.php index 250ca158..3bdbc3d2 100644 --- a/app/Activity/Resources/ActivityResource.php +++ b/app/Activity/Resources/ActivityResource.php @@ -28,11 +28,12 @@ class ActivityResource extends JsonResource 'name' => $this->name, 'id' => $this->id, 'subactivities' => $this->subactivities->pluck('id')->toArray(), + 'is_filterable' => $this->is_filterable, 'links' => [ 'edit' => route('activity.edit', ['activity' => $this->getModel()]), 'update' => route('activity.update', ['activity' => $this->getModel()]), 'destroy' => route('activity.destroy', ['activity' => $this->getModel()]), - ] + ], ]; } diff --git a/resources/js/views/activity/VForm.vue b/resources/js/views/activity/VForm.vue index 1b603a45..43043d03 100644 --- a/resources/js/views/activity/VForm.vue +++ b/resources/js/views/activity/VForm.vue @@ -1,6 +1,9 @@ diff --git a/tests/Feature/Activity/CreateTest.php b/tests/Feature/Activity/CreateTest.php index 328b4775..6589959b 100644 --- a/tests/Feature/Activity/CreateTest.php +++ b/tests/Feature/Activity/CreateTest.php @@ -19,6 +19,7 @@ class CreateTest extends TestCase $this->assertInertiaHas([ 'name' => '', + 'is_filterable' => false, 'subactivities' => [], ], $response, 'data'); $this->assertInertiaHas([ @@ -26,5 +27,4 @@ class CreateTest extends TestCase 'name' => 'Pupu', ], $response, 'meta.subactivities.0'); } - } diff --git a/tests/Feature/Activity/EditTest.php b/tests/Feature/Activity/EditTest.php index bae0342c..abcfbfb3 100644 --- a/tests/Feature/Activity/EditTest.php +++ b/tests/Feature/Activity/EditTest.php @@ -16,10 +16,11 @@ class EditTest extends TestCase $this->login()->loginNami()->withoutExceptionHandling(); $activity = Activity::factory()->name('Asas')->hasAttached(Subactivity::factory()->name('Pupu'))->create(); - $response = $this->get(route('activity.edit', ['activity' => $activity])); + $response = $this->get(route('activity.edit', ['activity' => $activity])); $this->assertInertiaHas([ 'name' => 'Asas', + 'is_filterable' => false, 'subactivities' => [$activity->subactivities->first()->id], ], $response, 'data'); $this->assertInertiaHas([ @@ -27,5 +28,4 @@ class EditTest extends TestCase 'name' => 'Pupu', ], $response, 'meta.subactivities.0'); } - } diff --git a/tests/Feature/Activity/UpdateTest.php b/tests/Feature/Activity/UpdateTest.php index 68934b18..75eba9be 100644 --- a/tests/Feature/Activity/UpdateTest.php +++ b/tests/Feature/Activity/UpdateTest.php @@ -18,6 +18,7 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'Lorem', + 'is_filterable' => false, 'subactivities' => [], ]); @@ -32,6 +33,7 @@ class UpdateTest extends TestCase $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'abc', + 'is_filterable' => false, 'subactivities' => [$subactivity->id], ]); @@ -45,6 +47,7 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'abc', + 'is_filterable' => false, 'subactivities' => [], ]); @@ -59,6 +62,7 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'abc', + 'is_filterable' => false, 'subactivities' => [$subactivity->id], ]); @@ -73,6 +77,7 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'abc', + 'is_filterable' => false, 'subactivities' => [$otherSubactivity->id], ]); @@ -86,6 +91,7 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => '', + 'is_filterable' => true, ]); $response->assertSessionHasErrors(['name' => 'Name ist erforderlich.']); @@ -99,11 +105,12 @@ class UpdateTest extends TestCase $response = $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'Lorem', + 'is_filterable' => true, 'subactivities' => [], ]); $response->assertRedirect('/activity'); - $this->assertDatabaseHas('activities', ['name' => 'Lorem']); + $this->assertDatabaseHas('activities', ['name' => 'Lorem', 'is_filterable' => true]); } public function testItSetsSubactivities(): void @@ -114,6 +121,7 @@ class UpdateTest extends TestCase $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'Lorem', + 'is_filterable' => false, 'subactivities' => [$subactivity->id], ]); @@ -128,6 +136,7 @@ class UpdateTest extends TestCase $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'Lorem', 'nami_id' => 66, + 'is_filterable' => false, 'subactivities' => [], ]); @@ -143,10 +152,10 @@ class UpdateTest extends TestCase $this->patch(route('activity.update', ['activity' => $activity]), [ 'name' => 'Lorem', + 'is_filterable' => false, 'subactivities' => [], ]); $this->assertDatabaseEmpty('activity_subactivity'); } - }