Skip to content

Commit

Permalink
endpoint for media patch (caption only)
Browse files Browse the repository at this point in the history
  • Loading branch information
ushahidlee committed Dec 24, 2023
1 parent 9f3e368 commit 69b364e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/Ushahidi/Modules/V5/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
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 69b364e

Please sign in to comment.