feat: add version

This commit is contained in:
2026-06-24 18:10:30 +03:30
parent 40b7efac2f
commit 706b349130
4 changed files with 152 additions and 0 deletions
@@ -0,0 +1,93 @@
<?php
namespace App\Http\Controllers;
use App\Models\AppVersion;
use App\Traits\HandlesImageUpload;
use Illuminate\Http\Request;
class AppVersionController extends Controller
{
use HandlesImageUpload;
// Admin list — every version, highest version_code first.
public function index()
{
return response()->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']);
}
}