Files
Investidor-V2/app/Http/Controllers/Api/V1/HomeController.php
T
nala 250ac35fd1
tests / ci (push) Canceled after 0s
first commit
2026-09-28 10:01:09 +02:00

63 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Models\Opportunity;
use App\Models\Sector;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;
class HomeController extends Controller
{
public function show(Request $request): JsonResponse
{
$validated = $request->validate([
'locale' => ['nullable', 'string', Rule::in(['pt', 'en'])],
]);
$locale = $validated['locale'] ?? 'pt';
$data = Cache::remember("portal_home_v2_{$locale}", now()->addMinutes(10), function () {
$sectors = Sector::withCount([
'opportunities as opportunities_count' => fn ($q) => $q->published(),
])->where('is_active', true)->orderBy('sort_order')->get();
$featured = Opportunity::with('sector:id,name,slug')
->published()->where('is_featured', true)
->latest('published_at')->limit(6)->get();
return [
'sectors' => $sectors->map(fn ($s) => [
'name' => $s->name,
'slug' => $s->slug,
'shortDescription' => $s->short_description,
'opportunitiesCount' => $s->opportunities_count,
'url' => "/sectores/{$s->slug}",
])->values()->all(),
'featuredOpportunities' => $featured->map(fn ($o) => [
'reference' => $o->reference,
'title' => $o->title,
'sector' => $o->sector?->name,
'province' => $o->province,
'maturity' => $o->maturity_label,
'estimatedInvestment' => [
'amount' => $o->estimated_investment_amount,
'currency' => $o->estimated_investment_currency,
],
'url' => "/oportunidades/{$o->slug}",
])->values()->all(),
'stats' => [
'opportunities' => Opportunity::published()->count(),
'sectors' => Sector::where('is_active', true)->count(),
'provinces' => Opportunity::published()->distinct('province')->count('province'),
],
];
});
return response()->json(['success' => true, 'locale' => $locale, 'data' => $data]);
}
}