From d8282557d74f61171c868e3bcd47c83c51e4c6dd Mon Sep 17 00:00:00 2001 From: Phan An Date: Mon, 1 Jan 2024 21:38:41 +0100 Subject: [PATCH] refactor: better repository method naming and usage --- app/Http/Controllers/API/AlbumController.php | 2 +- app/Http/Controllers/API/ArtistController.php | 2 +- app/Http/Controllers/API/PlaylistController.php | 4 ++-- app/Http/Controllers/API/SongController.php | 4 ++-- app/Http/Controllers/Download/SongController.php | 2 +- app/Repositories/AlbumRepository.php | 5 ----- app/Repositories/ArtistRepository.php | 7 +------ app/Repositories/Repository.php | 13 +++++++++---- app/Repositories/RepositoryInterface.php | 6 ++++-- app/Repositories/SongRepository.php | 6 +++--- app/Services/QueueService.php | 2 +- app/Services/SearchService.php | 6 +++--- 12 files changed, 28 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/API/AlbumController.php b/app/Http/Controllers/API/AlbumController.php index d409fbcf..f725fce2 100644 --- a/app/Http/Controllers/API/AlbumController.php +++ b/app/Http/Controllers/API/AlbumController.php @@ -20,6 +20,6 @@ class AlbumController extends Controller public function show(Album $album) { - return AlbumResource::make($this->repository->getOne($album->id)); + return AlbumResource::make($album); } } diff --git a/app/Http/Controllers/API/ArtistController.php b/app/Http/Controllers/API/ArtistController.php index a4940047..be8d7144 100644 --- a/app/Http/Controllers/API/ArtistController.php +++ b/app/Http/Controllers/API/ArtistController.php @@ -20,6 +20,6 @@ class ArtistController extends Controller public function show(Artist $artist) { - return ArtistResource::make($this->repository->getOne($artist->id)); + return ArtistResource::make($artist); } } diff --git a/app/Http/Controllers/API/PlaylistController.php b/app/Http/Controllers/API/PlaylistController.php index f73e67b6..e18b1794 100644 --- a/app/Http/Controllers/API/PlaylistController.php +++ b/app/Http/Controllers/API/PlaylistController.php @@ -38,7 +38,7 @@ class PlaylistController extends Controller if ($request->folder_id) { /** @var PlaylistFolder $folder */ - $folder = $this->folderRepository->getOneById($request->folder_id); + $folder = $this->folderRepository->getOne($request->folder_id); $this->authorize('own', $folder); } @@ -65,7 +65,7 @@ class PlaylistController extends Controller if ($request->folder_id) { /** @var PlaylistFolder $folder */ - $folder = $this->folderRepository->getOneById($request->folder_id); + $folder = $this->folderRepository->getOne($request->folder_id); $this->authorize('own', $folder); } diff --git a/app/Http/Controllers/API/SongController.php b/app/Http/Controllers/API/SongController.php index cc5c031f..458b547f 100644 --- a/app/Http/Controllers/API/SongController.php +++ b/app/Http/Controllers/API/SongController.php @@ -53,9 +53,9 @@ class SongController extends Controller $this->authorize('admin', $this->user); $updatedSongs = $this->songService->updateSongs($request->songs, SongUpdateData::fromRequest($request)); - $albums = $this->albumRepository->getByIds($updatedSongs->pluck('album_id')->toArray()); + $albums = $this->albumRepository->getMany($updatedSongs->pluck('album_id')->toArray()); - $artists = $this->artistRepository->getByIds( + $artists = $this->artistRepository->getMany( array_merge( $updatedSongs->pluck('artist_id')->all(), $updatedSongs->pluck('album_artist_id')->all() diff --git a/app/Http/Controllers/Download/SongController.php b/app/Http/Controllers/Download/SongController.php index c583d12d..2b4b3249 100644 --- a/app/Http/Controllers/Download/SongController.php +++ b/app/Http/Controllers/Download/SongController.php @@ -15,7 +15,7 @@ class SongController extends Controller public function show(SongRequest $request) { - $songs = $this->songRepository->getByIds($request->songs); + $songs = $this->songRepository->getMany($request->songs); return response()->download($this->downloadService->from($songs)); } diff --git a/app/Repositories/AlbumRepository.php b/app/Repositories/AlbumRepository.php index 00165cf7..76d05bbb 100644 --- a/app/Repositories/AlbumRepository.php +++ b/app/Repositories/AlbumRepository.php @@ -14,11 +14,6 @@ class AlbumRepository extends Repository { use Searchable; - public function getOne(int $id): Album - { - return Album::query()->find($id); - } - /** @return Collection|array */ public function getRecentlyAdded(int $count = 6): Collection { diff --git a/app/Repositories/ArtistRepository.php b/app/Repositories/ArtistRepository.php index 5e4b0845..70b021c0 100644 --- a/app/Repositories/ArtistRepository.php +++ b/app/Repositories/ArtistRepository.php @@ -37,13 +37,8 @@ class ArtistRepository extends Repository ->get('artists.*'); } - public function getOne(int $id): Artist - { - return Artist::query()->find($id); - } - /** @return Collection|array */ - public function getByIds(array $ids, bool $inThatOrder = false): Collection + public function getMany(array $ids, bool $inThatOrder = false): Collection { $artists = Artist::query() ->isStandard() diff --git a/app/Repositories/Repository.php b/app/Repositories/Repository.php index 0f50bb84..39af4b57 100644 --- a/app/Repositories/Repository.php +++ b/app/Repositories/Repository.php @@ -27,20 +27,25 @@ abstract class Repository implements RepositoryInterface return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class); } - public function getOneById($id): ?Model + public function getOne($id): Model + { + return $this->model->findOrFail($id); + } + + public function findOne($id): ?Model { return $this->model->find($id); } - /** @return Collection|array */ - public function getByIds(array $ids, bool $inThatOrder = false): Collection + /** @return Collection|array */ + public function getMany(array $ids, bool $inThatOrder = false): Collection { $models = $this->model::query()->find($ids); return $inThatOrder ? $models->orderByArray($ids) : $models; } - /** @return Collection|array */ + /** @return Collection|array */ public function getAll(): Collection { return $this->model->all(); diff --git a/app/Repositories/RepositoryInterface.php b/app/Repositories/RepositoryInterface.php index 26b3e996..3df318fe 100644 --- a/app/Repositories/RepositoryInterface.php +++ b/app/Repositories/RepositoryInterface.php @@ -7,10 +7,12 @@ use Illuminate\Support\Collection; interface RepositoryInterface { - public function getOneById($id): ?Model; + public function getOne($id): Model; + + public function findOne($id): ?Model; /** @return Collection|array */ - public function getByIds(array $ids, bool $inThatOrder = false): Collection; + public function getMany(array $ids, bool $inThatOrder = false): Collection; /** @return Collection|array */ public function getAll(): Collection; diff --git a/app/Repositories/SongRepository.php b/app/Repositories/SongRepository.php index 5c8ff778..1d73ca7e 100644 --- a/app/Repositories/SongRepository.php +++ b/app/Repositories/SongRepository.php @@ -175,7 +175,7 @@ class SongRepository extends Repository } /** @return Collection|array */ - public function getByIds(array $ids, bool $inThatOrder = false, ?User $scopedUser = null): Collection + public function getMany(array $ids, bool $inThatOrder = false, ?User $scopedUser = null): Collection { $songs = Song::query() ->withMeta($scopedUser ?? $this->auth->user()) @@ -185,12 +185,12 @@ class SongRepository extends Repository return $inThatOrder ? $songs->orderByArray($ids) : $songs; } - public function getOne(string $id, ?User $scopedUser = null): Song + public function getOne($id, ?User $scopedUser = null): Song { return Song::query()->withMeta($scopedUser ?? $this->auth->user())->findOrFail($id); } - public function findOne(string $id, ?User $scopedUser = null): ?Song + public function findOne($id, ?User $scopedUser = null): ?Song { return Song::query()->withMeta($scopedUser ?? $this->auth->user())->find($id); } diff --git a/app/Services/QueueService.php b/app/Services/QueueService.php index f1a6c2c3..d6a1f8e9 100644 --- a/app/Services/QueueService.php +++ b/app/Services/QueueService.php @@ -25,7 +25,7 @@ class QueueService $currentSong = $state->current_song_id ? $this->songRepository->findOne($state->current_song_id, $user) : null; return QueueStateDTO::create( - $this->songRepository->getByIds(ids: $state->song_ids, inThatOrder: true, scopedUser: $user), + $this->songRepository->getMany(ids: $state->song_ids, inThatOrder: true, scopedUser: $user), $currentSong, $state->playback_position ?? 0 ); diff --git a/app/Services/SearchService.php b/app/Services/SearchService.php index 8c4fafb3..f9045fd3 100644 --- a/app/Services/SearchService.php +++ b/app/Services/SearchService.php @@ -33,13 +33,13 @@ class SearchService $scopedUser ??= auth()->user(); return ExcerptSearchResult::make( - $this->songRepository->getByIds( + $this->songRepository->getMany( ids: Song::search($keywords)->get()->take($count)->pluck('id')->all(), inThatOrder: true, scopedUser: $scopedUser ), - $this->artistRepository->getByIds(Artist::search($keywords)->get()->take($count)->pluck('id')->all(), true), - $this->albumRepository->getByIds(Album::search($keywords)->get()->take($count)->pluck('id')->all(), true), + $this->artistRepository->getMany(Artist::search($keywords)->get()->take($count)->pluck('id')->all(), true), + $this->albumRepository->getMany(Album::search($keywords)->get()->take($count)->pluck('id')->all(), true), ); }