Skip to content

Commit

Permalink
Merge pull request #130 from horstoeko/transactions
Browse files Browse the repository at this point in the history
feat: transactions
  • Loading branch information
alexzarbn authored Oct 28, 2021
2 parents 152db2e + f1fc2ef commit 7d0d8e0
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 32 deletions.
3 changes: 3 additions & 0 deletions config/orion.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
['url' => env('APP_URL').'/api', 'description' => 'Default Environment'],
],
],
'transactions' => [
'enabled' => false,
],
];
106 changes: 101 additions & 5 deletions src/Concerns/HandlesRelationManyToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@
trait HandlesRelationManyToManyOperations
{
/**
* Attach resource to the relation.
* Attach resource to the relation in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
public function attach(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->attachWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Attach resource to the relation.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
protected function attachWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildAttachParentFetchQuery($request, $parentKey);
$parentEntity = $this->runAttachParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -215,13 +234,32 @@ protected function afterAttach(Request $request, Model $parentEntity, array &$at
}

/**
* Detach resource to the relation.
* Detach resource to the relation in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
public function detach(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->detachWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Detach resource to the relation.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
protected function detachWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildDetachParentFetchQuery($request, $parentKey);
$parentEntity = $this->runDetachParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -315,13 +353,32 @@ protected function afterDetach(Request $request, Model $parentEntity, array &$de
}

/**
* Sync relation resources.
* Sync relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
public function sync(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->syncWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Sync relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
protected function syncWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildSyncParentFetchQuery($request, $parentKey);
$parentEntity = $this->runSyncParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -422,13 +479,32 @@ protected function afterSync(Request $request, Model $parentEntity, array &$sync
}

/**
* Toggle relation resources.
* Toggle relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
public function toggle(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->toggleWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Toggle relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return JsonResponse
*/
protected function toggleWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildToggleParentFetchQuery($request, $parentKey);
$parentEntity = $this->runToggleParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -516,14 +592,34 @@ protected function afterToggle(Request $request, Model $parentEntity, array &$to
}

/**
* Update relation resource pivot.
* Update relation resource pivot in a transaction-safe wqy.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @return JsonResponse
*/
public function updatePivot(Request $request, $parentKey, $relatedKey)
{
try {
$this->startTransaction();
$result = $this->updatePivotWithTransaction($request, $parentKey, $relatedKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Update relation resource pivot.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @return JsonResponse
*/
protected function updatePivotWithTransaction(Request $request, $parentKey, $relatedKey)
{
$parentQuery = $this->buildUpdatePivotParentFetchQuery($request, $parentKey);
$parentEntity = $this->runUpdatePivotParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down
43 changes: 41 additions & 2 deletions src/Concerns/HandlesRelationOneToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@
trait HandlesRelationOneToManyOperations
{
/**
* Associates resource with another resource.
* Associates resource with another resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return Resource
*/
public function associate(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->associateWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Associates resource with another resource.
*
* @param Request $request
* @param int|string $parentKey
* @return Resource
*/
protected function associateWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildAssociateParentFetchQuery($request, $parentKey);
$parentEntity = $this->runAssociateParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -143,14 +162,34 @@ protected function afterAssociate(Request $request, Model $parentEntity, Model $
}

/**
* Disassociates resource from another resource.
* Disassociates resource from another resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @return Resource
*/
public function dissociate(Request $request, $parentKey, $relatedKey)
{
try {
$this->startTransaction();
$result = $this->dissociateWithTransaction($request, $parentKey, $relatedKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Disassociates resource from another resource.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @return Resource
*/
protected function dissociateWithTransaction(Request $request, $parentKey, $relatedKey)
{
$parentQuery = $this->buildDissociateParentFetchQuery($request, $parentKey);
$parentEntity = $this->runDissociateParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down
86 changes: 82 additions & 4 deletions src/Concerns/HandlesRelationStandardBatchOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@
trait HandlesRelationStandardBatchOperations
{
/**
* Create a batch of new relation resources.
* Create a batch of new relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
*/
public function batchStore(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->batchStoreWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Create a batch of new relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
*/
protected function batchStoreWithTransaction(Request $request, $parentKey)
{
$resourceModelClass = $this->resolveResourceModelClass();

Expand Down Expand Up @@ -133,13 +152,32 @@ protected function afterBatchStore(Request $request, Model $parentEntity, Collec
}

/**
* Updates a batch of relation resources.
* Updates a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
*/
public function batchUpdate(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->batchUpdateWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Updates a batch of relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
*/
protected function batchUpdateWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildBatchUpdateParentFetchQuery($request, $parentKey);
$parentEntity = $this->runBatchUpdateParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -309,14 +347,34 @@ protected function afterBatchUpdate(Request $request, Model $parentEntity, Colle
}

/**
* Deletes a batch of relation resources.
* Deletes a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
* @throws Exception
*/
public function batchDestroy(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->batchDestroyWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Deletes a batch of relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
* @throws Exception
*/
protected function batchDestroyWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildBatchDestroyParentFetchQuery($request, $parentKey);
$parentEntity = $this->runBatchDestroyParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down Expand Up @@ -458,14 +516,34 @@ protected function afterBatchDestroy(Request $request, Model $parentEntity, Coll
}

/**
* Restores a batch of relation resources.
* Restores a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
* @throws Exception
*/
public function batchRestore(Request $request, $parentKey)
{
try {
$this->startTransaction();
$result = $this->batchRestoreWithTransaction($request, $parentKey);
$this->commitTransaction();
return $result;
} catch (\Exception $exception) {
$this->rollbackTransactionAndRaise($exception);
}
}

/**
* Restores a batch of relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @return CollectionResource
* @throws Exception
*/
protected function batchRestoreWithTransaction(Request $request, $parentKey)
{
$parentQuery = $this->buildBatchRestoreParentFetchQuery($request, $parentKey);
$parentEntity = $this->runBatchRestoreParentFetchQuery($request, $parentQuery, $parentKey);
Expand Down
Loading

0 comments on commit 7d0d8e0

Please sign in to comment.