Skip to content

Commit

Permalink
DTO → Resources (#1668)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Feb 5, 2023
1 parent 7c94325 commit ba7cab2
Show file tree
Hide file tree
Showing 102 changed files with 2,029 additions and 1,342 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
build/*

public/dist/user.css

Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dist: dist-clean
@zip -r Lychee.zip Lychee

clean:
@rm build/* 2> /dev/null || true
@rm -r Lychee 2> /dev/null || true

test:
Expand Down Expand Up @@ -102,3 +103,11 @@ gen_major:

release_major: gen_major
git commit -m "bump to version $(shell cat version.md)"

TESTS_PHP := $(shell find tests/Feature -name "*Test.php" -printf "%f\n")
TEST_DONE := $(addprefix build/,$(TESTS_PHP:.php=.done))

build/%.done: tests/Feature/%.php
vendor/bin/phpunit --filter $* && touch build/$*.done

all_tests: $(TEST_DONE)
6 changes: 3 additions & 3 deletions app/Actions/Album/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace App\Actions\Album;

use App\Contracts\Models\AbstractAlbum;
use App\DTO\PositionData as PositionDataDTO;
use App\Enum\SizeVariantType;
use App\Http\Resources\Collections\PositionDataResource;
use App\Models\Album;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class PositionData extends Action
{
public function get(AbstractAlbum $album, bool $includeSubAlbums = false): PositionDataDTO
public function get(AbstractAlbum $album, bool $includeSubAlbums = false): PositionDataResource
{
$photoRelation = ($album instanceof Album && $includeSubAlbums) ?
$album->all_photos() :
Expand Down Expand Up @@ -40,6 +40,6 @@ public function get(AbstractAlbum $album, bool $includeSubAlbums = false): Posit
->whereNotNull('latitude')
->whereNotNull('longitude');

return new PositionDataDTO($album->id, $album->title, $photoRelation->get(), $album instanceof Album ? $album->track_url : null);
return new PositionDataResource($album->id, $album->title, $photoRelation->get(), $album instanceof Album ? $album->track_url : null);
}
}
8 changes: 4 additions & 4 deletions app/Actions/Albums/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Actions\Albums;

use App\Contracts\Exceptions\InternalLycheeException;
use App\DTO\PositionData as PositionDataDTO;
use App\Enum\SizeVariantType;
use App\Http\Resources\Collections\PositionDataResource;
use App\Models\Configs;
use App\Models\Photo;
use App\Policies\PhotoQueryPolicy;
Expand All @@ -25,11 +25,11 @@ public function __construct(PhotoQueryPolicy $photoQueryPolicy)
/**
* Given a list of albums, generate an array to be returned.
*
* @return PositionDataDTO
* @return PositionDataResource
*
* @throws InternalLycheeException
*/
public function do(): PositionDataDTO
public function do(): PositionDataResource
{
$photoQuery = $this->photoQueryPolicy->applySearchabilityFilter(
Photo::query()
Expand All @@ -56,6 +56,6 @@ public function do(): PositionDataDTO
->whereNotNull('longitude')
);

return new PositionDataDTO(null, null, $photoQuery->get(), null);
return new PositionDataResource(null, null, $photoQuery->get(), null);
}
}
12 changes: 6 additions & 6 deletions app/Actions/Albums/Top.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use App\Contracts\Exceptions\InternalLycheeException;
use App\DTO\AlbumSortingCriterion;
use App\DTO\TopAlbums;
use App\Enum\ColumnSortingType;
use App\Enum\OrderSortingType;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\InvalidOrderDirectionException;
use App\Factories\AlbumFactory;
use App\Http\Resources\Collections\TopAlbumsResource;
use App\Models\Album;
use App\Models\Configs;
use App\Models\Extensions\SortingDecorator;
Expand Down Expand Up @@ -54,11 +54,11 @@ public function __construct(AlbumFactory $albumFactory, AlbumQueryPolicy $albumQ
* Note, the result may include password-protected albums that are not
* accessible (but are visible).
*
* @return TopAlbums
* @return TopAlbumsResource
*
* @throws InternalLycheeException
*/
public function get(): TopAlbums
public function get(): TopAlbumsResource
{
if (Configs::getValueAsBool('SA_enabled')) {
// Do not eagerly load the relation `photos` for each smart album.
Expand All @@ -68,7 +68,7 @@ public function get(): TopAlbums
->getAllBuiltInSmartAlbums(false)
->map(
fn ($smartAlbum) => Gate::check(AlbumPolicy::CAN_SEE, $smartAlbum) ? $smartAlbum : null
);
)->reject(fn ($smartAlbum) => $smartAlbum === null);
} else {
$smartAlbums = new BaseCollection();
}
Expand Down Expand Up @@ -98,15 +98,15 @@ public function get(): TopAlbums
*/
list($a, $b) = $albums->partition(fn ($album) => $album->owner_id === $userID);

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

return new TopAlbums($smartAlbums, $tagAlbums, $albums);
return new TopAlbumsResource($smartAlbums, $tagAlbums, $albums);
}
}
}
8 changes: 4 additions & 4 deletions app/Actions/Albums/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use App\Contracts\Exceptions\InternalLycheeException;
use App\DTO\AlbumSortingCriterion;
use App\DTO\AlbumTree;
use App\Enum\ColumnSortingType;
use App\Enum\OrderSortingType;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\InvalidOrderDirectionException;
use App\Http\Resources\Collections\AlbumForestResource;
use App\Models\Album;
use App\Models\Extensions\SortingDecorator;
use App\Policies\AlbumQueryPolicy;
Expand All @@ -31,11 +31,11 @@ public function __construct(AlbumQueryPolicy $albumQueryPolicy)
}

/**
* @return AlbumTree
* @return AlbumForestResource
*
* @throws InternalLycheeException
*/
public function get(): AlbumTree
public function get(): AlbumForestResource
{
/*
* Note, strictly speaking
Expand Down Expand Up @@ -81,6 +81,6 @@ public function get(): AlbumTree
// as there are several top-level albums below root.
// Otherwise, `toTree` uses the ID of the album with the lowest
// `_lft` value as the (wrong) root album.
return new AlbumTree($albums->toTree(null), $sharedAlbums?->toTree(null));
return new AlbumForestResource($albums->toTree(null), $sharedAlbums?->toTree(null));
}
}
31 changes: 18 additions & 13 deletions app/Actions/Diagnostics/Pipes/Checks/ConfigSanityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@
namespace App\Actions\Diagnostics\Pipes\Checks;

use App\Contracts\DiagnosticPipe;
use App\ModelFunctions\ConfigFunctions;
use App\Models\Configs;

class ConfigSanityCheck implements DiagnosticPipe
{
private ConfigFunctions $configFunctions;

private array $settings;

/**
* @param ConfigFunctions $configFunctions
*/
public function __construct(
ConfigFunctions $configFunctions
) {
$this->configFunctions = $configFunctions;
}

public function handle(array &$data, \Closure $next): array
{
// Load settings
$this->settings = Configs::get();

$this->checkKeysExistsAndSet($data);

$this->configFunctions->sanity($data);
$this->sanity($data);

$this->checkDropBoxKeyWarning($data);

Expand Down Expand Up @@ -59,4 +47,21 @@ private function checkDropBoxKeyWarning(array &$data): void
= 'Warning: Dropbox import not working. dropbox_key is empty.';
}
}

/**
* Sanity check of the config.
*
* @param array $return
*/
private function sanity(array &$return): void
{
$configs = Configs::all(['key', 'value', 'type_range']);

foreach ($configs as $config) {
$message = $config->sanity($config->value);
if ($message !== '') {
$return[] = $message;
}
}
}
}
50 changes: 18 additions & 32 deletions app/Actions/Sharing/ListShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Actions\Sharing;

use App\DTO\Shares;
use App\Exceptions\Internal\QueryBuilderException;
use App\Http\Resources\Sharing\SharesResource;
use App\Models\Extensions\BaseAlbum;
use App\Models\User;
use Illuminate\Support\Collection;
Expand All @@ -21,16 +21,16 @@ class ListShare
* which are shared
* @param BaseAlbum|null $baseAlbum the optional album which is shared
*
* @return Shares
* @return SharesResource
*
* @throws QueryBuilderException
*/
public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Shares
public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): SharesResource
{
try {
// Active shares, optionally filtered by album ID, participant ID
// and or owner ID
$shared_query = DB::table('user_base_album')
$shared = DB::table('user_base_album')
->select([
'user_base_album.id',
'user_id',
Expand All @@ -39,34 +39,23 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha
'title',
])
->join('users', 'user_id', '=', 'users.id')
->join('base_albums', 'base_album_id', '=', 'base_albums.id');
if ($participant !== null) {
$shared_query->where('user_base_album.user_id', '=', $participant->id);
}
if ($owner !== null) {
$shared_query->where('base_albums.owner_id', '=', $owner->id);
}
if ($baseAlbum !== null) {
$shared_query->where('base_albums.id', '=', $baseAlbum->id);
}
$shared = $shared_query
->join('base_albums', 'base_album_id', '=', 'base_albums.id')
->when($participant !== null, fn ($q) => $q->where('user_base_album.user_id', '=', $participant->id))
->when($owner !== null, fn ($q) => $q->where('base_albums.owner_id', '=', $owner->id))
->when($baseAlbum !== null, fn ($q) => $q->where('base_albums.id', '=', $baseAlbum->id))
->orderBy('title', 'ASC')
->orderBy('username', 'ASC')
->get();

// Existing albums which can be shared optionally filtered by
// album ID and/or owner ID
$albums_query = DB::table('base_albums')
$albums = DB::table('base_albums')
->leftJoin('albums', 'albums.id', '=', 'base_albums.id')
->select(['base_albums.id', 'title', 'parent_id'])
->orderBy('title', 'ASC');
if ($owner !== null) {
$albums_query->where('owner_id', '=', $owner->id);
}
if ($baseAlbum !== null) {
$albums_query->where('base_albums.id', '=', $baseAlbum->id);
}
$albums = $albums_query->get();
->when($owner !== null, fn ($q) => $q->where('owner_id', '=', $owner->id))
->when($baseAlbum !== null, fn ($q) => $q->where('base_albums.id', '=', $baseAlbum->id))
->orderBy('title', 'ASC')
->get();
$this->linkAlbums($albums);
$albums->each(function ($album) {
$album->title = $this->breadcrumbPath($album);
Expand All @@ -78,19 +67,16 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha

// Existing users with whom an album can be shared optionally
// filtered by participant ID
$users_query = DB::table('users')->select(['id', 'username']);
if ($participant !== null) {
$users_query->where('id', '=', $participant->id);
} else {
$users_query->where('id', '>', 0);
}
$users = $users_query->orderBy('username', 'ASC')
$users = DB::table('users')->select(['id', 'username'])
->when($participant !== null, fn ($q) => $q->where('id', '=', $participant->id))
->when($participant === null, fn ($q) => $q->where('may_administrate', '=', false))
->orderBy('username', 'ASC')
->get()
->each(function ($user) {
$user->id = intval($user->id);
});

return new Shares($shared, $albums, $users);
return new SharesResource($shared, $albums, $users);
} catch (\InvalidArgumentException $e) {
throw new QueryBuilderException($e);
}
Expand Down
12 changes: 12 additions & 0 deletions app/Actions/Sharing/ListedAlbum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Actions\Sharing;

/**
* @property string $id
* @property string $title
* @property string $parent_id
*/
class ListedAlbum
{
}
14 changes: 14 additions & 0 deletions app/Actions/Sharing/SharedAlbum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Actions\Sharing;

/**
* @property int $id
* @property int $user_id
* @property string $album_id
* @property string $username
* @property string $title
*/
class SharedAlbum
{
}
11 changes: 11 additions & 0 deletions app/Actions/Sharing/UserShared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Actions\Sharing;

/**
* @property int $id
* @property string $username
*/
class UserShared
{
}
Loading

0 comments on commit ba7cab2

Please sign in to comment.