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

82 lines
2.5 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(
$request->sort ?: 'songs.title',
$request->order ?: 'asc',
$this->user
)
);
}
public function show(Song $song)
{
return SongResource::make($this->songRepository->getOne($song->id));
}
2016-03-05 09:01:12 +00:00
public function update(SongUpdateRequest $request)
{
2023-08-20 22:35:58 +00:00
$this->authorize('admin', $this->user);
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)
{
$this->authorize('admin', $this->user);
$this->songService->deleteSongs($request->songs);
return response()->noContent();
}
2015-12-13 04:42:28 +00:00
}