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(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly SongService $songService,
|
|
|
|
private readonly SongRepository $songRepository,
|
|
|
|
private readonly AlbumRepository $albumRepository,
|
|
|
|
private readonly ArtistRepository $artistRepository,
|
|
|
|
private readonly LibraryManager $libraryManager,
|
|
|
|
private readonly ?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(
|
2024-01-11 22:14:22 +00:00
|
|
|
sortColumn: $request->sort ?: 'songs.title',
|
|
|
|
sortDirection: $request->order ?: 'asc',
|
2024-01-15 22:26:50 +00:00
|
|
|
ownSongsOnly: $request->boolean('own_songs_only'),
|
2024-01-11 22:14:22 +00:00
|
|
|
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));
|
2024-01-01 20:38:41 +00:00
|
|
|
$albums = $this->albumRepository->getMany($updatedSongs->pluck('album_id')->toArray());
|
2022-06-10 10:47:46 +00:00
|
|
|
|
2024-01-01 20:38:41 +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
|
|
|
}
|