Skip to content

Commit 7d0d8e0

Browse files
authored
Merge pull request #130 from horstoeko/transactions
feat: transactions
2 parents 152db2e + f1fc2ef commit 7d0d8e0

9 files changed

+560
-32
lines changed

config/orion.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@
2828
['url' => env('APP_URL').'/api', 'description' => 'Default Environment'],
2929
],
3030
],
31+
'transactions' => [
32+
'enabled' => false,
33+
],
3134
];

src/Concerns/HandlesRelationManyToManyOperations.php

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,32 @@
1313
trait HandlesRelationManyToManyOperations
1414
{
1515
/**
16-
* Attach resource to the relation.
16+
* Attach resource to the relation in a transaction-safe way.
1717
*
1818
* @param Request $request
1919
* @param int|string $parentKey
2020
* @return JsonResponse
2121
*/
2222
public function attach(Request $request, $parentKey)
23+
{
24+
try {
25+
$this->startTransaction();
26+
$result = $this->attachWithTransaction($request, $parentKey);
27+
$this->commitTransaction();
28+
return $result;
29+
} catch (\Exception $exception) {
30+
$this->rollbackTransactionAndRaise($exception);
31+
}
32+
}
33+
34+
/**
35+
* Attach resource to the relation.
36+
*
37+
* @param Request $request
38+
* @param int|string $parentKey
39+
* @return JsonResponse
40+
*/
41+
protected function attachWithTransaction(Request $request, $parentKey)
2342
{
2443
$parentQuery = $this->buildAttachParentFetchQuery($request, $parentKey);
2544
$parentEntity = $this->runAttachParentFetchQuery($request, $parentQuery, $parentKey);
@@ -215,13 +234,32 @@ protected function afterAttach(Request $request, Model $parentEntity, array &$at
215234
}
216235

217236
/**
218-
* Detach resource to the relation.
237+
* Detach resource to the relation in a transaction-safe way.
219238
*
220239
* @param Request $request
221240
* @param int|string $parentKey
222241
* @return JsonResponse
223242
*/
224243
public function detach(Request $request, $parentKey)
244+
{
245+
try {
246+
$this->startTransaction();
247+
$result = $this->detachWithTransaction($request, $parentKey);
248+
$this->commitTransaction();
249+
return $result;
250+
} catch (\Exception $exception) {
251+
$this->rollbackTransactionAndRaise($exception);
252+
}
253+
}
254+
255+
/**
256+
* Detach resource to the relation.
257+
*
258+
* @param Request $request
259+
* @param int|string $parentKey
260+
* @return JsonResponse
261+
*/
262+
protected function detachWithTransaction(Request $request, $parentKey)
225263
{
226264
$parentQuery = $this->buildDetachParentFetchQuery($request, $parentKey);
227265
$parentEntity = $this->runDetachParentFetchQuery($request, $parentQuery, $parentKey);
@@ -315,13 +353,32 @@ protected function afterDetach(Request $request, Model $parentEntity, array &$de
315353
}
316354

317355
/**
318-
* Sync relation resources.
356+
* Sync relation resources in a transaction-safe way.
319357
*
320358
* @param Request $request
321359
* @param int|string $parentKey
322360
* @return JsonResponse
323361
*/
324362
public function sync(Request $request, $parentKey)
363+
{
364+
try {
365+
$this->startTransaction();
366+
$result = $this->syncWithTransaction($request, $parentKey);
367+
$this->commitTransaction();
368+
return $result;
369+
} catch (\Exception $exception) {
370+
$this->rollbackTransactionAndRaise($exception);
371+
}
372+
}
373+
374+
/**
375+
* Sync relation resources.
376+
*
377+
* @param Request $request
378+
* @param int|string $parentKey
379+
* @return JsonResponse
380+
*/
381+
protected function syncWithTransaction(Request $request, $parentKey)
325382
{
326383
$parentQuery = $this->buildSyncParentFetchQuery($request, $parentKey);
327384
$parentEntity = $this->runSyncParentFetchQuery($request, $parentQuery, $parentKey);
@@ -422,13 +479,32 @@ protected function afterSync(Request $request, Model $parentEntity, array &$sync
422479
}
423480

424481
/**
425-
* Toggle relation resources.
482+
* Toggle relation resources in a transaction-safe way.
426483
*
427484
* @param Request $request
428485
* @param int|string $parentKey
429486
* @return JsonResponse
430487
*/
431488
public function toggle(Request $request, $parentKey)
489+
{
490+
try {
491+
$this->startTransaction();
492+
$result = $this->toggleWithTransaction($request, $parentKey);
493+
$this->commitTransaction();
494+
return $result;
495+
} catch (\Exception $exception) {
496+
$this->rollbackTransactionAndRaise($exception);
497+
}
498+
}
499+
500+
/**
501+
* Toggle relation resources.
502+
*
503+
* @param Request $request
504+
* @param int|string $parentKey
505+
* @return JsonResponse
506+
*/
507+
protected function toggleWithTransaction(Request $request, $parentKey)
432508
{
433509
$parentQuery = $this->buildToggleParentFetchQuery($request, $parentKey);
434510
$parentEntity = $this->runToggleParentFetchQuery($request, $parentQuery, $parentKey);
@@ -516,14 +592,34 @@ protected function afterToggle(Request $request, Model $parentEntity, array &$to
516592
}
517593

518594
/**
519-
* Update relation resource pivot.
595+
* Update relation resource pivot in a transaction-safe wqy.
520596
*
521597
* @param Request $request
522598
* @param int|string $parentKey
523599
* @param int|string $relatedKey
524600
* @return JsonResponse
525601
*/
526602
public function updatePivot(Request $request, $parentKey, $relatedKey)
603+
{
604+
try {
605+
$this->startTransaction();
606+
$result = $this->updatePivotWithTransaction($request, $parentKey, $relatedKey);
607+
$this->commitTransaction();
608+
return $result;
609+
} catch (\Exception $exception) {
610+
$this->rollbackTransactionAndRaise($exception);
611+
}
612+
}
613+
614+
/**
615+
* Update relation resource pivot.
616+
*
617+
* @param Request $request
618+
* @param int|string $parentKey
619+
* @param int|string $relatedKey
620+
* @return JsonResponse
621+
*/
622+
protected function updatePivotWithTransaction(Request $request, $parentKey, $relatedKey)
527623
{
528624
$parentQuery = $this->buildUpdatePivotParentFetchQuery($request, $parentKey);
529625
$parentEntity = $this->runUpdatePivotParentFetchQuery($request, $parentQuery, $parentKey);

src/Concerns/HandlesRelationOneToManyOperations.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@
1111
trait HandlesRelationOneToManyOperations
1212
{
1313
/**
14-
* Associates resource with another resource.
14+
* Associates resource with another resource in a transaction-safe way.
1515
*
1616
* @param Request $request
1717
* @param int|string $parentKey
1818
* @return Resource
1919
*/
2020
public function associate(Request $request, $parentKey)
21+
{
22+
try {
23+
$this->startTransaction();
24+
$result = $this->associateWithTransaction($request, $parentKey);
25+
$this->commitTransaction();
26+
return $result;
27+
} catch (\Exception $exception) {
28+
$this->rollbackTransactionAndRaise($exception);
29+
}
30+
}
31+
32+
/**
33+
* Associates resource with another resource.
34+
*
35+
* @param Request $request
36+
* @param int|string $parentKey
37+
* @return Resource
38+
*/
39+
protected function associateWithTransaction(Request $request, $parentKey)
2140
{
2241
$parentQuery = $this->buildAssociateParentFetchQuery($request, $parentKey);
2342
$parentEntity = $this->runAssociateParentFetchQuery($request, $parentQuery, $parentKey);
@@ -143,14 +162,34 @@ protected function afterAssociate(Request $request, Model $parentEntity, Model $
143162
}
144163

145164
/**
146-
* Disassociates resource from another resource.
165+
* Disassociates resource from another resource in a transaction-safe way.
147166
*
148167
* @param Request $request
149168
* @param int|string $parentKey
150169
* @param int|string $relatedKey
151170
* @return Resource
152171
*/
153172
public function dissociate(Request $request, $parentKey, $relatedKey)
173+
{
174+
try {
175+
$this->startTransaction();
176+
$result = $this->dissociateWithTransaction($request, $parentKey, $relatedKey);
177+
$this->commitTransaction();
178+
return $result;
179+
} catch (\Exception $exception) {
180+
$this->rollbackTransactionAndRaise($exception);
181+
}
182+
}
183+
184+
/**
185+
* Disassociates resource from another resource.
186+
*
187+
* @param Request $request
188+
* @param int|string $parentKey
189+
* @param int|string $relatedKey
190+
* @return Resource
191+
*/
192+
protected function dissociateWithTransaction(Request $request, $parentKey, $relatedKey)
154193
{
155194
$parentQuery = $this->buildDissociateParentFetchQuery($request, $parentKey);
156195
$parentEntity = $this->runDissociateParentFetchQuery($request, $parentQuery, $parentKey);

src/Concerns/HandlesRelationStandardBatchOperations.php

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,32 @@
1414
trait HandlesRelationStandardBatchOperations
1515
{
1616
/**
17-
* Create a batch of new relation resources.
17+
* Create a batch of new relation resources in a transaction-safe way.
1818
*
1919
* @param Request $request
2020
* @param int|string $parentKey
2121
* @return CollectionResource
2222
*/
2323
public function batchStore(Request $request, $parentKey)
24+
{
25+
try {
26+
$this->startTransaction();
27+
$result = $this->batchStoreWithTransaction($request, $parentKey);
28+
$this->commitTransaction();
29+
return $result;
30+
} catch (\Exception $exception) {
31+
$this->rollbackTransactionAndRaise($exception);
32+
}
33+
}
34+
35+
/**
36+
* Create a batch of new relation resources.
37+
*
38+
* @param Request $request
39+
* @param int|string $parentKey
40+
* @return CollectionResource
41+
*/
42+
protected function batchStoreWithTransaction(Request $request, $parentKey)
2443
{
2544
$resourceModelClass = $this->resolveResourceModelClass();
2645

@@ -133,13 +152,32 @@ protected function afterBatchStore(Request $request, Model $parentEntity, Collec
133152
}
134153

135154
/**
136-
* Updates a batch of relation resources.
155+
* Updates a batch of relation resources in a transaction-safe way.
137156
*
138157
* @param Request $request
139158
* @param int|string $parentKey
140159
* @return CollectionResource
141160
*/
142161
public function batchUpdate(Request $request, $parentKey)
162+
{
163+
try {
164+
$this->startTransaction();
165+
$result = $this->batchUpdateWithTransaction($request, $parentKey);
166+
$this->commitTransaction();
167+
return $result;
168+
} catch (\Exception $exception) {
169+
$this->rollbackTransactionAndRaise($exception);
170+
}
171+
}
172+
173+
/**
174+
* Updates a batch of relation resources.
175+
*
176+
* @param Request $request
177+
* @param int|string $parentKey
178+
* @return CollectionResource
179+
*/
180+
protected function batchUpdateWithTransaction(Request $request, $parentKey)
143181
{
144182
$parentQuery = $this->buildBatchUpdateParentFetchQuery($request, $parentKey);
145183
$parentEntity = $this->runBatchUpdateParentFetchQuery($request, $parentQuery, $parentKey);
@@ -309,14 +347,34 @@ protected function afterBatchUpdate(Request $request, Model $parentEntity, Colle
309347
}
310348

311349
/**
312-
* Deletes a batch of relation resources.
350+
* Deletes a batch of relation resources in a transaction-safe way.
313351
*
314352
* @param Request $request
315353
* @param int|string $parentKey
316354
* @return CollectionResource
317355
* @throws Exception
318356
*/
319357
public function batchDestroy(Request $request, $parentKey)
358+
{
359+
try {
360+
$this->startTransaction();
361+
$result = $this->batchDestroyWithTransaction($request, $parentKey);
362+
$this->commitTransaction();
363+
return $result;
364+
} catch (\Exception $exception) {
365+
$this->rollbackTransactionAndRaise($exception);
366+
}
367+
}
368+
369+
/**
370+
* Deletes a batch of relation resources.
371+
*
372+
* @param Request $request
373+
* @param int|string $parentKey
374+
* @return CollectionResource
375+
* @throws Exception
376+
*/
377+
protected function batchDestroyWithTransaction(Request $request, $parentKey)
320378
{
321379
$parentQuery = $this->buildBatchDestroyParentFetchQuery($request, $parentKey);
322380
$parentEntity = $this->runBatchDestroyParentFetchQuery($request, $parentQuery, $parentKey);
@@ -458,14 +516,34 @@ protected function afterBatchDestroy(Request $request, Model $parentEntity, Coll
458516
}
459517

460518
/**
461-
* Restores a batch of relation resources.
519+
* Restores a batch of relation resources in a transaction-safe way.
462520
*
463521
* @param Request $request
464522
* @param int|string $parentKey
465523
* @return CollectionResource
466524
* @throws Exception
467525
*/
468526
public function batchRestore(Request $request, $parentKey)
527+
{
528+
try {
529+
$this->startTransaction();
530+
$result = $this->batchRestoreWithTransaction($request, $parentKey);
531+
$this->commitTransaction();
532+
return $result;
533+
} catch (\Exception $exception) {
534+
$this->rollbackTransactionAndRaise($exception);
535+
}
536+
}
537+
538+
/**
539+
* Restores a batch of relation resources.
540+
*
541+
* @param Request $request
542+
* @param int|string $parentKey
543+
* @return CollectionResource
544+
* @throws Exception
545+
*/
546+
protected function batchRestoreWithTransaction(Request $request, $parentKey)
469547
{
470548
$parentQuery = $this->buildBatchRestoreParentFetchQuery($request, $parentKey);
471549
$parentEntity = $this->runBatchRestoreParentFetchQuery($request, $parentQuery, $parentKey);

0 commit comments

Comments
 (0)