Merge pull request 'new-api' (#6) from new-api into clean-main

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-07-12 06:07:23 +00:00
2 changed files with 111 additions and 39 deletions

View File

@@ -51,7 +51,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100',
]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true);
if ($request->filled('category_id')) {
@@ -96,7 +96,7 @@ class BylawApiController extends Controller
public function single($id)
{
$bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->find($id);
@@ -160,7 +160,7 @@ class BylawApiController extends Controller
'per_page' => 'nullable|integer|min:1|max:100',
]);
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true);
if ($request->filled('search')) {
@@ -171,8 +171,10 @@ class BylawApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
}
@@ -209,7 +211,7 @@ class BylawApiController extends Controller
{
$searchTerm = $request->input('search');
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true);
if (!empty($searchTerm)) {
@@ -218,8 +220,10 @@ class BylawApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
}
@@ -237,7 +241,7 @@ class BylawApiController extends Controller
public function pager($id)
{
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->find($id);
@@ -248,7 +252,7 @@ class BylawApiController extends Controller
], 404);
}
$previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($query) use ($current) {
if ($current->approval_date) {
@@ -265,7 +269,7 @@ class BylawApiController extends Controller
->orderBy('id', 'desc')
->first();
$nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
$nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($query) use ($current) {
if ($current->approval_date) {
@@ -303,10 +307,15 @@ class BylawApiController extends Controller
{
$isLocked = $this->isLockedForCurrentUser($item);
// Map description: If description is null, combine all node_content into one full text!
$description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
// Direct Query Fallback: If description is empty, fetch node_content manually
if (empty($description)) {
$description = BylawNode::where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->pluck('node_content')
->filter()
->implode("\n\n");
}
return [

View File

@@ -7,6 +7,7 @@ use App\Models\Art;
use App\Models\Book;
use App\Models\Branch;
use App\Models\BylawDocument;
use App\Models\BylawNode;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
@@ -25,7 +26,7 @@ class GlobalSearchApiController extends Controller
use BaseApiResponse;
/**
* Massive Search across ALL data types (With dynamic Date/Number Sorting)
* Massive Search across ALL data types (With dynamic Date/Number Sorting & Deep Art Search)
*/
public function search(Request $request)
{
@@ -46,7 +47,7 @@ class GlobalSearchApiController extends Controller
if (!empty($searchTerm)) {
$limitPerTable = 100; // Performance safeguard
// 1. Search Bylaw Documents (Including deeper node_content search)
// 1. Search Bylaw Documents
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
@@ -54,8 +55,10 @@ class GlobalSearchApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatBylaw($item);
@@ -91,10 +94,18 @@ class GlobalSearchApiController extends Controller
});
$results = $results->merge($opinions);
// 4. Search Arts
$arts = Art::with(['law.category'])
->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
// 4. Search Arts (Deep Relational Search & Eager Loading)
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); });
})
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class);
$formatted['type'] = 'art';
@@ -198,8 +209,10 @@ class GlobalSearchApiController extends Controller
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) {
$nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%");
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
});
$totalCount += $bylawQuery->count();
@@ -242,9 +255,17 @@ class GlobalSearchApiController extends Controller
$results = $results->merge($opinions);
// 4. Search Arts
$artQuery = Art::with(['law.category'])
->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%");
$artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); });
});
$totalCount += $artQuery->count();
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class);
@@ -299,7 +320,6 @@ class GlobalSearchApiController extends Controller
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
// Fallback description to node_content if empty
$description = $item->description;
if (empty($description) && $item->relationLoaded('nodes')) {
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
@@ -370,11 +390,48 @@ class GlobalSearchApiController extends Controller
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)),
'category' => optional($law->category)->name,
'law' => $law?->title,
'count_art' => $law?->arts?->count() ?? 0,
'count_volums'=> $law?->volums?->count() ?? 0,
'price' => $law?->price,
'image' => $law?->image,
// Relational Objects
'law' => $item->law != null ? [
'id' => $item->law->id,
'title' => $item->law->title,
'number' => $item->law->number,
] : null,
'chapter' => $item->chapter != null ? [
'id' => $item->chapter->id,
'title' => $item->chapter->title,
'number' => $item->chapter->number,
] : null,
'part' => $item->part != null ? [
'id' => $item->part->id,
'title' => $item->part->title,
'number' => $item->part->number,
] : null,
'volum' => $item->volum != null ? [
'id' => $item->volum->id,
'title' => $item->volum->title,
'number' => $item->volum->number,
] : null,
'book' => $item->book != null ? [
'id' => $item->book->id,
'title' => $item->book->title,
'number' => $item->book->number,
] : null,
'section' => $item->section != null ? [
'id' => $item->section->id,
'title' => $item->section->title,
'number' => $item->section->number,
] : null,
'gate' => $item->gate != null ? [
'id' => $item->gate->id,
'title' => $item->gate->title,
'number' => $item->gate->number,
] : null,
];
}
@@ -389,7 +446,12 @@ class GlobalSearchApiController extends Controller
'number' => $item->number,
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)),
'law' => $law?->title ?? '',
// Keep law payload explicitly as an object to prevent DTO parsing errors
'law' => $law ? [
'id' => $law->id,
'title' => $law->title,
'number' => $law->number,
] : null,
];
if ((new $modelClass)->getTable() == 'laws') {
@@ -402,17 +464,18 @@ class GlobalSearchApiController extends Controller
private function route($modelClass, $query)
{
// Now safely uses eager loaded relations if they exist!
if ((new $modelClass)->getTable() == 'art') {
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
Chapter::find($query->chapter_id)?->title,
Part::find($query->part_id)?->title,
Gate::find($query->gate_id)?->title,
Branch::find($query->branch_id)?->title,
$query->law->title ?? Law::find($query->law_id)?->title,
$query->volum->title ?? Volum::find($query->volum_id)?->title,
$query->book->title ?? Book::find($query->book_id)?->title,
$query->division->title ?? Division::find($query->division_id)?->title,
$query->section->title ?? Section::find($query->section_id)?->title,
$query->chapter->title ?? Chapter::find($query->chapter_id)?->title,
$query->part->title ?? Part::find($query->part_id)?->title,
$query->gate->title ?? Gate::find($query->gate_id)?->title,
$query->branch->title ?? Branch::find($query->branch_id)?->title,
]);
return array_values($route);
}