Files
sam-manage/app/Services/AppVersionService.php

66 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Models\AppVersion;
use Illuminate\Http\UploadedFile;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Storage;
class AppVersionService
{
/**
* 버전 목록 (페이지네이션)
*/
public function list(int $perPage = 20): LengthAwarePaginator
{
return AppVersion::orderByDesc('version_code')
->paginate($perPage);
}
/**
* 버전 등록
*/
public function store(array $data, ?UploadedFile $apkFile = null): AppVersion
{
if ($apkFile) {
$data['apk_original_name'] = $apkFile->getClientOriginalName();
$data['apk_size'] = $apkFile->getSize();
$data['apk_path'] = $apkFile->store('apk', 'app_releases');
}
$data['created_by'] = auth()->id();
$data['published_at'] = $data['published_at'] ?? now();
return AppVersion::create($data);
}
/**
* 활성 토글
*/
public function toggleActive(int $id): AppVersion
{
$version = AppVersion::findOrFail($id);
$version->is_active = ! $version->is_active;
$version->updated_by = auth()->id();
$version->save();
return $version;
}
/**
* 삭제
*/
public function destroy(int $id): void
{
$version = AppVersion::findOrFail($id);
// APK 파일 삭제
if ($version->apk_path && Storage::disk('app_releases')->exists($version->apk_path)) {
Storage::disk('app_releases')->delete($version->apk_path);
}
$version->delete();
}
}