updated returnAsResource parameters
This commit is contained in:
@@ -122,19 +122,18 @@ class ApiController extends Controller
|
||||
* Return resource data
|
||||
*
|
||||
* @param array|Model|Collection $data Resource data.
|
||||
* @param boolean $isCollection If the data is a group of items.
|
||||
* @param array|null $appendData Data to append to response.
|
||||
* @param string|null $resourceName The resource name to group the result.
|
||||
* @param integer $respondCode Resource code.
|
||||
* @param array $options Respond options.
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function respondAsResource(
|
||||
mixed $data,
|
||||
bool $isCollection = false,
|
||||
mixed $appendData = null,
|
||||
string $resourceName = null,
|
||||
int $respondCode = HttpResponseCodes::HTTP_OK
|
||||
array $options = [],
|
||||
) {
|
||||
$isCollection = $options['isCollection'] ?? false;
|
||||
$appendData = $options['appendData'] ?? null;
|
||||
$resourceName = $options['resourceName'] ?? null;
|
||||
$respondCode = $options['respondCode'] ?? HttpResponseCodes::HTTP_OK;
|
||||
|
||||
if ($data === null || ($data instanceof Collection && $data->count() === 0)) {
|
||||
return $this->respondNotFound();
|
||||
}
|
||||
|
||||
@@ -73,8 +73,7 @@ class AuthController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$user->makeVisible(['permissions']),
|
||||
false,
|
||||
['token' => $token]
|
||||
['appendData' => ['token' => $token]]
|
||||
);
|
||||
}//end if
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ class EventController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$collection,
|
||||
true,
|
||||
['total' => $total]
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -64,9 +64,7 @@ class EventController extends ApiController
|
||||
$event = Event::create($request->all());
|
||||
return $this->respondAsResource(
|
||||
EventConductor::model($request, $event),
|
||||
false,
|
||||
null,
|
||||
HttpResponseCodes::HTTP_CREATED
|
||||
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||
);
|
||||
} else {
|
||||
return $this->respondForbidden();
|
||||
|
||||
@@ -33,8 +33,8 @@ class MediaController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$collection,
|
||||
true,
|
||||
['total' => $total]
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -104,9 +104,7 @@ class MediaController extends ApiController
|
||||
$media = $request->user()->media()->create($request->all());
|
||||
return $this->respondAsResource(
|
||||
MediaConductor::model($request, $media),
|
||||
false,
|
||||
null,
|
||||
HttpResponseCodes::HTTP_CREATED
|
||||
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||
);
|
||||
}//end if
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ class PostController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$collection,
|
||||
true,
|
||||
['total' => $total]
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -75,9 +75,7 @@ class PostController extends ApiController
|
||||
$post = Post::create($request->all());
|
||||
return $this->respondAsResource(
|
||||
PostConductor::model($request, $post),
|
||||
false,
|
||||
null,
|
||||
HttpResponseCodes::HTTP_CREATED
|
||||
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||
);
|
||||
} else {
|
||||
return $this->respondForbidden();
|
||||
@@ -134,7 +132,7 @@ class PostController extends ApiController
|
||||
return $attachment->media;
|
||||
});
|
||||
|
||||
return $this->respondAsResource(MediaConductor::collection($request, $medium), true, null, 'attachment');
|
||||
return $this->respondAsResource(MediaConductor::collection($request, $medium), ['isCollection' => true, 'resourceName' => 'attachment']);
|
||||
}
|
||||
|
||||
return $this->respondForbidden();
|
||||
|
||||
@@ -34,8 +34,8 @@ class SubscriptionController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$collection,
|
||||
true,
|
||||
['total' => $total]
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,8 @@ class UserController extends ApiController
|
||||
|
||||
return $this->respondAsResource(
|
||||
$collection,
|
||||
true,
|
||||
['total' => $total]
|
||||
['isCollection' => true,
|
||||
'appendData' => ['total' => $total]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ class UserController extends ApiController
|
||||
{
|
||||
if (UserConductor::creatable() === true) {
|
||||
$user = User::create($request->all());
|
||||
return $this->respondAsResource(UserConductor::model($request, $user), false, [], null, HttpResponseCodes::HTTP_CREATED);
|
||||
return $this->respondAsResource(UserConductor::model($request, $user), ['respondCode' => HttpResponseCodes::HTTP_CREATED]);
|
||||
} else {
|
||||
return $this->respondForbidden();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user