-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change layout for public lists (#57)
- Loading branch information
Showing
5 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Http\ViewModels\User; | ||
|
||
use App\Helpers\StringHelper; | ||
use App\Models\Name; | ||
use App\Models\NameList; | ||
use Illuminate\Support\Number; | ||
|
||
class PublicListViewModel | ||
{ | ||
public static function show(NameList $list): array | ||
{ | ||
$names = $list->names() | ||
->withPivot('public_note') | ||
->orderBy('name')->get() | ||
->map(fn (Name $name) => [ | ||
'id' => $name->id, | ||
'name' => StringHelper::formatNameFromDB($name->name), | ||
'total' => Number::format($name->total), | ||
'public_note' => $name->pivot->public_note, | ||
'url' => [ | ||
'show' => route('name.show', [ | ||
'id' => $name->id, | ||
'name' => StringHelper::sanitizeNameForURL($name->name), | ||
]), | ||
'favorite' => route('favorite.update', [ | ||
'id' => $name->id, | ||
]), | ||
], | ||
]); | ||
|
||
return [ | ||
'id' => $list->id, | ||
'name' => $list->name, | ||
'description' => $list->description, | ||
'names' => $names, | ||
'created_at' => $list->created_at->isoFormat('LL'), | ||
'url' => [ | ||
'show' => route('list.show', [ | ||
'liste' => $list->id, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\ViewModels\User; | ||
|
||
use App\Http\ViewModels\User\PublicListViewModel; | ||
use App\Models\Name; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Tests\TestCase; | ||
|
||
class PublicListViewModelTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_gets_the_list_of_names_in_the_list(): void | ||
{ | ||
$name = Name::factory()->create([ | ||
'name' => 'test', | ||
'total' => 1000, | ||
'origins' => 'test', | ||
]); | ||
$user = User::factory()->create(); | ||
$nameList = $user->lists()->create([ | ||
'is_list_of_favorites' => true, | ||
'uuid' => '1234567890', | ||
]); | ||
$nameList->names()->attach($name->id); | ||
|
||
$this->be($user); | ||
|
||
$array = PublicListViewModel::show($nameList); | ||
|
||
$this->assertCount(6, $array); | ||
$this->assertArrayHasKey('id', $array); | ||
$this->assertArrayHasKey('name', $array); | ||
$this->assertArrayHasKey('description', $array); | ||
$this->assertArrayHasKey('names', $array); | ||
$this->assertArrayHasKey('url', $array); | ||
$this->assertEquals( | ||
[ | ||
0 => [ | ||
'id' => $name->id, | ||
'name' => 'Test', | ||
'total' => '1 000', | ||
'public_note' => null, | ||
'url' => [ | ||
'show' => env('APP_URL') . '/prenoms/' . $name->id . '/test', | ||
'favorite' => env('APP_URL') . '/prenoms/' . $name->id . '/favorite', | ||
], | ||
], | ||
], | ||
$array['names']->toArray() | ||
); | ||
} | ||
} |