Fixed api issue #9

Merged
soheilkhaledabadi merged 1 commits from new-api into clean-main 2026-07-12 10:42:18 +00:00

View File

@@ -7,7 +7,6 @@ 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;
@@ -95,8 +94,8 @@ class GlobalSearchApiController extends Controller
});
$results = $results->merge($opinions);
// 4. Search Arts (Optimized to mirror ArtController speed)
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
// 4. Search Arts (Exact match to ArtController logic + eager loading relations)
$arts = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%");
@@ -250,7 +249,7 @@ class GlobalSearchApiController extends Controller
$results = $results->merge($opinions);
// 4. Search Arts
$artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
$artQuery = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%");
@@ -369,12 +368,12 @@ class GlobalSearchApiController extends Controller
];
}
private function formatArt($item, $searchTerm, $modelClass)
private function formatArt($art, $searchTerm, $modelClass)
{
$law = $item->law ?? Law::find($item->law_id);
$law = $art->law ?? Law::find($art->law_id);
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
$text = $item->text ?? '';
$text = $art->text ?? '';
$searchQuoted = preg_quote($searchTerm, '/');
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
$context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? '');
@@ -382,55 +381,56 @@ class GlobalSearchApiController extends Controller
$context = $text;
}
// PERFECTLY MATCHED TO YOUR ARTCONTROLLER MAPPING
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'id' => $art->id,
'title' => $art->title,
'number' => $art->number,
'text' => $isLocked ? null : $context,
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)),
'category' => optional($law->category)->name,
'route' => array_values($this->route($modelClass, $art)),
'category' => optional($law?->category)->name,
'count_art' => $law?->arts?->count() ?? 0,
'count_volums'=> $law?->volums?->count() ?? 0,
'price' => $law?->price,
'image' => $law?->image,
// Relational Objects matching ArtController exactly
'law' => $item->law != null ? [
'id' => $item->law->id,
'title' => $item->law->title,
'number' => $item->law->number,
// Full nested relationship mappings
'chapter' => $art->chapter != null ? [
'id' => $art->chapter->id,
'title' => $art->chapter->title,
'number' => $art->chapter->number,
] : null,
'chapter' => $item->chapter != null ? [
'id' => $item->chapter->id,
'title' => $item->chapter->title,
'number' => $item->chapter->number,
'part' => $art->part != null ? [
'id' => $art->part->id,
'title' => $art->part->title,
'number' => $art->part->number,
] : null,
'part' => $item->part != null ? [
'id' => $item->part->id,
'title' => $item->part->title,
'number' => $item->part->number,
'volum' => $art->volum != null ? [
'id' => $art->volum->id,
'title' => $art->volum->title,
'number' => $art->volum->number,
] : null,
'volum' => $item->volum != null ? [
'id' => $item->volum->id,
'title' => $item->volum->title,
'number' => $item->volum->number,
'law' => $art->law != null ? [
'id' => $art->law->id,
'title' => $art->law->title,
'number' => $art->law->number,
] : null,
'book' => $item->book != null ? [
'id' => $item->book->id,
'title' => $item->book->title,
'number' => $item->book->number,
'book' => $art->book != null ? [
'id' => $art->book->id,
'title' => $art->book->title,
'number' => $art->book->number,
] : null,
'section' => $item->section != null ? [
'id' => $item->section->id,
'title' => $item->section->title,
'number' => $item->section->number,
'section' => $art->section != null ? [
'id' => $art->section->id,
'title' => $art->section->title,
'number' => $art->section->number,
] : null,
'gate' => $item->gate != null ? [
'id' => $item->gate->id,
'title' => $item->gate->title,
'number' => $item->gate->number,
'gate' => $art->gate != null ? [
'id' => $art->gate->id,
'title' => $art->gate->title,
'number' => $art->gate->number,
] : null,
];
}
@@ -463,18 +463,17 @@ class GlobalSearchApiController extends Controller
private function route($modelClass, $query)
{
// Safe Breadcrumb Building - Uses relation if loaded, else manual lookup.
if ((new $modelClass)->getTable() == 'art') {
$route = array_filter([
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
$query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
$query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Division::find($query->division_id)?->title, // Division is not in eagerly loaded relations, query safely
$query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title,
$query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
$query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title,
$query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
Branch::find($query->branch_id)?->title,
Branch::find($query->branch_id)?->title, // Branch is not in eagerly loaded relations, query safely
]);
return array_values($route);
}