114 lines
3.5 KiB
PHP
114 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Slider;
|
|
use App\Traits\HandlesImageUpload;
|
|
|
|
class SliderController extends Controller
|
|
{
|
|
use HandlesImageUpload;
|
|
|
|
public function index()
|
|
{
|
|
return response()->json([
|
|
'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([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'url' => 'nullable|string|max:500',
|
|
'action' => 'nullable|array',
|
|
'image_id' => 'nullable|exists:images,id',
|
|
'image' => 'nullable|file|extensions:jpg,jpeg,png,gif,webp,bmp,svg|max:8192',
|
|
]);
|
|
|
|
// An uploaded image file takes precedence over a provided image_id.
|
|
$data['image_id'] = $this->uploadedImageId($request) ?? ($data['image_id'] ?? null);
|
|
|
|
$slider = Slider::create($data);
|
|
|
|
return response()->json([
|
|
'message' => 'Slider created successfully',
|
|
'data' => $this->formatSlider($slider->load('image')),
|
|
]);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$slider = Slider::with('image')->findOrFail($id);
|
|
return response()->json($this->formatSlider($slider));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$slider = Slider::findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'title' => 'sometimes|string|max:255',
|
|
'description' => 'sometimes|string',
|
|
'url' => 'sometimes|string|max:500',
|
|
'action' => 'nullable|array',
|
|
'image_id' => 'nullable|exists:images,id',
|
|
'image' => 'nullable|file|extensions:jpg,jpeg,png,gif,webp,bmp,svg|max:8192',
|
|
]);
|
|
|
|
// An uploaded image file takes precedence over a provided image_id.
|
|
if (($uploadedImageId = $this->uploadedImageId($request)) !== null) {
|
|
$data['image_id'] = $uploadedImageId;
|
|
}
|
|
|
|
$slider->update($data);
|
|
|
|
return response()->json([
|
|
'message' => 'Slider updated successfully',
|
|
'data' => $this->formatSlider($slider->load('image')),
|
|
]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$slider = Slider::findOrFail($id);
|
|
$slider->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Slider deleted successfully'
|
|
]);
|
|
}
|
|
}
|