Skip to content

Add Basic JSON API Support to Index Actions in Controllers #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Homestead.yaml
.env
/nbproject/private/
/config/renewal.php
.DS_Store
9 changes: 8 additions & 1 deletion app/Http/Controllers/ActorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public function index(Request $request)
$actor = $actor->where('warn', 1);
break;
}
$actorslist = $actor->with('company')->orderby('name')->paginate(21);

$query = $actor->with('company')->orderby('name');

if ($request->wantsJson()) {
return response()->json($query->get());
}

$actorslist = $query->paginate(21);
$actorslist->appends($request->input())->links();

return view('actor.index', compact('actorslist'));
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function index(Request $request)

$categories = $category->get();

if ($request->wantsJson()) {
return response()->json($categories);
}

return view('category.index', compact('categories'));
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/ClassifierTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function index(Request $request)

$types = $classifierType->with(['category:code,category'])->get();

if ($request->wantsJson()) {
return response()->json($types);
}

return view('classifier_type.index', compact('types'));
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/DefaultActorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function index(Request $request)
}
$default_actors = $default_actor->with(['roleInfo:code,name', 'actor:id,name', 'client:id,name', 'category:code,category', 'country:iso,name'])->get();

if ($request->wantsJson()) {
return response()->json($default_actors);
}

return view('default_actor.index', compact('default_actors'));
}

Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public function index(Request $request)
$template_classes = $template_classes->whereLike('notes', $Notes.'%');
}

$template_classes = $template_classes->orderby('name')->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));
$query = $template_classes->orderby('name');

if ($request->wantsJson()) {
return response()->json($query->get());
}

$template_classes = $query->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));
$template_classes->appends($request->input())->links();

return view('documents.index', compact('template_classes'));
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/EventNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function index(Request $request)
$ename = $ename->whereJsonLike('name', $Name);
}

if ($request->wantsJson()) {
return response()->json($ename->get());
}

$enameslist = $ename->paginate(21);
$enameslist->appends($request->input())->links();

Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/FeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ public function index(Request $request)
}
}
}
$fees = $fees->orderBy('for_category')->orderBy('for_country')->orderBy('qt')->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));

$query = $fees->orderBy('for_category')->orderBy('for_country')->orderBy('qt');

if ($request->wantsJson()) {
return response()->json($query->get());
}

$fees = $query->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));

return view('fee.index', compact('fees'));
}
Expand Down
32 changes: 19 additions & 13 deletions app/Http/Controllers/MatterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,31 @@ public function index(Request $request)
{
$filters = $request->except(
[
'display_with',
'page',
'filter',
'value',
'sortkey',
'sortdir',
'tab',
'include_dead',
]
);
'display_with',
'page',
'filter',
'value',
'sortkey',
'sortdir',
'tab',
'include_dead',
]);

$matters = Matter::filter(
$query = Matter::filter(
$request->input('sortkey', 'id'),
$request->input('sortdir', 'desc'),
$filters,
$request->display_with,
$request->include_dead
)->simplePaginate(25);
$matters->withQueryString()->links(); // Keep URL parameters in the paginator links
);

if ($request->wantsJson()) {
$matters = $query->with('events.info')->get();
return response()->json($matters);
}
Comment on lines +59 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The JSON response for matters is unpaginated and includes events.info. This is a good first step for the API.

However, as the number of matters can be substantial, returning all of them at once might lead to performance or memory issues. For future enhancements, would it be beneficial to implement pagination for the JSON API, similar to how the HTML view is paginated?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be thousands of matters. It might be appropriate to keep pagination.


$matters = $query->simplePaginate(25);
$matters->withQueryString()->links(); // Keep URL parameters in the paginator links

return view('matter.index', compact('matters'));
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/MatterTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function index(Request $request)

$matter_types = $type->get();

if ($request->wantsJson()) {
return response()->json($matter_types);
}

return view('type.index', compact('matter_types'));
}

Expand Down
13 changes: 12 additions & 1 deletion app/Http/Controllers/RenewalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ public function index(Request $request)
$renewals->orderByDesc('due_date');
}

if ($request->wantsJson()) {
$renewals = $renewals->get();
$renewals->transform(function ($ren) {
$this->adjustFees($ren, $cost, $fee);
$ren->cost = $cost;
$ren->fee = $fee;
return $ren;
});
return response()->json($renewals);
}
Comment on lines +99 to +108

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The JSON response for renewals returns an unpaginated dataset after applying transformations. Consistent with the PR's goal, but as with other controllers, pagination should be a consideration for future API versions to handle potentially large renewal lists effectively.


$renewals = $renewals->simplePaginate(config('renewal.general.paginate', 25));

// Adjust the cost and fee of each renewal based on customized settings
Expand All @@ -106,7 +117,7 @@ public function index(Request $request)
return $ren;
});

$renewals->appends($request->input())->links(); // Keep URL parameters in the paginator links
$renewals->appends($request->input())->links(); // Keep URL parameters in the paginator links
return view('renewals.index', compact('renewals', 'step', 'invoice_step'));
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function index(Request $request)

$roles = $role->get();

if ($request->wantsJson()) {
return response()->json($roles);
}

return view('role.index', compact('roles'));
}

Expand Down
11 changes: 8 additions & 3 deletions app/Http/Controllers/RuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ public function index(Request $request)
$rule = $rule->whereLike('for_origin', "{$Origin}%");
}

$ruleslist = $rule->with(['country:iso,name', 'trigger:code,name', 'category:code,category', 'origin:iso,name', 'type:code,type', 'taskInfo:code,name'])
$query = $rule->with(['country:iso,name', 'trigger:code,name', 'category:code,category', 'origin:iso,name', 'type:code,type', 'taskInfo:code,name'])
->select('task_rules.*')
->join('event_name AS t', 't.code', '=', 'task_rules.task')
->orderByRaw("t.name->>'$.$baseLocale'")
->paginate(21);
->orderByRaw("t.name->>'$.$baseLocale'");

if ($request->wantsJson()) {
return response()->json($query->get());
}

$ruleslist = $query->paginate(21);
$ruleslist->appends($request->input())->links();

return view('rule.index', compact('ruleslist'));
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public function index(Request $request)
});
}

$tasks = $tasks->orderBy('due_date')->simplePaginate(18)
$query = $tasks->orderBy('due_date');

if ($request->wantsJson()) {
return response()->json($query->get());
}
Comment on lines +52 to +54

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This endpoint returns all tasks (matching filters) unpaginated for JSON requests. While this fulfills the current requirement for a full dataset, it's important to consider scalability.

Could a large number of tasks lead to performance issues with an unpaginated JSON response? Planning for pagination in future API versions might be beneficial.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true that there can be tens of thousands of tasks, and keeping pagination could be appropriate.


$tasks = $query->simplePaginate(18)
->appends($request->input());

return view('task.index', compact('tasks', 'isrenewals'));
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/TemplateMemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public function index(Request $request)
$template_members = $template_members->where('style', 'LIKE', "$Style%");
}

$template_members = $template_members->orderBy('summary')->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));
$query = $template_members->orderBy('summary');

if ($request->wantsJson()) {
return response()->json($query->get());
}

$template_members = $query->simplePaginate(config('renewal.general.paginate') == 0 ? 25 : intval(config('renewal.general.paginate')));
$template_members->appends($request->input())->links();

return view('template-members.index', compact('template_members'));
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public function index(Request $request)
if ($request->filled('Name')) {
$user = $user->where('name', 'like', $request->Name . '%');
}
$userslist = $user->with('company')->orderby('name')->paginate(21);

$query = $user->with('company')->orderby('name');

if ($request->wantsJson()) {
return response()->json($query->get());
}

$userslist = $query->paginate(21);
$userslist->appends($request->input())->links();

return view('user.index', compact('userslist'));
Expand Down