From 4ede6bd41ef25d77f483fa784076ae90edb684e8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 11 Apr 2021 13:52:37 +1000 Subject: [PATCH] Add subscription delete routes --- .../Controllers/SubscriptionController.php | 71 +++++++++++++++++++ routes/api.php | 2 + 2 files changed, 73 insertions(+) diff --git a/app/Http/Controllers/SubscriptionController.php b/app/Http/Controllers/SubscriptionController.php index 1b4a6154cbde..a3a82c09fdc9 100644 --- a/app/Http/Controllers/SubscriptionController.php +++ b/app/Http/Controllers/SubscriptionController.php @@ -24,9 +24,12 @@ use App\Models\Subscription; use App\Repositories\SubscriptionRepository; use App\Transformers\SubscriptionTransformer; use App\Utils\Ninja; +use App\Utils\Traits\MakesHash; class SubscriptionController extends BaseController { + use MakesHash; + protected $entity_type = Subscription::class; protected $entity_transformer = SubscriptionTransformer::class; @@ -407,4 +410,72 @@ class SubscriptionController extends BaseController return $this->itemResponse($subscription->fresh()); } + + /** + * Perform bulk actions on the list view. + * + * @return Response + * + * + * @OA\Post( + * path="/api/v1/subscriptions/bulk", + * operationId="bulkSubscriptions", + * tags={"subscriptions"}, + * summary="Performs bulk actions on an array of subscriptions", + * description="", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter(ref="#/components/parameters/index"), + * @OA\RequestBody( + * description="User credentials", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * type="array", + * @OA\Items( + * type="integer", + * description="Array of hashed IDs to be bulk 'actioned", + * example="[0,1,2,3]", + * ), + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="The Subscription response", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function bulk() + { + $action = request()->input('action'); + + $ids = request()->input('ids'); + $subscriptions = Subscription::withTrashed()->find($this->transformKeys($ids)); + + $subscriptions->each(function ($subscription, $key) use ($action) { + if (auth()->user()->can('edit', $subscription)) { + $this->subscription_repo->{$action}($subscription); + } + }); + + return $this->listResponse(Subscription::withTrashed()->whereIn('id', $this->transformKeys($ids))); + } + } diff --git a/routes/api.php b/routes/api.php index 99ffe08a17a8..a527ad0a6ae4 100644 --- a/routes/api.php +++ b/routes/api.php @@ -177,6 +177,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a // Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe'); Route::resource('subscriptions', 'SubscriptionController'); + Route::post('subscriptions/bulk', 'SubscriptionController@bulk')->name('subscriptions.bulk'); + Route::resource('cliente_subscriptions', 'ClientSubscriptionController'); });