koel/app/Http/Controllers/API/SongController.php

87 lines
2.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2022-07-29 06:47:10 +00:00
use App\Http\Controllers\Controller;
2023-06-05 21:46:41 +00:00
use App\Http\Requests\API\DeleteSongsRequest;
use App\Http\Requests\API\SongListRequest;
2016-03-05 09:01:12 +00:00
use App\Http\Requests\API\SongUpdateRequest;
2022-06-10 10:47:46 +00:00
use App\Http\Resources\AlbumResource;
use App\Http\Resources\ArtistResource;
use App\Http\Resources\SongResource;
2023-06-05 21:46:41 +00:00
use App\Models\Song;
use App\Models\User;
2018-08-29 06:30:39 +00:00
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
2023-06-05 21:46:41 +00:00
use App\Repositories\SongRepository;
2022-06-10 10:47:46 +00:00
use App\Services\LibraryManager;
use App\Services\SongService;
use App\Values\SongUpdateData;
2023-06-05 21:46:41 +00:00
use Illuminate\Contracts\Auth\Authenticatable;
2015-12-13 04:42:28 +00:00
class SongController extends Controller
{
2023-06-05 21:46:41 +00:00
/** @param User $user */
2022-06-10 10:47:46 +00:00
public function __construct(
private SongService $songService,
2023-06-05 21:46:41 +00:00
private SongRepository $songRepository,
2022-06-10 10:47:46 +00:00
private AlbumRepository $albumRepository,
private ArtistRepository $artistRepository,
2023-06-05 21:46:41 +00:00
private LibraryManager $libraryManager,
private ?Authenticatable $user
2022-06-10 10:47:46 +00:00
) {
2018-08-18 13:19:40 +00:00
}
2023-06-05 21:46:41 +00:00
public function index(SongListRequest $request)
{
return SongResource::collection(
$this->songRepository->getForListing(
sortColumn: $request->sort ?: 'songs.title',
sortDirection: $request->order ?: 'asc',
ownSongOnly: (bool) $request->ownSongsOnly,
scopedUser: $this->user
2023-06-05 21:46:41 +00:00
)
);
}
public function show(Song $song)
{
2024-01-09 18:34:40 +00:00
$this->authorize('access', $song);
2024-01-10 23:11:45 +00:00
return SongResource::make($this->songRepository->getOne($song->id, $this->user));
2023-06-05 21:46:41 +00:00
}
2016-03-05 09:01:12 +00:00
public function update(SongUpdateRequest $request)
{
2024-01-09 18:34:40 +00:00
// Don't use SongRepository::findMany() because it'd be already catered to the current user.
2024-01-10 23:11:45 +00:00
Song::query()->findMany($request->songs)->each(fn (Song $song) => $this->authorize('edit', $song));
2024-01-06 22:28:31 +00:00
2022-06-10 10:47:46 +00:00
$updatedSongs = $this->songService->updateSongs($request->songs, SongUpdateData::fromRequest($request));
$albums = $this->albumRepository->getMany($updatedSongs->pluck('album_id')->toArray());
2022-06-10 10:47:46 +00:00
$artists = $this->artistRepository->getMany(
2022-06-10 10:47:46 +00:00
array_merge(
$updatedSongs->pluck('artist_id')->all(),
$updatedSongs->pluck('album_artist_id')->all()
)
);
2018-08-29 06:30:39 +00:00
return response()->json([
2022-06-10 10:47:46 +00:00
'songs' => SongResource::collection($updatedSongs),
'albums' => AlbumResource::collection($albums),
'artists' => ArtistResource::collection($artists),
'removed' => $this->libraryManager->prune(),
2018-08-29 06:30:39 +00:00
]);
2016-03-05 09:01:12 +00:00
}
2023-06-05 21:46:41 +00:00
public function destroy(DeleteSongsRequest $request)
{
2024-01-09 18:34:40 +00:00
// Don't use SongRepository::findMany() because it'd be already catered to the current user.
2024-01-10 21:37:24 +00:00
Song::query()->findMany($request->songs)->each(fn (Song $song) => $this->authorize('delete', $song));
2024-01-06 22:28:31 +00:00
2023-06-05 21:46:41 +00:00
$this->songService->deleteSongs($request->songs);
return response()->noContent();
}
2015-12-13 04:42:28 +00:00
}