json(['layouts' => $layouts]); } /** * GET /api/floor-layouts/{layoutName} * Returns: * - data.rooms (array of room objects) * - data.bgFile (data-uri string or null) */ public function show(string $room_id) { $layout = FloorLayout::with(['room'])->where( ['room_id' => $room_id] )->first(); $bgFile = null; if ($layout->layout_bg_file) { $mime = 'image/png'; // adjust if you store SVG/jpeg $encoded = $layout->layout_bg_file; $bgFile = "data:{$mime};base64,{$encoded}"; } return response()->json([ 'room' => $layout->room, 'layoutItems' => $layout->data, 'bgFile' => $bgFile, ]); } /** * POST /api/floor-layouts/{layoutName} * Accepts multipart/form-data with: * - rooms : JSON array * - bg_file : optional image file */ public function update(Request $request) { // Validate rooms array, and optionally a Base64 image string $validated = $request->validate([ 'contents' => 'required|array', 'room_id' => 'required|integer', ]); // Lookup (or new) by the unique layout_name $layout = FloorLayout::where(['room_id' => $validated['room_id']])->first(); // Save room JSON, track user, persist $layout->data = $validated['contents']; $layout->user_id = Auth::id(); $layout->save(); // will INSERT or UPDATE, with your unique key on layout_name return response()->json([ 'success' => true, 'updated_at' => $layout->updated_at->toIso8601String(), ]); } public function create(Request $request) { // Validate that we got a layout name, and optionally a Base64‐encoded image $validated = $request->validate([ 'room_id' => 'required|integer', 'name' => 'required|string|max:100', 'bgFile' => 'nullable|string', // still expect a Data-URL / Base64 string ]); // Use the submitted name as the unique layout_name $layout = FloorLayout::firstOrNew(['layout_name' => $validated['name'], 'room_id' => $validated['room_id']]); // If they sent us a Data-URL, strip the prefix and store *that* Base64 if (!empty($validated['bgFile'])) { if (preg_match('#^data:image/\w+;base64,#i', $validated['bgFile'])) { // keep only the Base64 payload $base64 = substr($validated['bgFile'], strpos($validated['bgFile'], ',') + 1); $layout->layout_bg_file = $base64; } else { return response()->json([ 'success' => false, 'message' => 'bgFile must be a Base64 data URL', ], 422); } } // Initialize with empty rooms array (you’ll add rooms via the save‐layout endpoint later) $layout->data = []; $layout->user_id = Auth::id(); $layout->save(); // INSERT or UPDATE // Return the layout_name so the front end can add it into tabs & select it return response()->json([ 'success' => true, 'layout' => $layout->layout_name, 'created_at' => $layout->created_at->toIso8601String(), ]); } }