From 424ca30932610dcef496640e4097644ed404f13b Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 13 Nov 2024 13:50:41 +0100 Subject: [PATCH] Fix response codes --- src/DeferredMediaData.php | 2 +- src/MediaController.php | 8 +++++--- src/OrderController.php | 2 +- tests/Feature/IndexTest.php | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/DeferredMediaData.php b/src/DeferredMediaData.php index 4262384..108ea67 100644 --- a/src/DeferredMediaData.php +++ b/src/DeferredMediaData.php @@ -29,7 +29,7 @@ class DeferredMediaData extends Data ]); } - public static function collectionFromPaths(Collection $paths, MediaCollection $collection): DataCollection + public static function collectionFromPaths(Collection $paths, MediaCollection $collection): Collection { return static::collect($paths->map(fn ($path) => static::fromPath($path, $collection))); } diff --git a/src/MediaController.php b/src/MediaController.php index f544fd9..6877576 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -5,6 +5,8 @@ namespace Zoomyboy\MedialibraryHelper; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Http\Response; +use Illuminate\Support\Collection; use Spatie\Image\Image; use Spatie\LaravelData\DataCollection; use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data; @@ -98,7 +100,7 @@ class MediaController }); $this->authorize('storeMedia', [$modelName, null, $collection->name]); - return $isSingle ? DeferredMediaData::fromPath($tempPaths->first(), $collection) : DeferredMediaData::collect($tempPaths, $collection); + return $isSingle ? DeferredMediaData::fromPath($tempPaths->first(), $collection) : response(DeferredMediaData::collectionFromPaths($tempPaths, $collection), 201); } public function update(Request $request, Media $media): MediaData @@ -116,7 +118,7 @@ class MediaController return MediaData::from($media); } - public function index(Request $request, $parentModel, int $parentId, string $collectionName): MediaData|DataCollection + public function index(Request $request, $parentModel, int $parentId, string $collectionName): MediaData|JsonResponse { $model = app('media-library-helpers')->get($parentModel); $model = $model::find($parentId); @@ -132,7 +134,7 @@ class MediaController return $isSingle ? MediaData::from($model->getFirstMedia($collectionName)) - : MediaData::collect($model->getMedia($collectionName)); + : response()->json(MediaData::collect($model->getMedia($collectionName))->toArray()); } public function destroy(Media $media, Request $request): JsonResponse diff --git a/src/OrderController.php b/src/OrderController.php index f526ab3..e3a70a0 100644 --- a/src/OrderController.php +++ b/src/OrderController.php @@ -31,6 +31,6 @@ class OrderController $model->getMediaCollection($collectionName)->runCallback('after', $model->fresh()); - return MediaData::collect($model->getMedia($collectionName)); + return MediaData::collect($model->getMedia($collectionName))->toArray(); } } diff --git a/tests/Feature/IndexTest.php b/tests/Feature/IndexTest.php index 90e0888..d698b56 100644 --- a/tests/Feature/IndexTest.php +++ b/tests/Feature/IndexTest.php @@ -7,6 +7,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media; test('it gets all medias', function () { $this->auth()->registerModel(); + $this->withoutExceptionHandling(); $post = $this->newPost(); $firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images'); $secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');