From b303373b73745b7d6d82dffff48a8c3674f9a550 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 25 Feb 2023 18:00:23 +0100 Subject: [PATCH] add activity store --- app/Activity/Actions/ActivityStoreAction.php | 2 +- app/Activity/Actions/CreateAction.php | 25 ++++++++++++++++ app/Activity/Actions/IndexAction.php | 3 ++ resources/js/views/activity/VForm.vue | 5 +++- routes/web.php | 2 ++ tests/Feature/Activity/CreateTest.php | 30 ++++++++++++++++++++ tests/Feature/Activity/StoreTest.php | 2 +- 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/Activity/Actions/CreateAction.php create mode 100644 tests/Feature/Activity/CreateTest.php diff --git a/app/Activity/Actions/ActivityStoreAction.php b/app/Activity/Actions/ActivityStoreAction.php index e86c206b..5afc40eb 100644 --- a/app/Activity/Actions/ActivityStoreAction.php +++ b/app/Activity/Actions/ActivityStoreAction.php @@ -38,6 +38,6 @@ class ActivityStoreAction { $this->handle($request->validated()); - return redirect()->back(); + return redirect()->route('activity.index'); } } diff --git a/app/Activity/Actions/CreateAction.php b/app/Activity/Actions/CreateAction.php new file mode 100644 index 00000000..547265db --- /dev/null +++ b/app/Activity/Actions/CreateAction.php @@ -0,0 +1,25 @@ + ActivityResource::meta(), + 'data' => [ + 'name' => '', + 'subactivities' => [], + ] + ]); + } +} diff --git a/app/Activity/Actions/IndexAction.php b/app/Activity/Actions/IndexAction.php index 5e5b63c6..752dbe16 100644 --- a/app/Activity/Actions/IndexAction.php +++ b/app/Activity/Actions/IndexAction.php @@ -28,6 +28,9 @@ class IndexAction return Inertia::render('activity/VIndex', [ 'data' => $this->handle($filter), + 'toolbar' => [ + ['href' => route('activity.create'), 'label' => 'Tätigkeit erstellen', 'color' => 'primary', 'icon' => 'plus'], + ] ]); } } diff --git a/resources/js/views/activity/VForm.vue b/resources/js/views/activity/VForm.vue index a5a08f27..2d19bc0c 100644 --- a/resources/js/views/activity/VForm.vue +++ b/resources/js/views/activity/VForm.vue @@ -14,6 +14,7 @@ export default { data: function () { return { inner: {...this.data}, + mode: this.data.name === '' ? 'create' : 'edit', }; }, @@ -28,7 +29,9 @@ export default { methods: { submit() { - this.$inertia.patch(`/activity/${this.inner.id}`, this.inner); + this.mode === 'create' + ? this.$inertia.post('/activity', this.inner) + : this.$inertia.patch(`/activity/${this.inner.id}`, this.inner); }, } diff --git a/routes/web.php b/routes/web.php index 15ca3068..afc2c391 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Activity\Actions\ActivityStoreAction; use App\Activity\Actions\ActivityUpdateAction; use App\Activity\Actions\IndexAction as ActivityIndexAction; +use App\Activity\Actions\CreateAction as ActivityCreateAction; use App\Activity\Actions\EditAction as ActivityEditAction; use App\Activity\Actions\DestroyAction as ActivityDestroyAction; use App\Contribution\Actions\FormAction as ContributionFormAction; @@ -53,6 +54,7 @@ Route::group(['middleware' => 'auth:web'], function (): void { Route::get('/contribution/generate', [ContributionController::class, 'generate'])->name('contribution.generate'); Route::get('/activity', ActivityIndexAction::class)->name('activity.index'); Route::get('/activity/{activity}/edit', ActivityEditAction::class)->name('activity.edit'); + Route::get('/activity/create', ActivityCreateAction::class)->name('activity.create'); Route::post('/activity', ActivityStoreAction::class)->name('activity.store'); Route::patch('/activity/{activity}', ActivityUpdateAction::class)->name('activity.update'); Route::delete('/activity/{activity}', ActivityDestroyAction::class)->name('activity.destroy'); diff --git a/tests/Feature/Activity/CreateTest.php b/tests/Feature/Activity/CreateTest.php new file mode 100644 index 00000000..328b4775 --- /dev/null +++ b/tests/Feature/Activity/CreateTest.php @@ -0,0 +1,30 @@ +login()->loginNami()->withoutExceptionHandling(); + Subactivity::factory()->name('Pupu')->create(); + + $response = $this->get(route('activity.create')); + + $this->assertInertiaHas([ + 'name' => '', + 'subactivities' => [], + ], $response, 'data'); + $this->assertInertiaHas([ + 'id' => Subactivity::first()->id, + 'name' => 'Pupu', + ], $response, 'meta.subactivities.0'); + } + +} diff --git a/tests/Feature/Activity/StoreTest.php b/tests/Feature/Activity/StoreTest.php index 99307c74..105a1147 100644 --- a/tests/Feature/Activity/StoreTest.php +++ b/tests/Feature/Activity/StoreTest.php @@ -15,7 +15,7 @@ class StoreTest extends TestCase { $this->login()->loginNami()->withoutExceptionHandling(); - $response = $this->from('/activity')->post(route('activity.store'), [ + $response = $this->post(route('activity.store'), [ 'name' => 'Lorem', 'subactivities' => [], ]);