mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
46 lines
1.1 KiB
PHP
46 lines
1.1 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 InsertTrackAfterAction
|
|
{
|
|
/**
|
|
* Insert track after previous track.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function insertAfter(Playlist $playlist, PlaylistTrack $track, PlaylistTrack $previous): void
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($playlist->last()->is($previous)) {
|
|
$playlist->last()->associate($track)->save();
|
|
}
|
|
|
|
$next = $previous->next;
|
|
$next?->previous()?->associate($track)?->save();
|
|
$track->next()->associate($next);
|
|
|
|
$previous->next()->associate($track)->save();
|
|
$track->previous()->associate($previous)->save();
|
|
|
|
DB::commit();
|
|
} catch (Exception $e) {
|
|
Log::error($e->getMessage());
|
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|