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

50 lines
1.6 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;
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;
use App\Models\User;
2018-08-29 06:30:39 +00:00
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
2022-06-10 10:47:46 +00:00
use App\Services\LibraryManager;
use App\Services\SongService;
use App\Values\SongUpdateData;
use Illuminate\Contracts\Auth\Authenticatable;
2015-12-13 04:42:28 +00:00
class SongController extends Controller
{
2022-06-10 10:47:46 +00:00
/** @param User $user */
public function __construct(
private SongService $songService,
private AlbumRepository $albumRepository,
private ArtistRepository $artistRepository,
private LibraryManager $libraryManager,
private ?Authenticatable $user
) {
2018-08-18 13:19:40 +00:00
}
2016-03-05 09:01:12 +00:00
public function update(SongUpdateRequest $request)
{
2022-06-10 10:47:46 +00:00
$updatedSongs = $this->songService->updateSongs($request->songs, SongUpdateData::fromRequest($request));
$albums = $this->albumRepository->getByIds($updatedSongs->pluck('album_id')->toArray(), $this->user);
$artists = $this->artistRepository->getByIds(
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
}
2015-12-13 04:42:28 +00:00
}