Skip to content

Commit

Permalink
Merge pull request #34 from RonasIT/33_return-preview-key-in-response
Browse files Browse the repository at this point in the history
feat: add preview to response
  • Loading branch information
DenTray authored Dec 11, 2024
2 parents b73f5d7 + 26c2c1f commit 743b643
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Http/Resources/MediaResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function toArray($request): array
'name' => $this->resource->name,
'is_public' => $this->resource->is_public,
'meta' => $this->resource->meta,
'preview' => MediaResource::make($this->whenLoaded('preview')),
];
}
}
5 changes: 5 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function owner(): BelongsTo
return $this->belongsTo(config('media.classes.user_model'));
}

public function preview(): BelongsTo
{
return $this->belongsTo(self::class, 'preview_id');
}

protected static function newFactory(): MediaFactory
{
return MediaFactory::new();
Expand Down
12 changes: 7 additions & 5 deletions src/Services/MediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace RonasIT\Media\Services;

use Illuminate\Support\Facades\File as FileFacade;
use League\Flysystem\Local\LocalFilesystemAdapter;
use RonasIT\Media\Repositories\MediaRepository;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -15,8 +14,6 @@
use Spatie\Image\Image;
use Spatie\MediaLibrary\InteractsWithMedia;

use function PHPUnit\Framework\directoryExists;

/**
* @property MediaRepository $repository
* @mixin MediaRepository
Expand Down Expand Up @@ -48,12 +45,17 @@ public function search(array $filters): LengthAwarePaginator
public function create($content, string $fileName, array $data = []): Model
{
$fileName = $this->saveFile($fileName, $content);

$preview = $this->createPreview($fileName);

$data['name'] = $fileName;
$data['link'] = Storage::url($data['name']);
$data['owner_id'] = Auth::id();
$data['preview_id'] = $this->createPreview($data['name'])->id;
$data['preview_id'] = $preview->id;

return $this->repository->create($data);
$media = $this->repository->create($data);

return $media->setRelation('preview', $preview);
}

public function bulkCreate(array $data): array
Expand Down
4 changes: 4 additions & 0 deletions tests/MediaStaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function testCreate(): void
$response->assertCreated();

self::$mediaTestState->assertChangesEqualsFixture('create_changes.json');

$this->assertEqualsFixture('create_response.json', $response->json());
}

public function testCreateWasCreateDisabled(): void
Expand Down Expand Up @@ -185,6 +187,8 @@ public function testBulkCreate(): void
$response->assertOk();

self::$mediaTestState->assertChangesEqualsFixture('bulk_create_changes.json');

$this->assertEqualsFixture('bulk_create_response.json', $response->json());
}

public function testDeleteWasDeleteDisabled(): void
Expand Down
4 changes: 4 additions & 0 deletions tests/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function testCreate(): void
$response->assertCreated();

self::$mediaTestState->assertChangesEqualsFixture('create_changes.json');

$this->assertEqualsFixture('create_response.json', $response->json());
}

public function testCreatePublic(): void
Expand Down Expand Up @@ -100,6 +102,8 @@ public function testBulkCreate(): void
$response->assertOk();

self::$mediaTestState->assertChangesEqualsFixture('bulk_create_changes.json');

$this->assertEqualsFixture('bulk_create_response.json', $response->json());
}

public function testDelete(): void
Expand Down
36 changes: 36 additions & 0 deletions tests/fixtures/MediaStaticTest/bulk_create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"data": [
{
"id": 12,
"link": "\/storage\/file1.png",
"name": "file1.png",
"is_public": false,
"meta": [
"test1"
],
"preview": {
"id": 11,
"link": "\/storage\/preview_file1.png",
"name": "preview_file1.png",
"is_public": false,
"meta": []
}
},
{
"id": 14,
"link": "\/storage\/file2.png",
"name": "file2.png",
"is_public": false,
"meta": [
"test2"
],
"preview": {
"id": 13,
"link": "\/storage\/preview_file2.png",
"name": "preview_file2.png",
"is_public": false,
"meta": []
}
}
]
}
14 changes: 14 additions & 0 deletions tests/fixtures/MediaStaticTest/create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 12,
"link": "\/storage\/file.png",
"name": "file.png",
"is_public": false,
"meta": [],
"preview": {
"id": 11,
"link": "\/storage\/preview_file.png",
"name": "preview_file.png",
"is_public": false,
"meta": []
}
}
36 changes: 36 additions & 0 deletions tests/fixtures/MediaTest/bulk_create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"data": [
{
"id": 12,
"link": "\/storage\/file1.png",
"name": "file1.png",
"is_public": false,
"meta": [
"test1"
],
"preview": {
"id": 11,
"link": "\/storage\/preview_file1.png",
"name": "preview_file1.png",
"is_public": false,
"meta": []
}
},
{
"id": 14,
"link": "\/storage\/file2.png",
"name": "file2.png",
"is_public": false,
"meta": [
"test2"
],
"preview": {
"id": 13,
"link": "\/storage\/preview_file2.png",
"name": "preview_file2.png",
"is_public": false,
"meta": []
}
}
]
}
14 changes: 14 additions & 0 deletions tests/fixtures/MediaTest/create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 12,
"link": "\/storage\/file.png",
"name": "file.png",
"is_public": false,
"meta": [],
"preview": {
"id": 11,
"link": "\/storage\/preview_file.png",
"name": "preview_file.png",
"is_public": false,
"meta": []
}
}

0 comments on commit 743b643

Please sign in to comment.