Compare commits

2 Commits
+34 -4
View File
@@ -13,10 +13,40 @@ class SliderController extends Controller
public function index() public function index()
{ {
return response()->json([ 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) public function store(Request $request)
{ {
$data = $request->validate([ $data = $request->validate([
@@ -35,14 +65,14 @@ public function store(Request $request)
return response()->json([ return response()->json([
'message' => 'Slider created successfully', 'message' => 'Slider created successfully',
'data' => $slider->load('image'), 'data' => $this->formatSlider($slider->load('image')),
]); ]);
} }
public function show($id) public function show($id)
{ {
$slider = Slider::with('image')->findOrFail($id); $slider = Slider::with('image')->findOrFail($id);
return response()->json($slider); return response()->json($this->formatSlider($slider));
} }
public function update(Request $request, $id) public function update(Request $request, $id)
@@ -67,7 +97,7 @@ public function update(Request $request, $id)
return response()->json([ return response()->json([
'message' => 'Slider updated successfully', 'message' => 'Slider updated successfully',
'data' => $slider->load('image'), 'data' => $this->formatSlider($slider->load('image')),
]); ]);
} }