Add media ordering
This commit is contained in:
parent
f269864194
commit
c24c32afd3
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\MedialibraryHelper;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class OrderController
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
public function __invoke(Request $request, $parentModel, int $parentId, string $collectionName)
|
||||
{
|
||||
$mediaCount = collect($request->order)->map(function ($media) {
|
||||
$media = Media::findOrFail($media);
|
||||
|
||||
return $media->model_id.'_'.$media->model_type;
|
||||
})->unique()->count();
|
||||
|
||||
if (1 !== $mediaCount) {
|
||||
throw ValidationException::withMessages(['order' => 'Sortierung von verschiedenen Medien nicht möglich.']);
|
||||
}
|
||||
|
||||
$model = app('media-library-helpers')->get($parentModel);
|
||||
$model = $model::find($parentId);
|
||||
$this->authorize('updateMedia', [$model, $collectionName]);
|
||||
|
||||
Media::setNewOrder($request->order);
|
||||
|
||||
return MediaData::collection($model->getMedia($collectionName));
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@ class ServiceProvider extends BaseServiceProvider
|
|||
$router->post('mediaupload', [MediaController::class, 'store'])->name('media.store');
|
||||
$router->delete('mediaupload/{media}', [MediaController::class, 'destroy'])->name('media.destroy');
|
||||
$router->get('mediaupload/{parent_model}/{parent_id}/{collection_name}', [MediaController::class, 'index'])->name('media.index');
|
||||
$router->patch('mediaupload/{parent_model}/{parent_id}/{collection_name}', OrderController::class)->name('media.order');
|
||||
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
||||
});
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||
|
||||
test('it can reorder media', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$order = $post->getMedia('images')->pluck('id');
|
||||
$order->prepend($order->pop());
|
||||
|
||||
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('0.id', $order->get(0));
|
||||
$response->assertJsonPath('1.id', $order->get(1));
|
||||
$response->assertJsonPath('2.id', $order->get(2));
|
||||
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
|
||||
});
|
||||
|
||||
test('images should belong to same model', function () {
|
||||
$this->auth()->registerModel();
|
||||
|
||||
$post = $this->newPost();
|
||||
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
|
||||
$post = $this->newPost();
|
||||
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
$thirdMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
|
||||
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
|
||||
'order' => [$firstMedia->id, $secondMedia->id, $thirdMedia->id],
|
||||
]);
|
||||
|
||||
$response->assertJsonValidationErrors('order');
|
||||
});
|
Loading…
Reference in New Issue