Skip to content

Commit

Permalink
chore: add upload controller test (json add), consolidate most of 'pu…
Browse files Browse the repository at this point in the history
…blic identifier' logic
  • Loading branch information
mdshack committed Jan 16, 2024
1 parent 169047e commit 0497567
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 2 additions & 3 deletions app/Http/Controllers/ShotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ public function index(Request $request)

public function show(Request $request, string $id)
{
$shot = (config('features.uuid_routes')
? Shot::whereUuid($id)
: Shot::whereId($id))->firstOrFail();
$shot = Shot::wherePublicIdentifier($id)->firstOrFail();

if ($shot->parent_shot_id) {
$parentShotId = config('features.uuid_routes')
// TODO: Don't load parent (perhaps migrate parent_shot_id to be either uuid or id pending settings)
? $shot->parentShot->uuid
: $shot->parent_shot_id;

Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public function __invoke(UploadRequest $request)
}
}

$id = config('features.uuid_routes')
? $parentShot?->uuid
: $parentShot?->getKey();
$id = $parentShot->publicIdentifier;

if ($request->expectsJson()) {
return response()->json([
Expand Down
23 changes: 20 additions & 3 deletions app/Models/Shot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -43,14 +44,30 @@ protected function links(): Attribute
{
return Attribute::make(
get: fn ($_, array $attributes) => [
'url' => config('features.uuid_routes')
? route('shots.show', $attributes['uuid'])
: route('shots.show', $attributes['id']),
'url' => route('shots.show', $this->publicIdentifier),
'asset_url' => asset($attributes['path']),
],
);
}

protected function publicIdentifier(): Attribute
{
return Attribute::make(
get: fn ($_, array $attributes) => config('features.uuid_routes')
? $attributes['uuid']
: $attributes['id'],
);
}

public function scopeWherePublicIdentifier(Builder $query, string|int $id): void
{
if (config('features.uuid_routes')) {
$query->whereUuid($id);
} else {
$query->whereId($id);
}
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Http/Controllers/UploadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ public function test_it_uploads_shots()
$this->assertNotNull($parentRecord, 'it created parent record');
$this->assertNotNull($childRecord, 'it created child record');
}

public function test_it_returns_json()
{
Storage::fake();

$image = UploadedFile::fake()->image('fileA.jpg');

$response = $this
->actingAs($this->user)
->post(route('api.upload'), [
'images' => [$image],
], [
'accept' => 'application/json',
]);

$shot = Shot::first();

$this->assertEquals([
'data' => [
'link' => $shot->links['url'],
],
], json_decode($response->getContent(), true), 'it returns json');
}
}

0 comments on commit 0497567

Please sign in to comment.