Skip to content

Commit

Permalink
Edit post: Unable to specifically remove "Add caption" field for image (
Browse files Browse the repository at this point in the history
#4686)

* endpoint for media patch (caption only)

* Modified to implement CQRS

* cleaning out the lint
  • Loading branch information
ushahidlee authored and tuxpiper committed Jan 15, 2024
1 parent a5dc0e7 commit 0ff0c29
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 9 deletions.
18 changes: 11 additions & 7 deletions app/Providers/BusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private function registerCommands(): void


$commandBus->register(CreateTosCommand::class, CreateTosCommandHandler::class);

$commandBus->register(
AddTranslationCommand::class,
AddTranslationCommandHandler::class
Expand All @@ -200,7 +200,7 @@ private function registerCommands(): void
Category\Handlers\StoreCategoryCommandHandler::class
);



$commandBus->register(
Category\Commands\DeleteCategoryCommand::class,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -387,7 +391,7 @@ private function registerCommands(): void
Webhook\Commands\UpdateWebhookPostsCommand::class,
Webhook\Handlers\UpdateWebhookPostsCommandHandler::class
);


$commandBus->register(
HXL\Commands\CreateHXLMetaDataCommand::class,
Expand Down Expand Up @@ -488,7 +492,7 @@ private function registerQueries(): void
Survey\Queries\GetSurveyIdsWithPrivateLocationQuery::class,
Survey\Handlers\GetSurveyIdsWithPrivateLocationQueryHandler::class
);



$queryBus->register(
Expand Down Expand Up @@ -559,7 +563,7 @@ private function registerQueries(): void
DataProvider\Queries\FetchDataProviderByIdQuery::class,
DataProvider\Handlers\FetchDataProviderByIdQueryHandler::class
);


$queryBus->register(
Contact\Queries\FetchContactQuery::class,
Expand Down Expand Up @@ -672,7 +676,7 @@ private function registerQueries(): void
);




return $queryBus;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Ushahidi\Modules\V5\Actions\Media\Commands;

use App\Bus\Command\Command;
use Ushahidi\Modules\V5\Models\Media;
use Ushahidi\Modules\V5\Requests\MediaRequest;
use Ushahidi\Core\Entity\Media as MediaEntity;
use Illuminate\Support\Facades\Auth;
use Ushahidi\Modules\V5\Helpers\ParameterUtilities;

class UpdateMediaCaptionCommand implements Command
{

/**
* @var int
*/
private $id;

/**
* @var string
*/
private $caption;

/**
* @var MediaEntity
*/
private $media_entity;

public function __construct(
string $caption,
Media $media
) {
// $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Ushahidi\Modules\V5\Actions\Media\Handlers;

use App\Bus\Action;
use App\Bus\Command\AbstractCommandHandler;
use App\Bus\Command\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Ushahidi\Core\Exception\NotFoundException;
use Ushahidi\Modules\V5\Models\Media;
use Ushahidi\Modules\V5\Repository\Media\MediaRepository;
use Ushahidi\Modules\V5\Actions\Media\Commands\UpdateMediaCaptionCommand;
use Illuminate\Support\Facades\DB;
use Ushahidi\Modules\V5\Models\MediaLock as Lock;

class UpdateMediaCaptionCommandHandler extends AbstractCommandHandler
{
private $media_repository;

public function __construct(MediaRepository $media_repository)
{
$this->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());
}
}
20 changes: 20 additions & 0 deletions src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
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;
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;
Expand Down Expand Up @@ -97,4 +101,20 @@ public function delete(int $id)
$this->commandBus->handle(new DeleteMediaCommand($id));
return $this->deleteResponse($id);
}// end delete

/**
* Patch media item (currently only caption)
* @param int $id
* @param Request $request
* @return MediaResource|JsonResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function patch(int $id, Request $request)
{
$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
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Ushahidi/Modules/V5/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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');
}
);

Expand Down

0 comments on commit 0ff0c29

Please sign in to comment.