From 9598a44e138b934d651ed529f00c5a624c48fc35 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 7 Mar 2023 01:35:39 +0100 Subject: [PATCH] Add: Edit subactivity --- app/Activity/Actions/ActivityStoreAction.php | 1 + app/Activity/Actions/ActivityUpdateAction.php | 1 + app/Activity/Api/SubactivityShowAction.php | 26 +++++++++++ app/Activity/Api/SubactivityStoreAction.php | 1 + app/Activity/Api/SubactivityUpdateAction.php | 1 + app/Activity/Resources/ActivityResource.php | 3 +- app/Lib/HasMeta.php | 6 ++- app/Resources/SubactivityResource.php | 44 +++++++++++++++++++ .../js/views/activity/SubactivityForm.vue | 20 +++++++-- resources/js/views/activity/VForm.vue | 40 ++++++++++------- routes/web.php | 2 + tests/Feature/Activity/EditTest.php | 9 ++++ .../Feature/Activity/SubactivityShowTest.php | 29 ++++++++++++ 13 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 app/Activity/Api/SubactivityShowAction.php create mode 100644 app/Resources/SubactivityResource.php create mode 100644 tests/Feature/Activity/SubactivityShowTest.php diff --git a/app/Activity/Actions/ActivityStoreAction.php b/app/Activity/Actions/ActivityStoreAction.php index 22b5eb00..abdb0ba9 100644 --- a/app/Activity/Actions/ActivityStoreAction.php +++ b/app/Activity/Actions/ActivityStoreAction.php @@ -32,6 +32,7 @@ class ActivityStoreAction 'name' => 'required|max:255', 'is_filterable' => 'present|boolean', 'subactivities' => 'present|array', + 'subactivities.*' => 'integer', ]; } diff --git a/app/Activity/Actions/ActivityUpdateAction.php b/app/Activity/Actions/ActivityUpdateAction.php index 41b18c89..7facad24 100644 --- a/app/Activity/Actions/ActivityUpdateAction.php +++ b/app/Activity/Actions/ActivityUpdateAction.php @@ -38,6 +38,7 @@ class ActivityUpdateAction 'name' => 'required|max:255', 'is_filterable' => 'present|boolean', 'subactivities' => 'present|array', + 'subactivities.*' => 'integer', ]; } diff --git a/app/Activity/Api/SubactivityShowAction.php b/app/Activity/Api/SubactivityShowAction.php new file mode 100644 index 00000000..175f8116 --- /dev/null +++ b/app/Activity/Api/SubactivityShowAction.php @@ -0,0 +1,26 @@ +json([ + 'data' => new SubactivityResource($subactivity), + 'meta' => SubactivityResource::meta(), + ]); + } +} diff --git a/app/Activity/Api/SubactivityStoreAction.php b/app/Activity/Api/SubactivityStoreAction.php index 5a275931..4d74274c 100644 --- a/app/Activity/Api/SubactivityStoreAction.php +++ b/app/Activity/Api/SubactivityStoreAction.php @@ -34,6 +34,7 @@ class SubactivityStoreAction return [ 'name' => 'required|unique:subactivities,name', 'activities' => 'present|array|min:1', + 'activities.*' => 'integer', 'is_filterable' => 'present|boolean', ]; } diff --git a/app/Activity/Api/SubactivityUpdateAction.php b/app/Activity/Api/SubactivityUpdateAction.php index 33861a45..15e63030 100644 --- a/app/Activity/Api/SubactivityUpdateAction.php +++ b/app/Activity/Api/SubactivityUpdateAction.php @@ -41,6 +41,7 @@ class SubactivityUpdateAction return [ 'name' => ['required', 'string', 'max:255', Rule::unique('subactivities', 'name')->ignore(request()->route('subactivity')->id)], 'activities' => ['present', 'array', 'min:1'], + 'activities.*' => 'integer', 'is_filterable' => 'present|boolean', ]; } diff --git a/app/Activity/Resources/ActivityResource.php b/app/Activity/Resources/ActivityResource.php index 123a5f8c..341b1612 100644 --- a/app/Activity/Resources/ActivityResource.php +++ b/app/Activity/Resources/ActivityResource.php @@ -5,6 +5,7 @@ namespace App\Activity\Resources; use App\Activity; use App\Http\Views\ActivityFilterScope; use App\Lib\HasMeta; +use App\Resources\SubactivityResource; use App\Subactivity; use Illuminate\Http\Resources\Json\JsonResource; @@ -49,7 +50,7 @@ class ActivityResource extends JsonResource public static function meta(): array { return [ - 'subactivities' => Subactivity::select('name', 'id', 'is_filterable')->get(), + 'subactivities' => SubactivityResource::collectionWithoutMeta(Subactivity::get()), 'filter' => ActivityFilterScope::fromRequest(request()->input('filter')), ]; } diff --git a/app/Lib/HasMeta.php b/app/Lib/HasMeta.php index 871d2610..854e8899 100644 --- a/app/Lib/HasMeta.php +++ b/app/Lib/HasMeta.php @@ -25,9 +25,13 @@ trait HasMeta ]); } + public static function collectionWithoutMeta($resource) + { + return parent::collection($resource); + } + public static function meta(): array { return []; } } - diff --git a/app/Resources/SubactivityResource.php b/app/Resources/SubactivityResource.php new file mode 100644 index 00000000..aa6100eb --- /dev/null +++ b/app/Resources/SubactivityResource.php @@ -0,0 +1,44 @@ + $this->id, + 'name' => $this->name, + 'is_filterable' => $this->is_filterable, + 'activities' => $this->activities->pluck('id')->toArray(), + 'links' => [ + 'show' => route('api.subactivity.show', ['subactivity' => $this->getModel()]), + 'update' => route('api.subactivity.update', ['subactivity' => $this->getModel()]), + ], + ]; + } + + public static function meta(): array + { + return [ + 'activities' => ActivityResource::collectionWithoutMeta(Activity::get()), + ]; + } +} diff --git a/resources/js/views/activity/SubactivityForm.vue b/resources/js/views/activity/SubactivityForm.vue index c002dae6..b432e235 100644 --- a/resources/js/views/activity/SubactivityForm.vue +++ b/resources/js/views/activity/SubactivityForm.vue @@ -1,6 +1,6 @@