Skip to content

Commit ba7cab2

Browse files
authored
DTO → Resources (#1668)
1 parent 7c94325 commit ba7cab2

File tree

102 files changed

+2029
-1342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2029
-1342
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/node_modules
2+
build/*
23

34
public/dist/user.css
45

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dist: dist-clean
5959
@zip -r Lychee.zip Lychee
6060

6161
clean:
62+
@rm build/* 2> /dev/null || true
6263
@rm -r Lychee 2> /dev/null || true
6364

6465
test:
@@ -102,3 +103,11 @@ gen_major:
102103

103104
release_major: gen_major
104105
git commit -m "bump to version $(shell cat version.md)"
106+
107+
TESTS_PHP := $(shell find tests/Feature -name "*Test.php" -printf "%f\n")
108+
TEST_DONE := $(addprefix build/,$(TESTS_PHP:.php=.done))
109+
110+
build/%.done: tests/Feature/%.php
111+
vendor/bin/phpunit --filter $* && touch build/$*.done
112+
113+
all_tests: $(TEST_DONE)

app/Actions/Album/PositionData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace App\Actions\Album;
44

55
use App\Contracts\Models\AbstractAlbum;
6-
use App\DTO\PositionData as PositionDataDTO;
76
use App\Enum\SizeVariantType;
7+
use App\Http\Resources\Collections\PositionDataResource;
88
use App\Models\Album;
99
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
1111

1212
class PositionData extends Action
1313
{
14-
public function get(AbstractAlbum $album, bool $includeSubAlbums = false): PositionDataDTO
14+
public function get(AbstractAlbum $album, bool $includeSubAlbums = false): PositionDataResource
1515
{
1616
$photoRelation = ($album instanceof Album && $includeSubAlbums) ?
1717
$album->all_photos() :
@@ -40,6 +40,6 @@ public function get(AbstractAlbum $album, bool $includeSubAlbums = false): Posit
4040
->whereNotNull('latitude')
4141
->whereNotNull('longitude');
4242

43-
return new PositionDataDTO($album->id, $album->title, $photoRelation->get(), $album instanceof Album ? $album->track_url : null);
43+
return new PositionDataResource($album->id, $album->title, $photoRelation->get(), $album instanceof Album ? $album->track_url : null);
4444
}
4545
}

app/Actions/Albums/PositionData.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace App\Actions\Albums;
44

55
use App\Contracts\Exceptions\InternalLycheeException;
6-
use App\DTO\PositionData as PositionDataDTO;
76
use App\Enum\SizeVariantType;
7+
use App\Http\Resources\Collections\PositionDataResource;
88
use App\Models\Configs;
99
use App\Models\Photo;
1010
use App\Policies\PhotoQueryPolicy;
@@ -25,11 +25,11 @@ public function __construct(PhotoQueryPolicy $photoQueryPolicy)
2525
/**
2626
* Given a list of albums, generate an array to be returned.
2727
*
28-
* @return PositionDataDTO
28+
* @return PositionDataResource
2929
*
3030
* @throws InternalLycheeException
3131
*/
32-
public function do(): PositionDataDTO
32+
public function do(): PositionDataResource
3333
{
3434
$photoQuery = $this->photoQueryPolicy->applySearchabilityFilter(
3535
Photo::query()
@@ -56,6 +56,6 @@ public function do(): PositionDataDTO
5656
->whereNotNull('longitude')
5757
);
5858

59-
return new PositionDataDTO(null, null, $photoQuery->get(), null);
59+
return new PositionDataResource(null, null, $photoQuery->get(), null);
6060
}
6161
}

app/Actions/Albums/Top.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use App\Contracts\Exceptions\InternalLycheeException;
66
use App\DTO\AlbumSortingCriterion;
7-
use App\DTO\TopAlbums;
87
use App\Enum\ColumnSortingType;
98
use App\Enum\OrderSortingType;
109
use App\Exceptions\ConfigurationKeyMissingException;
1110
use App\Exceptions\Internal\InvalidOrderDirectionException;
1211
use App\Factories\AlbumFactory;
12+
use App\Http\Resources\Collections\TopAlbumsResource;
1313
use App\Models\Album;
1414
use App\Models\Configs;
1515
use App\Models\Extensions\SortingDecorator;
@@ -54,11 +54,11 @@ public function __construct(AlbumFactory $albumFactory, AlbumQueryPolicy $albumQ
5454
* Note, the result may include password-protected albums that are not
5555
* accessible (but are visible).
5656
*
57-
* @return TopAlbums
57+
* @return TopAlbumsResource
5858
*
5959
* @throws InternalLycheeException
6060
*/
61-
public function get(): TopAlbums
61+
public function get(): TopAlbumsResource
6262
{
6363
if (Configs::getValueAsBool('SA_enabled')) {
6464
// Do not eagerly load the relation `photos` for each smart album.
@@ -68,7 +68,7 @@ public function get(): TopAlbums
6868
->getAllBuiltInSmartAlbums(false)
6969
->map(
7070
fn ($smartAlbum) => Gate::check(AlbumPolicy::CAN_SEE, $smartAlbum) ? $smartAlbum : null
71-
);
71+
)->reject(fn ($smartAlbum) => $smartAlbum === null);
7272
} else {
7373
$smartAlbums = new BaseCollection();
7474
}
@@ -98,15 +98,15 @@ public function get(): TopAlbums
9898
*/
9999
list($a, $b) = $albums->partition(fn ($album) => $album->owner_id === $userID);
100100

101-
return new TopAlbums($smartAlbums, $tagAlbums, $a->values(), $b->values());
101+
return new TopAlbumsResource($smartAlbums, $tagAlbums, $a->values(), $b->values());
102102
} else {
103103
// For anonymous users we don't want to implicitly expose
104104
// ownership via sorting.
105105
$albums = (new SortingDecorator($query))
106106
->orderBy($this->sorting->column, $this->sorting->order)
107107
->get();
108108

109-
return new TopAlbums($smartAlbums, $tagAlbums, $albums);
109+
return new TopAlbumsResource($smartAlbums, $tagAlbums, $albums);
110110
}
111111
}
112112
}

app/Actions/Albums/Tree.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use App\Contracts\Exceptions\InternalLycheeException;
66
use App\DTO\AlbumSortingCriterion;
7-
use App\DTO\AlbumTree;
87
use App\Enum\ColumnSortingType;
98
use App\Enum\OrderSortingType;
109
use App\Exceptions\ConfigurationKeyMissingException;
1110
use App\Exceptions\Internal\InvalidOrderDirectionException;
11+
use App\Http\Resources\Collections\AlbumForestResource;
1212
use App\Models\Album;
1313
use App\Models\Extensions\SortingDecorator;
1414
use App\Policies\AlbumQueryPolicy;
@@ -31,11 +31,11 @@ public function __construct(AlbumQueryPolicy $albumQueryPolicy)
3131
}
3232

3333
/**
34-
* @return AlbumTree
34+
* @return AlbumForestResource
3535
*
3636
* @throws InternalLycheeException
3737
*/
38-
public function get(): AlbumTree
38+
public function get(): AlbumForestResource
3939
{
4040
/*
4141
* Note, strictly speaking
@@ -81,6 +81,6 @@ public function get(): AlbumTree
8181
// as there are several top-level albums below root.
8282
// Otherwise, `toTree` uses the ID of the album with the lowest
8383
// `_lft` value as the (wrong) root album.
84-
return new AlbumTree($albums->toTree(null), $sharedAlbums?->toTree(null));
84+
return new AlbumForestResource($albums->toTree(null), $sharedAlbums?->toTree(null));
8585
}
8686
}

app/Actions/Diagnostics/Pipes/Checks/ConfigSanityCheck.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,20 @@
33
namespace App\Actions\Diagnostics\Pipes\Checks;
44

55
use App\Contracts\DiagnosticPipe;
6-
use App\ModelFunctions\ConfigFunctions;
76
use App\Models\Configs;
87

98
class ConfigSanityCheck implements DiagnosticPipe
109
{
11-
private ConfigFunctions $configFunctions;
12-
1310
private array $settings;
1411

15-
/**
16-
* @param ConfigFunctions $configFunctions
17-
*/
18-
public function __construct(
19-
ConfigFunctions $configFunctions
20-
) {
21-
$this->configFunctions = $configFunctions;
22-
}
23-
2412
public function handle(array &$data, \Closure $next): array
2513
{
2614
// Load settings
2715
$this->settings = Configs::get();
2816

2917
$this->checkKeysExistsAndSet($data);
3018

31-
$this->configFunctions->sanity($data);
19+
$this->sanity($data);
3220

3321
$this->checkDropBoxKeyWarning($data);
3422

@@ -59,4 +47,21 @@ private function checkDropBoxKeyWarning(array &$data): void
5947
= 'Warning: Dropbox import not working. dropbox_key is empty.';
6048
}
6149
}
50+
51+
/**
52+
* Sanity check of the config.
53+
*
54+
* @param array $return
55+
*/
56+
private function sanity(array &$return): void
57+
{
58+
$configs = Configs::all(['key', 'value', 'type_range']);
59+
60+
foreach ($configs as $config) {
61+
$message = $config->sanity($config->value);
62+
if ($message !== '') {
63+
$return[] = $message;
64+
}
65+
}
66+
}
6267
}

app/Actions/Sharing/ListShare.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App\Actions\Sharing;
44

5-
use App\DTO\Shares;
65
use App\Exceptions\Internal\QueryBuilderException;
6+
use App\Http\Resources\Sharing\SharesResource;
77
use App\Models\Extensions\BaseAlbum;
88
use App\Models\User;
99
use Illuminate\Support\Collection;
@@ -21,16 +21,16 @@ class ListShare
2121
* which are shared
2222
* @param BaseAlbum|null $baseAlbum the optional album which is shared
2323
*
24-
* @return Shares
24+
* @return SharesResource
2525
*
2626
* @throws QueryBuilderException
2727
*/
28-
public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Shares
28+
public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): SharesResource
2929
{
3030
try {
3131
// Active shares, optionally filtered by album ID, participant ID
3232
// and or owner ID
33-
$shared_query = DB::table('user_base_album')
33+
$shared = DB::table('user_base_album')
3434
->select([
3535
'user_base_album.id',
3636
'user_id',
@@ -39,34 +39,23 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha
3939
'title',
4040
])
4141
->join('users', 'user_id', '=', 'users.id')
42-
->join('base_albums', 'base_album_id', '=', 'base_albums.id');
43-
if ($participant !== null) {
44-
$shared_query->where('user_base_album.user_id', '=', $participant->id);
45-
}
46-
if ($owner !== null) {
47-
$shared_query->where('base_albums.owner_id', '=', $owner->id);
48-
}
49-
if ($baseAlbum !== null) {
50-
$shared_query->where('base_albums.id', '=', $baseAlbum->id);
51-
}
52-
$shared = $shared_query
42+
->join('base_albums', 'base_album_id', '=', 'base_albums.id')
43+
->when($participant !== null, fn ($q) => $q->where('user_base_album.user_id', '=', $participant->id))
44+
->when($owner !== null, fn ($q) => $q->where('base_albums.owner_id', '=', $owner->id))
45+
->when($baseAlbum !== null, fn ($q) => $q->where('base_albums.id', '=', $baseAlbum->id))
5346
->orderBy('title', 'ASC')
5447
->orderBy('username', 'ASC')
5548
->get();
5649

5750
// Existing albums which can be shared optionally filtered by
5851
// album ID and/or owner ID
59-
$albums_query = DB::table('base_albums')
52+
$albums = DB::table('base_albums')
6053
->leftJoin('albums', 'albums.id', '=', 'base_albums.id')
6154
->select(['base_albums.id', 'title', 'parent_id'])
62-
->orderBy('title', 'ASC');
63-
if ($owner !== null) {
64-
$albums_query->where('owner_id', '=', $owner->id);
65-
}
66-
if ($baseAlbum !== null) {
67-
$albums_query->where('base_albums.id', '=', $baseAlbum->id);
68-
}
69-
$albums = $albums_query->get();
55+
->when($owner !== null, fn ($q) => $q->where('owner_id', '=', $owner->id))
56+
->when($baseAlbum !== null, fn ($q) => $q->where('base_albums.id', '=', $baseAlbum->id))
57+
->orderBy('title', 'ASC')
58+
->get();
7059
$this->linkAlbums($albums);
7160
$albums->each(function ($album) {
7261
$album->title = $this->breadcrumbPath($album);
@@ -78,19 +67,16 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha
7867

7968
// Existing users with whom an album can be shared optionally
8069
// filtered by participant ID
81-
$users_query = DB::table('users')->select(['id', 'username']);
82-
if ($participant !== null) {
83-
$users_query->where('id', '=', $participant->id);
84-
} else {
85-
$users_query->where('id', '>', 0);
86-
}
87-
$users = $users_query->orderBy('username', 'ASC')
70+
$users = DB::table('users')->select(['id', 'username'])
71+
->when($participant !== null, fn ($q) => $q->where('id', '=', $participant->id))
72+
->when($participant === null, fn ($q) => $q->where('may_administrate', '=', false))
73+
->orderBy('username', 'ASC')
8874
->get()
8975
->each(function ($user) {
9076
$user->id = intval($user->id);
9177
});
9278

93-
return new Shares($shared, $albums, $users);
79+
return new SharesResource($shared, $albums, $users);
9480
} catch (\InvalidArgumentException $e) {
9581
throw new QueryBuilderException($e);
9682
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Actions\Sharing;
4+
5+
/**
6+
* @property string $id
7+
* @property string $title
8+
* @property string $parent_id
9+
*/
10+
class ListedAlbum
11+
{
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Actions\Sharing;
4+
5+
/**
6+
* @property int $id
7+
* @property int $user_id
8+
* @property string $album_id
9+
* @property string $username
10+
* @property string $title
11+
*/
12+
class SharedAlbum
13+
{
14+
}

0 commit comments

Comments
 (0)