mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-11-14 10:08:49 +01:00
fix(api): use nested transactions for store track endpoint (#551)
This commit is contained in:
1 parent
3d633be59d
commit
a7b74acaa0
2 files changed
+96
-57
No files matched your search
@@ -14,6 +14,7 @@ use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
@@ -42,49 +43,65 @@ class StoreTrackAction
|
||||
|
||||
Log::debug('Track Parameters', $trackParameters);
|
||||
|
||||
$storeAction = new StoreAction();
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
/** @var PlaylistTrack $track */
|
||||
$track = $storeAction->store($builder, $trackParameters);
|
||||
Log::debug('Begin Store Transaction');
|
||||
|
||||
if (! empty($nextId) && empty($previousId)) {
|
||||
/** @var PlaylistTrack $next */
|
||||
$next = PlaylistTrack::query()
|
||||
->with(PlaylistTrack::RELATION_PREVIOUS)
|
||||
->findOrFail($nextId);
|
||||
$storeAction = new StoreAction();
|
||||
|
||||
Log::debug('Next Track', $next->toArray());
|
||||
/** @var PlaylistTrack $track */
|
||||
$track = $storeAction->store($builder, $trackParameters);
|
||||
|
||||
$insertAction = new InsertTrackBeforeAction();
|
||||
if (! empty($nextId) && empty($previousId)) {
|
||||
/** @var PlaylistTrack $next */
|
||||
$next = PlaylistTrack::query()
|
||||
->with(PlaylistTrack::RELATION_PREVIOUS)
|
||||
->findOrFail($nextId);
|
||||
|
||||
$insertAction->insertBefore($playlist, $track, $next);
|
||||
Log::debug('Next Track', $next->toArray());
|
||||
|
||||
Log::debug('Insert Before Completed');
|
||||
$insertAction = new InsertTrackBeforeAction();
|
||||
|
||||
$insertAction->insertBefore($playlist, $track, $next);
|
||||
|
||||
Log::debug('Insert Before Completed');
|
||||
}
|
||||
|
||||
if (! empty($previousId) && empty($nextId)) {
|
||||
/** @var PlaylistTrack $previous */
|
||||
$previous = PlaylistTrack::query()
|
||||
->with(PlaylistTrack::RELATION_NEXT)
|
||||
->findOrFail($previousId);
|
||||
|
||||
Log::debug('Previous Track', $previous->toArray());
|
||||
|
||||
$insertAction = new InsertTrackAfterAction();
|
||||
|
||||
$insertAction->insertAfter($playlist, $track, $previous);
|
||||
|
||||
Log::debug('Insert After Completed');
|
||||
}
|
||||
|
||||
if (empty($nextId) && empty($previousId)) {
|
||||
$insertAction = new InsertTrackAction();
|
||||
|
||||
$insertAction->insert($playlist, $track);
|
||||
|
||||
Log::debug('Insert Completed');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
Log::debug('End Store Transaction');
|
||||
|
||||
return $storeAction->cleanup($track);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
DB::rollBack();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if (! empty($previousId) && empty($nextId)) {
|
||||
/** @var PlaylistTrack $previous */
|
||||
$previous = PlaylistTrack::query()
|
||||
->with(PlaylistTrack::RELATION_NEXT)
|
||||
->findOrFail($previousId);
|
||||
|
||||
Log::debug('Previous Track', $previous->toArray());
|
||||
|
||||
$insertAction = new InsertTrackAfterAction();
|
||||
|
||||
$insertAction->insertAfter($playlist, $track, $previous);
|
||||
|
||||
Log::debug('Insert After Completed');
|
||||
}
|
||||
|
||||
if (empty($nextId) && empty($previousId)) {
|
||||
$insertAction = new InsertTrackAction();
|
||||
|
||||
$insertAction->insert($playlist, $track);
|
||||
|
||||
Log::debug('Insert Completed');
|
||||
}
|
||||
|
||||
return $storeAction->cleanup($track);
|
||||
}
|
||||
}
|
||||
@@ -29,23 +29,59 @@ class InsertTrackAction
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
Log::debug('Begin Transaction');
|
||||
Log::debug('Begin Playlist Transaction');
|
||||
|
||||
if ($playlist->first()->doesntExist()) {
|
||||
$playlist->first()->associate($track);
|
||||
}
|
||||
|
||||
Log::debug('First Playlist Track');
|
||||
Log::debug('First Playlist Track Set');
|
||||
|
||||
$this->insertTrack($playlist, $track);
|
||||
|
||||
Log::debug('Insert Track Completed');
|
||||
|
||||
$playlist->last()->associate($track)->save();
|
||||
|
||||
Log::debug('First Playlist Track Set');
|
||||
|
||||
DB::commit();
|
||||
|
||||
Log::debug('End Playlist Transaction');
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
DB::rollBack();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Track Relation with nested transaction.
|
||||
*
|
||||
* @param Playlist $playlist
|
||||
* @param PlaylistTrack $track
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function insertTrack(Playlist $playlist, PlaylistTrack $track): void
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
Log::debug('Begin Track Transaction');
|
||||
|
||||
$last = $playlist->last;
|
||||
$last?->next()?->associate($track)?->save();
|
||||
$track->previous()->associate($last);
|
||||
|
||||
Log::debug('Track Last Relation');
|
||||
Log::debug('Set Previous Track Relation');
|
||||
|
||||
$track->next()->disassociate();
|
||||
|
||||
Log::debug('Track Next Relation');
|
||||
Log::debug('Set Next Track Relation');
|
||||
|
||||
if ($track->isDirty()) {
|
||||
$track->save();
|
||||
@@ -53,23 +89,9 @@ class InsertTrackAction
|
||||
|
||||
Log::debug('Track Saved');
|
||||
|
||||
$playlist->last()->dissociate();
|
||||
|
||||
Log::debug('Dissociate last track');
|
||||
|
||||
$lastBuilder = $playlist->last();
|
||||
|
||||
Log::debug('Last Track builder');
|
||||
|
||||
$modifiedPlaylist = $lastBuilder->associate($track);
|
||||
|
||||
Log::debug('Associate last track');
|
||||
|
||||
$modifiedPlaylist->save();
|
||||
|
||||
Log::debug('Save Playlist');
|
||||
|
||||
DB::commit();
|
||||
|
||||
Log::debug('End Track Transaction');
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
|
||||
Reference in new issue
Block a user