3c38d085bd
is_verified 原本只有 Admin toggle 開關、無任何業務約束(稽核 P1-1)。 - DivingOffer 新增 visibleToPublic scope:provider_id null 或教練已驗證 - 公開 index/show 套用過濾,未驗證教練課程列表排除、詳情 404 - toggle-verified 後 flush diving_offers 快取標籤,切換立即生效 - 新增 provider-verification 規格(含已知限制註記)與 7 條可見性測試 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DivingOffer;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DivingOfferController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$perPage = min((int) $request->query('per_page', 12), 50);
|
|
$cacheKey = 'diving_offers_' . md5(serialize($request->all()));
|
|
|
|
$result = Cache::tags(['diving_offers'])->remember($cacheKey, 180, function () use ($request, $perPage) {
|
|
$query = DivingOffer::query()->visibleToPublic();
|
|
|
|
if ($q = $request->query('q')) {
|
|
$query->where(function ($sub) use ($q) {
|
|
$sub->where('title', 'like', "%{$q}%")
|
|
->orWhere('location', 'like', "%{$q}%")
|
|
->orWhere('spot', 'like', "%{$q}%");
|
|
});
|
|
}
|
|
|
|
if ($region = $request->query('region')) {
|
|
$query->where('region', $region);
|
|
}
|
|
|
|
if ($tag = $request->query('tag')) {
|
|
$query->where('tag', 'like', "%{$tag}%");
|
|
}
|
|
|
|
$paginated = $query->paginate($perPage);
|
|
|
|
return [
|
|
'items' => collect($paginated->items())->map(fn($o) => $this->formatOffer($o, false))->values(),
|
|
'meta' => [
|
|
'total' => $paginated->total(),
|
|
'per_page' => $paginated->perPage(),
|
|
'current_page' => $paginated->currentPage(),
|
|
'last_page' => $paginated->lastPage(),
|
|
],
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $result['items'],
|
|
'meta' => $result['meta'],
|
|
]);
|
|
}
|
|
|
|
public function show(int $id)
|
|
{
|
|
$offer = DivingOffer::with('courseImages')->visibleToPublic()->find($id);
|
|
|
|
if (!$offer) {
|
|
return response()->json(['status' => false, 'message' => '課程不存在'], 404);
|
|
}
|
|
|
|
return response()->json(['status' => true, 'data' => $this->formatOffer($offer, true)]);
|
|
}
|
|
|
|
private function formatOffer(DivingOffer $offer, bool $withImages): array
|
|
{
|
|
$data = array_merge($offer->toArray(), [
|
|
'cover_image_url' => $offer->cover_image_url,
|
|
]);
|
|
|
|
if ($withImages) {
|
|
$data['images'] = $offer->courseImages->map(fn($img) => [
|
|
'id' => $img->id,
|
|
'url' => $img->url,
|
|
'sort_order' => $img->sort_order,
|
|
])->values();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|