mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
refactor: better repository method naming and usage
This commit is contained in:
parent
5f0eaf228d
commit
d8282557d7
12 changed files with 28 additions and 31 deletions
|
@ -20,6 +20,6 @@ class AlbumController extends Controller
|
||||||
|
|
||||||
public function show(Album $album)
|
public function show(Album $album)
|
||||||
{
|
{
|
||||||
return AlbumResource::make($this->repository->getOne($album->id));
|
return AlbumResource::make($album);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,6 @@ class ArtistController extends Controller
|
||||||
|
|
||||||
public function show(Artist $artist)
|
public function show(Artist $artist)
|
||||||
{
|
{
|
||||||
return ArtistResource::make($this->repository->getOne($artist->id));
|
return ArtistResource::make($artist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ class PlaylistController extends Controller
|
||||||
|
|
||||||
if ($request->folder_id) {
|
if ($request->folder_id) {
|
||||||
/** @var PlaylistFolder $folder */
|
/** @var PlaylistFolder $folder */
|
||||||
$folder = $this->folderRepository->getOneById($request->folder_id);
|
$folder = $this->folderRepository->getOne($request->folder_id);
|
||||||
$this->authorize('own', $folder);
|
$this->authorize('own', $folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class PlaylistController extends Controller
|
||||||
|
|
||||||
if ($request->folder_id) {
|
if ($request->folder_id) {
|
||||||
/** @var PlaylistFolder $folder */
|
/** @var PlaylistFolder $folder */
|
||||||
$folder = $this->folderRepository->getOneById($request->folder_id);
|
$folder = $this->folderRepository->getOne($request->folder_id);
|
||||||
$this->authorize('own', $folder);
|
$this->authorize('own', $folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,9 +53,9 @@ class SongController extends Controller
|
||||||
$this->authorize('admin', $this->user);
|
$this->authorize('admin', $this->user);
|
||||||
|
|
||||||
$updatedSongs = $this->songService->updateSongs($request->songs, SongUpdateData::fromRequest($request));
|
$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(
|
array_merge(
|
||||||
$updatedSongs->pluck('artist_id')->all(),
|
$updatedSongs->pluck('artist_id')->all(),
|
||||||
$updatedSongs->pluck('album_artist_id')->all()
|
$updatedSongs->pluck('album_artist_id')->all()
|
||||||
|
|
|
@ -15,7 +15,7 @@ class SongController extends Controller
|
||||||
|
|
||||||
public function show(SongRequest $request)
|
public function show(SongRequest $request)
|
||||||
{
|
{
|
||||||
$songs = $this->songRepository->getByIds($request->songs);
|
$songs = $this->songRepository->getMany($request->songs);
|
||||||
|
|
||||||
return response()->download($this->downloadService->from($songs));
|
return response()->download($this->downloadService->from($songs));
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,6 @@ class AlbumRepository extends Repository
|
||||||
{
|
{
|
||||||
use Searchable;
|
use Searchable;
|
||||||
|
|
||||||
public function getOne(int $id): Album
|
|
||||||
{
|
|
||||||
return Album::query()->find($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return Collection|array<array-key, Album> */
|
/** @return Collection|array<array-key, Album> */
|
||||||
public function getRecentlyAdded(int $count = 6): Collection
|
public function getRecentlyAdded(int $count = 6): Collection
|
||||||
{
|
{
|
||||||
|
|
|
@ -37,13 +37,8 @@ class ArtistRepository extends Repository
|
||||||
->get('artists.*');
|
->get('artists.*');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOne(int $id): Artist
|
|
||||||
{
|
|
||||||
return Artist::query()->find($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return Collection|array<array-key, Artist> */
|
/** @return Collection|array<array-key, Artist> */
|
||||||
public function getByIds(array $ids, bool $inThatOrder = false): Collection
|
public function getMany(array $ids, bool $inThatOrder = false): Collection
|
||||||
{
|
{
|
||||||
$artists = Artist::query()
|
$artists = Artist::query()
|
||||||
->isStandard()
|
->isStandard()
|
||||||
|
|
|
@ -27,20 +27,25 @@ abstract class Repository implements RepositoryInterface
|
||||||
return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class);
|
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 $this->model->find($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Collection|array<Model> */
|
/** @return Collection|array<array-key, Model> */
|
||||||
public function getByIds(array $ids, bool $inThatOrder = false): Collection
|
public function getMany(array $ids, bool $inThatOrder = false): Collection
|
||||||
{
|
{
|
||||||
$models = $this->model::query()->find($ids);
|
$models = $this->model::query()->find($ids);
|
||||||
|
|
||||||
return $inThatOrder ? $models->orderByArray($ids) : $models;
|
return $inThatOrder ? $models->orderByArray($ids) : $models;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Collection|array<Model> */
|
/** @return Collection|array<array-key, Model> */
|
||||||
public function getAll(): Collection
|
public function getAll(): Collection
|
||||||
{
|
{
|
||||||
return $this->model->all();
|
return $this->model->all();
|
||||||
|
|
|
@ -7,10 +7,12 @@ use Illuminate\Support\Collection;
|
||||||
|
|
||||||
interface RepositoryInterface
|
interface RepositoryInterface
|
||||||
{
|
{
|
||||||
public function getOneById($id): ?Model;
|
public function getOne($id): Model;
|
||||||
|
|
||||||
|
public function findOne($id): ?Model;
|
||||||
|
|
||||||
/** @return Collection|array<Model> */
|
/** @return Collection|array<Model> */
|
||||||
public function getByIds(array $ids, bool $inThatOrder = false): Collection;
|
public function getMany(array $ids, bool $inThatOrder = false): Collection;
|
||||||
|
|
||||||
/** @return Collection|array<Model> */
|
/** @return Collection|array<Model> */
|
||||||
public function getAll(): Collection;
|
public function getAll(): Collection;
|
||||||
|
|
|
@ -175,7 +175,7 @@ class SongRepository extends Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Collection|array<array-key, Song> */
|
/** @return Collection|array<array-key, Song> */
|
||||||
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()
|
$songs = Song::query()
|
||||||
->withMeta($scopedUser ?? $this->auth->user())
|
->withMeta($scopedUser ?? $this->auth->user())
|
||||||
|
@ -185,12 +185,12 @@ class SongRepository extends Repository
|
||||||
return $inThatOrder ? $songs->orderByArray($ids) : $songs;
|
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);
|
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);
|
return Song::query()->withMeta($scopedUser ?? $this->auth->user())->find($id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ class QueueService
|
||||||
$currentSong = $state->current_song_id ? $this->songRepository->findOne($state->current_song_id, $user) : null;
|
$currentSong = $state->current_song_id ? $this->songRepository->findOne($state->current_song_id, $user) : null;
|
||||||
|
|
||||||
return QueueStateDTO::create(
|
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,
|
$currentSong,
|
||||||
$state->playback_position ?? 0
|
$state->playback_position ?? 0
|
||||||
);
|
);
|
||||||
|
|
|
@ -33,13 +33,13 @@ class SearchService
|
||||||
$scopedUser ??= auth()->user();
|
$scopedUser ??= auth()->user();
|
||||||
|
|
||||||
return ExcerptSearchResult::make(
|
return ExcerptSearchResult::make(
|
||||||
$this->songRepository->getByIds(
|
$this->songRepository->getMany(
|
||||||
ids: Song::search($keywords)->get()->take($count)->pluck('id')->all(),
|
ids: Song::search($keywords)->get()->take($count)->pluck('id')->all(),
|
||||||
inThatOrder: true,
|
inThatOrder: true,
|
||||||
scopedUser: $scopedUser
|
scopedUser: $scopedUser
|
||||||
),
|
),
|
||||||
$this->artistRepository->getByIds(Artist::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
$this->artistRepository->getMany(Artist::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
||||||
$this->albumRepository->getByIds(Album::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
$this->albumRepository->getMany(Album::search($keywords)->get()->take($count)->pluck('id')->all(), true),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue