mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
48 lines
1.0 KiB
PHP
48 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Models\List\Playlist;
|
|
|
|
use App\Models\List\Playlist;
|
|
use App\Models\List\Playlist\PlaylistTrack;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class InsertTrackAction
|
|
{
|
|
/**
|
|
* Append track to playlist.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function insert(Playlist $playlist, PlaylistTrack $track): void
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($playlist->first()->doesntExist()) {
|
|
$playlist->first()->associate($track)->save();
|
|
}
|
|
|
|
$last = $playlist->last;
|
|
|
|
$playlist->last()->associate($track)->save();
|
|
|
|
$last?->next()?->associate($track)?->save();
|
|
$track->previous()->associate($last);
|
|
|
|
$track->next()->disassociate()->save();
|
|
|
|
DB::commit();
|
|
} catch (Exception $e) {
|
|
Log::error($e->getMessage());
|
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|