Skip to content

Commit

Permalink
feat: add the ability to create API routes with multiple route parame…
Browse files Browse the repository at this point in the history
…ters

Merge pull request #271 from jonerickson/feature/multiple-controller-arguments
  • Loading branch information
alexzarbn authored Nov 4, 2024
2 parents 306fbff + 3fa175c commit 686c16c
Show file tree
Hide file tree
Showing 43 changed files with 915 additions and 89 deletions.
32 changes: 21 additions & 11 deletions src/Concerns/HandlesRelationManyToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ trait HandlesRelationManyToManyOperations
* Attach resource to the relation in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed>$args
* @return JsonResponse
*/
public function attach(Request $request, $parentKey)
public function attach(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->attachWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -237,11 +239,13 @@ protected function afterAttach(Request $request, Model $parentEntity, array &$at
* Detach resource to the relation in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return JsonResponse
*/
public function detach(Request $request, $parentKey)
public function detach(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->detachWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -360,11 +364,13 @@ protected function afterDetach(Request $request, Model $parentEntity, array &$de
* Sync relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return JsonResponse
*/
public function sync(Request $request, $parentKey)
public function sync(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->syncWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -486,11 +492,13 @@ protected function afterSync(Request $request, Model $parentEntity, array &$sync
* Toggle relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return JsonResponse
*/
public function toggle(Request $request, $parentKey)
public function toggle(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->toggleWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -603,12 +611,14 @@ protected function afterToggle(Request $request, Model $parentEntity, array &$to
* Update relation resource pivot in a transaction-safe wqy.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @param array<int, mixed> $args
* @return JsonResponse
*/
public function updatePivot(Request $request, $parentKey, $relatedKey)
public function updatePivot(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

try {
$this->startTransaction();
$result = $this->updatePivotWithTransaction($request, $parentKey, $relatedKey);
Expand Down
14 changes: 9 additions & 5 deletions src/Concerns/HandlesRelationOneToManyOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ trait HandlesRelationOneToManyOperations
* Associates resource with another resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return Resource
*/
public function associate(Request $request, $parentKey)
public function associate(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->associateWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -164,12 +166,14 @@ protected function afterAssociate(Request $request, Model $parentEntity, Model $
* Disassociates resource from another resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string $relatedKey
* @param array<int, mixed> $args
* @return Resource
*/
public function dissociate(Request $request, $parentKey, $relatedKey)
public function dissociate(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

try {
$this->startTransaction();
$result = $this->dissociateWithTransaction($request, $parentKey, $relatedKey);
Expand Down
24 changes: 16 additions & 8 deletions src/Concerns/HandlesRelationStandardBatchOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ trait HandlesRelationStandardBatchOperations
* Create a batch of new relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return CollectionResource
*/
public function batchStore(Request $request, $parentKey)
public function batchStore(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->batchStoreWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -161,11 +163,13 @@ protected function afterBatchStore(Request $request, Model $parentEntity, Collec
* Updates a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return CollectionResource
*/
public function batchUpdate(Request $request, $parentKey)
public function batchUpdate(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->batchUpdateWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -355,12 +359,14 @@ protected function afterBatchUpdate(Request $request, Model $parentEntity, Colle
* Deletes a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return CollectionResource
* @throws Exception
*/
public function batchDestroy(Request $request, $parentKey)
public function batchDestroy(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->batchDestroyWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -527,12 +533,14 @@ protected function afterBatchDestroy(Request $request, Model $parentEntity, Coll
* Restores a batch of relation resources in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return CollectionResource
* @throws Exception
*/
public function batchRestore(Request $request, $parentKey)
public function batchRestore(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->batchRestoreWithTransaction($request, $parentKey);
Expand Down
41 changes: 28 additions & 13 deletions src/Concerns/HandlesRelationStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ trait HandlesRelationStandardOperations
* Fetch the list of relation resources.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return CollectionResource
*/
public function index(Request $request, $parentKey)
public function index(Request $request, ...$args)
{
$requestedRelations = $this->relationsResolver->requestedRelations($request);

$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

$parentQuery = $this->buildIndexParentFetchQuery($request, $parentKey);
$parentEntity = $this->runIndexParentFetchQuery($request, $parentQuery, $parentKey);

Expand Down Expand Up @@ -255,11 +257,13 @@ public function search(Request $request, $parentKey)
* Create new relation resource.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @return Resource
*/
public function store(Request $request, $parentKey)
public function store(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);

try {
$this->startTransaction();
$result = $this->storeWithTransaction($request, $parentKey);
Expand Down Expand Up @@ -484,12 +488,14 @@ protected function afterStore(Request $request, Model $parentEntity, Model $enti
* Fetch a relation resource.
*
* @param Request $request
* @param int|string $parentKey
* @param int|string|null $relatedKey
* @param array<int, mixed> $args
* @return Resource
*/
public function show(Request $request, $parentKey, $relatedKey = null)
public function show(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

$parentQuery = $this->buildShowParentFetchQuery($request, $parentKey);
$parentEntity = $this->runShowParentFetchQuery($request, $parentQuery, $parentKey);

Expand Down Expand Up @@ -672,12 +678,15 @@ protected function afterShow(Request $request, Model $parentEntity, Model $entit
* Update a relation resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @param int|string|null $relatedKey
* @return Resource
*/
public function update(Request $request, $parentKey, $relatedKey = null)
public function update(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

try {
$this->startTransaction();
$result = $this->updateWithTransaction($request, $parentKey, $relatedKey);
Expand Down Expand Up @@ -889,13 +898,16 @@ protected function afterUpdate(Request $request, Model $parentEntity, Model $ent
* Delete a relation resource.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @param int|string|null $relatedKey
* @return Resource
* @throws Exception
*/
public function destroy(Request $request, $parentKey, $relatedKey = null)
public function destroy(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

try {
$this->startTransaction();
$result = $this->destroyWithTransaction($request, $parentKey, $relatedKey);
Expand Down Expand Up @@ -1079,12 +1091,15 @@ protected function afterDestroy(Request $request, Model $parentEntity, Model $en
* Restores a previously deleted relation resource in a transaction-save way.
*
* @param Request $request
* @param int|string $parentKey
* @param array<int, mixed> $args
* @param int|string|null $relatedKey
* @return Resource
*/
public function restore(Request $request, $parentKey, $relatedKey = null)
public function restore(Request $request, ...$args)
{
$parentKey = $this->keyResolver->resolveRelationOperationParentKey($request, $args);
$relatedKey = $this->keyResolver->resolveRelationOperationRelatedKey($request, $args);

try {
$this->startTransaction();
$result = $this->restoreWithTransaction($request, $parentKey, $relatedKey);
Expand Down
24 changes: 16 additions & 8 deletions src/Concerns/HandlesStandardOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,19 @@ protected function afterStore(Request $request, Model $entity)
* Fetches resource.
*
* @param Request $request
* @param int|string $key
* @param array<int, mixed> $args
* @return Resource
* @throws AuthorizationException
* @throws BindingResolutionException
*/
public function show(Request $request, $key)
public function show(Request $request, ...$args)
{
$requestedRelations = $this->relationsResolver->requestedRelations($request);

$query = $this->buildShowFetchQuery($request, $requestedRelations);

$key = $this->keyResolver->resolveStandardOperationKey($request, $args);

$beforeHookResult = $this->beforeShow($request, $key);
if ($this->hookResponds($beforeHookResult)) {
return $beforeHookResult;
Expand Down Expand Up @@ -438,11 +440,13 @@ protected function afterShow(Request $request, Model $entity)
* Update a resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $key
* @param array<int, mixed> $args
* @return Resource
*/
public function update(Request $request, $key)
public function update(Request $request, ...$args)
{
$key = $this->keyResolver->resolveStandardOperationKey($request, $args);

try {
$this->startTransaction();
$result = $this->updateWithTransaction($request, $key);
Expand Down Expand Up @@ -602,12 +606,14 @@ protected function afterUpdate(Request $request, Model $entity)
* Deletes a resource.
*
* @param Request $request
* @param int|string $key
* @param array<int, mixed> $args
* @return Resource
* @throws Exception
*/
public function destroy(Request $request, $key)
public function destroy(Request $request, ...$args)
{
$key = $this->keyResolver->resolveStandardOperationKey($request, $args);

try {
$this->startTransaction();
$result = $this->destroyWithTransaction($request, $key);
Expand Down Expand Up @@ -767,12 +773,14 @@ protected function afterDestroy(Request $request, Model $entity)
* Restore previously deleted resource in a transaction-safe way.
*
* @param Request $request
* @param int|string $key
* @param array<int, mixed> $args
* @return Resource
* @throws Exception
*/
public function restore(Request $request, $key)
public function restore(Request $request, ...$args)
{
$key = $this->keyResolver->resolveStandardOperationKey($request, $args);

try {
$this->startTransaction();
$result = $this->restoreWithTransaction($request, $key);
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/KeyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Orion\Contracts;

use Illuminate\Http\Request;

interface KeyResolver
{
public function resolveStandardOperationKey(Request $request, array $args);

public function resolveRelationOperationParentKey(Request $request, array $args);

public function resolveRelationOperationRelatedKey(Request $request, array $args);
}
23 changes: 23 additions & 0 deletions src/Drivers/Standard/KeyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Orion\Drivers\Standard;

use Illuminate\Http\Request;

class KeyResolver implements \Orion\Contracts\KeyResolver
{
public function resolveStandardOperationKey(Request $request, array $args)
{
return $args[0];
}

public function resolveRelationOperationParentKey(Request $request, array $args)
{
return $args[0];
}

public function resolveRelationOperationRelatedKey(Request $request, array $args)
{
return $args[1] ?? null;
}
}
Loading

0 comments on commit 686c16c

Please sign in to comment.