Skip to content

Commit

Permalink
fix(controllers): Allow JSON responses in POST controller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
anibalealvarezs committed Aug 26, 2022
1 parent a8c958a commit 1e39d9d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/Controllers/Config/PbConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function create(string $route = 'level'): InertiaResponse|JsonResponse
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|Application|JsonResponse|null
{
$this->vars->validationRules['configkey'] = ['required', 'max:50', Rule::unique($this->vars->level->table)];

Expand Down Expand Up @@ -93,9 +93,9 @@ public function edit(
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse|null
{
$this->vars->validationRules['configkey'] = ['required', 'max:50', Rule::unique($this->vars->level->table)->ignore($id)];

Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/Config/PbLocaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class PbLocaleController extends Controller
* Remove the specified resource from storage.
*
* @param Request $request
* @return RedirectResponse
* @return RedirectResponse|JsonResponse
*/
public function update(Request $request): RedirectResponse
public function update(Request $request): RedirectResponse|JsonResponse
{
if (!$language = PbLanguage::findByCode($request->input('locale'))) {
return $this->redirectResponse(
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/File/PbFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ function __construct(Request $request, $crud_perms = false)
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|Application|JsonResponse|null
{
$this->pushValidationRules([
'file' => ['required', 'file', 'mimes:jpeg,png,jpg,gif,svg,pdf', 'max:8192'],
Expand Down Expand Up @@ -108,9 +108,9 @@ public function store(Request $request): Redirector|RedirectResponse|Application
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse|null
{
// Validation
if ($failed = $this->validateRequest($this->vars->validationRules, $request)) {
Expand Down Expand Up @@ -178,9 +178,9 @@ public function update(Request $request, int $id): Redirector|RedirectResponse|A
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse
* @return Application|Redirector|RedirectResponse|JsonResponse
*/
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse
{
// Set cache/methods arguments
$this->initArgs([
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/PbBuilderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public function create(string $route = 'level'): InertiaResponse|JsonResponse
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Redirector|RedirectResponse|JsonResponse|Application|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|JsonResponse|Application|null
{
// Validation
if ($failed = $this->validateRequest($this->vars->validationRules, $request)) {
Expand Down Expand Up @@ -340,9 +340,9 @@ public function edit(
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|JsonResponse|Application|null
{
// Validation
if ($failed = $this->validateRequest($this->vars->validationRules, $request)) {
Expand Down Expand Up @@ -392,9 +392,9 @@ public function update(Request $request, int $id): Redirector|RedirectResponse|A
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse
* @return Application|Redirector|JsonResponse|RedirectResponse
*/
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application
public function destroy(Request $request, int $id): Redirector|RedirectResponse|JsonResponse|Application
{
// Set cache/methods arguments
$this->initArgs([
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/Permission/PbPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function index(
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|Application|JsonResponse|null
{
// Validation
if ($failed = $this->validateRequest($this->vars->validationRules, $request)) {
Expand Down Expand Up @@ -244,9 +244,9 @@ public function edit(
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse|null
{
// Validation
if ($failed = $this->validateRequest($this->vars->validationRules, $request)) {
Expand Down Expand Up @@ -324,9 +324,9 @@ public function update(Request $request, int $id): Redirector|RedirectResponse|A
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse
* @return Application|Redirector|RedirectResponse|JsonResponse
*/
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse
{
// Set cache/methods arguments
$this->initArgs([
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/Role/PbRoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public function create(string $route = 'level'): InertiaResponse|JsonResponse
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|Application|JsonResponse|null
{
$this->pushValidationRules([
'name' => ['required', 'max:20', Rule::unique($this->vars->level->table)],
Expand Down Expand Up @@ -238,9 +238,9 @@ public function edit(int $id, $element = null, bool $multiple = false, string $r
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse|null
{
$this->pushValidationRules([
'name' => ['required', 'max:20', Rule::unique($this->vars->level->table)->ignore($id)],
Expand Down Expand Up @@ -314,9 +314,9 @@ public function update(Request $request, int $id): Redirector|RedirectResponse|A
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse
* @return Application|Redirector|RedirectResponse|JsonResponse
*/
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application
public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application|JsonResponse
{
// Set cache/methods arguments
$this->initArgs([
Expand Down
8 changes: 4 additions & 4 deletions src/Controllers/User/PbUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public function create(string $route = 'level'): InertiaResponse|JsonResponse
* Store a newly created resource in storage.
*
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|RedirectResponse|JsonResponse|null
*/
public function store(Request $request): Redirector|RedirectResponse|Application|null
public function store(Request $request): Redirector|RedirectResponse|JsonResponse|Application|null
{
$this->pushValidationRules([
'password' => ['required'],
Expand Down Expand Up @@ -301,9 +301,9 @@ public function edit(int $id, $element = null, bool $multiple = false, string $r
*
* @param Request $request
* @param int $id
* @return Application|Redirector|RedirectResponse|null
* @return Application|Redirector|JsonResponse|RedirectResponse|null
*/
public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null
public function update(Request $request, int $id): Redirector|RedirectResponse|JsonResponse|Application|null
{
$this->pushValidationRules([
'email' => ['required', 'max:50', 'email', Rule::unique($this->vars->level->table)->ignore($id)],
Expand Down
56 changes: 34 additions & 22 deletions src/Traits/PbControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ protected function getAllowed(array|string $permissions): array
*
* @param array $validationRules
* @param Request $request
* @return Application|Redirector|RedirectResponse|null
* @return Redirector|RedirectResponse|Application|JsonResponse|null
*/
protected function validateRequest(array $validationRules, Request $request): Redirector|RedirectResponse|Application|null
protected function validateRequest(array $validationRules, Request $request): Redirector|RedirectResponse|JsonResponse|Application|null
{
$validator = Validator::make($request->all(), $validationRules);

Expand All @@ -99,7 +99,7 @@ protected function validateRequest(array $validationRules, Request $request): Re
* @param string $destiny
* @param string $flashStyle
* @param bool $withInput
* @return RedirectResponse|Application|Redirector
* @return RedirectResponse|Application|Redirector|JsonResponse
*/
protected function redirectResponse(
Request $request,
Expand All @@ -108,9 +108,13 @@ protected function redirectResponse(
string $destiny = "",
string $flashStyle = 'danger',
bool $withInput = true
): RedirectResponse|Application|Redirector {
$request->session()->flash('flash.banner', $flashMessage);
$request->session()->flash('flash.bannerStyle', $flashStyle);
): RedirectResponse|Application|Redirector|JsonResponse {
if (!isApi($request)) {
$request->session()->flash('flash.banner', $flashMessage);
$request->session()->flash('flash.bannerStyle', $flashStyle);
} else {
return $this->handleJSONError($flashMessage);
}

$redirect = redirect();

Expand Down Expand Up @@ -139,20 +143,24 @@ protected function redirectResponse(
*
* @param Request $request
* @param string $process
* @return RedirectResponse|Application|Redirector
* @return RedirectResponse|Application|Redirector|JsonResponse
*/
protected function redirectResponseCRUDSuccess(
Request $request,
string $process
): Redirector|Application|RedirectResponse {
return $this->redirectResponse(
$request,
$this->buildCRUDResponseMessage($process, 'success'),
'route',
$this->vars->level->names . '.index',
'success',
false
);
): Redirector|Application|RedirectResponse|JsonResponse {
if (!isApi($request)) {
return $this->redirectResponse(
$request,
$this->buildCRUDResponseMessage($process, 'success'),
'route',
$this->vars->level->names . '.index',
'success',
false
);
} else {
return $this->handleJSONResponse([], $this->buildCRUDResponseMessage($process, 'success'));
}
}

/**
Expand All @@ -161,17 +169,21 @@ protected function redirectResponseCRUDSuccess(
* @param Request $request
* @param string $process
* @param string $error
* @return RedirectResponse|Application|Redirector
* @return RedirectResponse|Application|Redirector|JsonResponse
*/
protected function redirectResponseCRUDFail(
Request $request,
string $process,
string $error
): Redirector|Application|RedirectResponse {
return $this->redirectResponse(
$request,
$this->buildCRUDResponseMessage($process, 'fail', $error)
);
): Redirector|Application|RedirectResponse|JsonResponse {
if (!isApi($request)) {
return $this->redirectResponse(
$request,
$this->buildCRUDResponseMessage($process, 'fail', $error)
);
} else {
return $this->handleJSONError($this->buildCRUDResponseMessage($process, 'fail', $error));
}
}

/**
Expand Down

0 comments on commit 1e39d9d

Please sign in to comment.