From 69b364e67d0af83693d86f7397251cddb67abfae Mon Sep 17 00:00:00 2001 From: ushahidlee Date: Sun, 24 Dec 2023 13:03:38 +0100 Subject: [PATCH 1/3] endpoint for media patch (caption only) --- .../V5/Http/Controllers/MediaController.php | 39 +++++++++++++++++++ src/Ushahidi/Modules/V5/routes/api.php | 3 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php index adfc7e6c90..ee5e7912ad 100644 --- a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php +++ b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php @@ -3,6 +3,9 @@ namespace Ushahidi\Modules\V5\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Http\JsonResponse; + use Ushahidi\Modules\V5\Actions\Media\Queries\FetchMediaByIdQuery; use Ushahidi\Modules\V5\Http\Resources\Media\MediaResource; use Ushahidi\Modules\V5\Actions\Media\Commands\CreateMediaCommand; @@ -97,4 +100,40 @@ public function delete(int $id) $this->commandBus->handle(new DeleteMediaCommand($id)); return $this->deleteResponse($id); }// end delete + + /** + * Patch media item + * @param int $id + * @param Request $request + * @return MediaResource|JsonResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function patch(int $id, Request $request) + { + $media = Media::find($id); + $caption = $this->getField('caption', $request->input('caption')); + if (!$media) { + return self::make404(); + } + if (!$caption) { + return self::make422("Caption required for media patch call."); + } + + DB::beginTransaction(); + try { + $media->setAttribute('caption', $caption); + $this->authorize('update', $media); + + if ($media->save()) { + DB::commit(); + return new MediaResource($media); + } else { + DB::rollback(); + return self::make422($media->errors); + } + } catch (\Exception $e) { + DB::rollback(); + return self::make500($e->getMessage()); + } + } // end patch } //end class diff --git a/src/Ushahidi/Modules/V5/routes/api.php b/src/Ushahidi/Modules/V5/routes/api.php index 6956744d23..71d5689c70 100644 --- a/src/Ushahidi/Modules/V5/routes/api.php +++ b/src/Ushahidi/Modules/V5/routes/api.php @@ -214,7 +214,7 @@ function () use ($router) { $router->post('/', 'UserController@store'); $router->put('/{id}', 'UserController@update'); $router->delete('/{id}', 'UserController@delete'); - + $router->group( [ 'prefix' => '{user_id}/settings', @@ -489,6 +489,7 @@ function () use ($router) { function () use ($router) { $router->put('/{id}', 'MediaController@update'); $router->delete('/{id}', 'MediaController@delete'); + $router->patch('/{id}', 'MediaController@patch'); } ); From ec384f5784bc88a901028eebd38365e2088a249e Mon Sep 17 00:00:00 2001 From: ushahidlee Date: Wed, 10 Jan 2024 08:16:22 +0100 Subject: [PATCH 2/3] Modified to implement CQRS --- app/Providers/BusServiceProvider.php | 18 +++--- .../Commands/UpdateMediaCaptionCommand.php | 62 +++++++++++++++++++ .../UpdateMediaCaptionCommandHandler.php | 41 ++++++++++++ .../V5/Http/Controllers/MediaController.php | 34 +++------- .../V5/Repository/Media/MediaRepository.php | 2 +- 5 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 src/Ushahidi/Modules/V5/Actions/Media/Commands/UpdateMediaCaptionCommand.php create mode 100644 src/Ushahidi/Modules/V5/Actions/Media/Handlers/UpdateMediaCaptionCommandHandler.php diff --git a/app/Providers/BusServiceProvider.php b/app/Providers/BusServiceProvider.php index acfe0f0226..3caca59952 100644 --- a/app/Providers/BusServiceProvider.php +++ b/app/Providers/BusServiceProvider.php @@ -189,7 +189,7 @@ private function registerCommands(): void $commandBus->register(CreateTosCommand::class, CreateTosCommandHandler::class); - + $commandBus->register( AddTranslationCommand::class, AddTranslationCommandHandler::class @@ -200,7 +200,7 @@ private function registerCommands(): void Category\Handlers\StoreCategoryCommandHandler::class ); - + $commandBus->register( Category\Commands\DeleteCategoryCommand::class, @@ -352,6 +352,10 @@ private function registerCommands(): void Media\Commands\UpdateMediaCommand::class, Media\Handlers\UpdateMediaCommandHandler::class ); + $commandBus->register( + Media\Commands\UpdateMediaCaptionCommand::class, + Media\Handlers\UpdateMediaCaptionCommandHandler::class + ); $commandBus->register( Media\Commands\DeleteMediaCommand::class, Media\Handlers\DeleteMediaCommandHandler::class @@ -370,7 +374,7 @@ private function registerCommands(): void Apikey\Commands\DeleteApikeyCommand::class, Apikey\Handlers\DeleteApikeyCommandHandler::class ); - + $commandBus->register( Webhook\Commands\CreateWebhookCommand::class, Webhook\Handlers\CreateWebhookCommandHandler::class @@ -387,7 +391,7 @@ private function registerCommands(): void Webhook\Commands\UpdateWebhookPostsCommand::class, Webhook\Handlers\UpdateWebhookPostsCommandHandler::class ); - + $commandBus->register( HXL\Commands\CreateHXLMetaDataCommand::class, @@ -488,7 +492,7 @@ private function registerQueries(): void Survey\Queries\GetSurveyIdsWithPrivateLocationQuery::class, Survey\Handlers\GetSurveyIdsWithPrivateLocationQueryHandler::class ); - + $queryBus->register( @@ -559,7 +563,7 @@ private function registerQueries(): void DataProvider\Queries\FetchDataProviderByIdQuery::class, DataProvider\Handlers\FetchDataProviderByIdQueryHandler::class ); - + $queryBus->register( Contact\Queries\FetchContactQuery::class, @@ -672,7 +676,7 @@ private function registerQueries(): void ); - + return $queryBus; }); diff --git a/src/Ushahidi/Modules/V5/Actions/Media/Commands/UpdateMediaCaptionCommand.php b/src/Ushahidi/Modules/V5/Actions/Media/Commands/UpdateMediaCaptionCommand.php new file mode 100644 index 0000000000..74b2ab1723 --- /dev/null +++ b/src/Ushahidi/Modules/V5/Actions/Media/Commands/UpdateMediaCaptionCommand.php @@ -0,0 +1,62 @@ +id = $id; + $input['id'] = $media->id; + $input['user_id'] = $media->user_id; + $input['caption'] = $caption; + $input['mime'] = $media->mime; + $input['o_filename'] = $media->o_filename; + $input['o_size'] = $media->o_size; + $input['o_width'] = $media->o_width; + $input['o_height'] = $media->o_height; + $input['created'] = strtotime($media->created); + $input['updated'] = time(); + + $this->media_entity = new MediaEntity($input); + } + + // public function getId(): int + // { + // return $this->id; + // } + public function getCaption(): string + { + return $this->caption; + } + + public function getMediaEntity(): MediaEntity + { + return $this->media_entity; + } +} diff --git a/src/Ushahidi/Modules/V5/Actions/Media/Handlers/UpdateMediaCaptionCommandHandler.php b/src/Ushahidi/Modules/V5/Actions/Media/Handlers/UpdateMediaCaptionCommandHandler.php new file mode 100644 index 0000000000..05faf5f9fb --- /dev/null +++ b/src/Ushahidi/Modules/V5/Actions/Media/Handlers/UpdateMediaCaptionCommandHandler.php @@ -0,0 +1,41 @@ +media_repository = $media_repository; + } + + protected function isSupported(Command $command): void + { + if (!$command instanceof UpdateMediaCaptionCommand) { + throw new \Exception('Provided $command is not instance of UpdateMediaCommand'); + } + } + + public function __invoke(Action $action) + { + /** + * @var UpdateMediaCaptionCommand $action + */ + $this->isSupported($action); + + return $this->media_repository->update($action->getMediaEntity()->getId(), $action->getMediaEntity()); + } +} diff --git a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php index ee5e7912ad..bdc153bda6 100644 --- a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php +++ b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php @@ -10,6 +10,7 @@ use Ushahidi\Modules\V5\Http\Resources\Media\MediaResource; use Ushahidi\Modules\V5\Actions\Media\Commands\CreateMediaCommand; use Ushahidi\Modules\V5\Actions\Media\Commands\UpdateMediaCommand; +use Ushahidi\Modules\V5\Actions\Media\Commands\UpdateMediaCaptionCommand; use Ushahidi\Modules\V5\Actions\Media\Commands\DeleteMediaCommand; use Ushahidi\Modules\V5\Requests\MediaRequest; use Ushahidi\Modules\V5\Models\Media; @@ -102,7 +103,7 @@ public function delete(int $id) }// end delete /** - * Patch media item + * Patch media item (currently only caption) * @param int $id * @param Request $request * @return MediaResource|JsonResponse @@ -110,30 +111,11 @@ public function delete(int $id) */ public function patch(int $id, Request $request) { - $media = Media::find($id); - $caption = $this->getField('caption', $request->input('caption')); - if (!$media) { - return self::make404(); - } - if (!$caption) { - return self::make422("Caption required for media patch call."); - } - - DB::beginTransaction(); - try { - $media->setAttribute('caption', $caption); - $this->authorize('update', $media); - - if ($media->save()) { - DB::commit(); - return new MediaResource($media); - } else { - DB::rollback(); - return self::make422($media->errors); - } - } catch (\Exception $e) { - DB::rollback(); - return self::make500($e->getMessage()); - } + $caption = $request->input('caption'); + $media = $this->queryBus->handle(new FetchMediaByIdQuery($id)); + $this->authorize('update', $media); + $command = new UpdateMediaCaptionCommand($caption, $media); + return $this->commandBus->handle($command); } // end patch + } //end class diff --git a/src/Ushahidi/Modules/V5/Repository/Media/MediaRepository.php b/src/Ushahidi/Modules/V5/Repository/Media/MediaRepository.php index d3fa7b8ea2..3e37658c00 100644 --- a/src/Ushahidi/Modules/V5/Repository/Media/MediaRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Media/MediaRepository.php @@ -48,7 +48,7 @@ public function create(MediaEntity $entity): int; */ public function update(int $id, MediaEntity $entity): void; - /** + /** * This method will delete the Media * @param int $id */ From 767628e8a59d11c085202804209dc61503be4d83 Mon Sep 17 00:00:00 2001 From: ushahidlee Date: Wed, 10 Jan 2024 16:16:28 +0100 Subject: [PATCH 3/3] cleaning out the lint --- src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php index bdc153bda6..4e26ef0cb1 100644 --- a/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php +++ b/src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php @@ -117,5 +117,4 @@ public function patch(int $id, Request $request) $command = new UpdateMediaCaptionCommand($caption, $media); return $this->commandBus->handle($command); } // end patch - } //end class