json([ 'data' => AppVersion::with('image')->orderByDesc('version_code')->get(), ]); } // App side: the newest version (highest version_code) for update checks. public function latest() { return response()->json([ 'data' => AppVersion::with('image')->orderByDesc('version_code')->first(), ]); } public function store(Request $request) { $data = $request->validate([ 'title' => 'required|string|max:255', 'description' => 'nullable|string', 'version_name' => 'required|string|max:255', 'version_code' => 'required|integer|min:0', 'link' => 'nullable|string|max:500', '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); $version = AppVersion::create($data); return response()->json([ 'message' => 'Version created successfully', 'data' => $version->load('image'), ], 201); } public function show($id) { return response()->json( AppVersion::with('image')->findOrFail($id) ); } public function update(Request $request, $id) { $version = AppVersion::findOrFail($id); $data = $request->validate([ 'title' => 'sometimes|string|max:255', 'description' => 'nullable|string', 'version_name' => 'sometimes|string|max:255', 'version_code' => 'sometimes|integer|min:0', 'link' => 'nullable|string|max:500', '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; } $version->update($data); return response()->json([ 'message' => 'Version updated successfully', 'data' => $version->load('image'), ]); } public function destroy($id) { $version = AppVersion::findOrFail($id); $version->delete(); return response()->json(['message' => 'Version deleted successfully']); } }