Fixed major bugs
This commit is contained in:
@@ -13,9 +13,6 @@ class BylawApiController extends Controller
|
||||
{
|
||||
use BaseApiResponse;
|
||||
|
||||
/**
|
||||
* Get all Bylaw Subject Categories
|
||||
*/
|
||||
public function categories()
|
||||
{
|
||||
$categories = BylawSubjectCategory::all()->map(function ($category) {
|
||||
@@ -28,9 +25,6 @@ class BylawApiController extends Controller
|
||||
return $this->success($categories, 'Success', 'Bylaw Categories List');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Bylaw Legal Authorities
|
||||
*/
|
||||
public function authorities()
|
||||
{
|
||||
$authorities = BylawLegalAuthority::all()->map(function ($authority) {
|
||||
@@ -44,9 +38,6 @@ class BylawApiController extends Controller
|
||||
return $this->success($authorities, 'Success', 'Bylaw Authorities List');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Paginated Bylaws (Filtered & Sorted by Approval Date)
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@@ -93,9 +84,6 @@ class BylawApiController extends Controller
|
||||
return $this->success($responseData, 'Success', 'Bylaws Fetched');
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Bylaws by Keyword
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@@ -118,8 +106,6 @@ class BylawApiController extends Controller
|
||||
}
|
||||
|
||||
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
||||
|
||||
// Default search sort to newest first
|
||||
$paginator = $query->orderBy('approval_date', 'desc')->paginate($perPage);
|
||||
|
||||
$items = collect($paginator->items())->map(function ($item) {
|
||||
@@ -137,9 +123,6 @@ class BylawApiController extends Controller
|
||||
return $this->success($responseData, 'Success', 'Search Results Fetched');
|
||||
}
|
||||
|
||||
/**
|
||||
* Contextual Pager API
|
||||
*/
|
||||
public function pager($id)
|
||||
{
|
||||
$current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||
@@ -199,11 +182,15 @@ class BylawApiController extends Controller
|
||||
return $this->success($results, 'Success', 'Pager Window Details');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to uniformly format the Bylaw Document payload
|
||||
*/
|
||||
private function isLockedForCurrentUser($item): bool
|
||||
{
|
||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||
}
|
||||
|
||||
private function formatBylaw($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUser($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->title,
|
||||
@@ -219,8 +206,9 @@ class BylawApiController extends Controller
|
||||
'execution_date' => $item->execution_date,
|
||||
'bylaw_type' => $item->bylaw_type,
|
||||
'global_status' => $item->global_status,
|
||||
'pdf_file_url' => $item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null,
|
||||
'description' => $item->description,
|
||||
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
|
||||
'description' => $isLocked ? null : $item->description,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,6 @@ class GlobalSearchApiController extends Controller
|
||||
{
|
||||
use BaseApiResponse;
|
||||
|
||||
/**
|
||||
* Massive Search across ALL data types
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@@ -42,7 +39,7 @@ class GlobalSearchApiController extends Controller
|
||||
$results = collect();
|
||||
|
||||
if (!empty($searchTerm)) {
|
||||
$limitPerTable = 100; // Performance safeguard
|
||||
$limitPerTable = 100;
|
||||
|
||||
// 1. Search Bylaw Documents
|
||||
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||
@@ -111,7 +108,6 @@ class GlobalSearchApiController extends Controller
|
||||
$formatted['type'] = 'art';
|
||||
} else {
|
||||
$formatted = $this->formatStructure($item, $modelClass);
|
||||
// Standardize the structural type names (laws, sections, etc.)
|
||||
$formatted['type'] = ($table === 'section') ? 'sections' : $table;
|
||||
}
|
||||
return $formatted;
|
||||
@@ -121,12 +117,9 @@ class GlobalSearchApiController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// --- Execute Manual Pagination on the Combined Collection ---
|
||||
$total = $results->count();
|
||||
// Slice the array for the exact page requested
|
||||
$paginatedItems = $results->forPage($page, $perPage)->values();
|
||||
|
||||
// Build a proper Laravel paginator object so the response metadata remains identical
|
||||
$paginator = new LengthAwarePaginator(
|
||||
$paginatedItems,
|
||||
$total,
|
||||
@@ -150,8 +143,15 @@ class GlobalSearchApiController extends Controller
|
||||
* FORMATTING HELPERS
|
||||
* ----------------------------------------------------------------- */
|
||||
|
||||
private function isLockedForCurrentUserGeneric($item): bool
|
||||
{
|
||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||
}
|
||||
|
||||
private function formatBylaw($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->title,
|
||||
@@ -159,42 +159,48 @@ class GlobalSearchApiController extends Controller
|
||||
'approval_authority_name' => optional($item->approvalAuthority)->name,
|
||||
'approval_date' => $item->approval_date,
|
||||
'promulgation_number' => $item->promulgation_number,
|
||||
'description' => $item->description,
|
||||
'description' => $isLocked ? null : $item->description,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
|
||||
private function formatJudicial($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->subject, // Map subject to title for universal UI parsing
|
||||
'title' => $item->subject,
|
||||
'ruling_number' => $item->ruling_number,
|
||||
'ruling_date' => $item->ruling_date,
|
||||
'text' => $item->full_text,
|
||||
'text' => $isLocked ? null : $item->full_text,
|
||||
'issuing_authority' => $item->issuing_authority,
|
||||
'category_name' => optional($item->category)->name,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
|
||||
private function formatOpinion($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->subject, // Map subject to title for universal UI parsing
|
||||
'title' => $item->subject,
|
||||
'opinion_number' => $item->opinion_number,
|
||||
'opinion_date' => $item->opinion_date,
|
||||
'text' => $item->full_text,
|
||||
'text' => $isLocked ? null : $item->full_text,
|
||||
'issuing_authority' => $item->issuing_authority,
|
||||
'category_name' => optional($item->category)->name,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
|
||||
private function formatArt($item, $searchTerm, $modelClass)
|
||||
{
|
||||
$law = Law::find($item->law_id);
|
||||
$isLocked = $this->isLockedForCurrentUser($law);
|
||||
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
||||
|
||||
// Text snippet highlighting logic (from ArtController)
|
||||
$text = $item->text ?? '';
|
||||
$searchQuoted = preg_quote($searchTerm, '/');
|
||||
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
||||
@@ -221,7 +227,7 @@ class GlobalSearchApiController extends Controller
|
||||
private function formatStructure($item, $modelClass)
|
||||
{
|
||||
$law = Law::find($item?->law_id ?? $item->id);
|
||||
$isLocked = $this->isLockedForCurrentUser($law);
|
||||
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
||||
|
||||
$payload = [
|
||||
'id' => $item->id,
|
||||
@@ -240,15 +246,6 @@ class GlobalSearchApiController extends Controller
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------
|
||||
* UTILITY METHODS
|
||||
* ----------------------------------------------------------------- */
|
||||
|
||||
private function isLockedForCurrentUser(?Law $law): bool
|
||||
{
|
||||
return ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
||||
}
|
||||
|
||||
private function route($modelClass, $query)
|
||||
{
|
||||
if ((new $modelClass)->getTable() == 'art') {
|
||||
|
||||
@@ -59,9 +59,6 @@ class JudicialPrecedentApiController extends Controller
|
||||
return $this->success($responseData, 'Success', 'Judicial Precedents Fetched');
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Judicial Precedents by Keyword
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@@ -141,20 +138,25 @@ class JudicialPrecedentApiController extends Controller
|
||||
return $this->success($results, 'Success', 'Pager Window Details');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to format payload uniformly
|
||||
*/
|
||||
private function isLockedForCurrentUser($item): bool
|
||||
{
|
||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||
}
|
||||
|
||||
private function formatItem($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUser($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'ruling_number' => $item->ruling_number,
|
||||
'ruling_date' => $item->ruling_date,
|
||||
'subject' => $item->subject,
|
||||
'full_text' => $item->full_text,
|
||||
'full_text' => $isLocked ? null : $item->full_text,
|
||||
'issuing_authority' => $item->issuing_authority,
|
||||
'category_id' => $item->category_id,
|
||||
'category_name' => optional($item->category)->name,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,6 @@ class LegalOpinionApiController extends Controller
|
||||
return $this->success($responseData, 'Success', 'Legal Opinions Fetched');
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Legal Opinions by Keyword
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@@ -153,20 +150,25 @@ class LegalOpinionApiController extends Controller
|
||||
return $this->success($results, 'Success', 'Pager Window Details');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to format payload uniformly
|
||||
*/
|
||||
private function isLockedForCurrentUser($item): bool
|
||||
{
|
||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||
}
|
||||
|
||||
private function formatItem($item)
|
||||
{
|
||||
$isLocked = $this->isLockedForCurrentUser($item);
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'opinion_number' => $item->opinion_number,
|
||||
'opinion_date' => $item->opinion_date,
|
||||
'subject' => $item->subject,
|
||||
'full_text' => $item->full_text,
|
||||
'full_text' => $isLocked ? null : $item->full_text,
|
||||
'issuing_authority' => $item->issuing_authority,
|
||||
'category_id' => $item->category_id,
|
||||
'category_name' => optional($item->category)->name,
|
||||
'is_locked' => $isLocked,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user