diff --git a/app/Http/Controllers/SliderController.php b/app/Http/Controllers/SliderController.php index 388831c..285bfe2 100644 --- a/app/Http/Controllers/SliderController.php +++ b/app/Http/Controllers/SliderController.php @@ -13,10 +13,40 @@ class SliderController extends Controller public function index() { return response()->json([ - 'data' => Slider::with('image')->get() + 'data' => Slider::with('image')->get()->map(fn ($s) => $this->formatSlider($s))->values(), ]); } + // Shape a slider so the (old) app parses it safely: description is always a + // string, and action.type is normalized to 'navigation' or 'link' (the only + // types the client understands) — unknown/empty actions become null. + private function formatSlider(Slider $slider): array + { + return array_merge($slider->toArray(), [ + 'description' => $slider->description ?? '', + 'action' => $this->normalizeAction($slider->action), + ]); + } + + private function normalizeAction($action): ?array + { + if (!is_array($action) || empty($action['type'])) { + return null; + } + + $type = $action['type']; + + if (in_array($type, ['navigation', 'screen'], true)) { + return ['type' => 'navigation', 'path' => $action['path'] ?? ($action['url'] ?? '')]; + } + + if (in_array($type, ['link', 'url'], true)) { + return ['type' => 'link', 'url' => $action['url'] ?? ($action['path'] ?? '')]; + } + + return null; // unknown type → drop so the client doesn't throw + } + public function store(Request $request) { $data = $request->validate([ @@ -35,14 +65,14 @@ public function store(Request $request) return response()->json([ 'message' => 'Slider created successfully', - 'data' => $slider->load('image'), + 'data' => $this->formatSlider($slider->load('image')), ]); } public function show($id) { $slider = Slider::with('image')->findOrFail($id); - return response()->json($slider); + return response()->json($this->formatSlider($slider)); } public function update(Request $request, $id) @@ -67,7 +97,7 @@ public function update(Request $request, $id) return response()->json([ 'message' => 'Slider updated successfully', - 'data' => $slider->load('image'), + 'data' => $this->formatSlider($slider->load('image')), ]); }