235 lines
7.2 KiB
PHP
235 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Barobill;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Barobill\BarobillConfig;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BarobillConfigController extends Controller
|
|
{
|
|
/**
|
|
* 설정 목록 조회
|
|
*/
|
|
public function index(Request $request): JsonResponse|Response
|
|
{
|
|
$configs = BarobillConfig::query()
|
|
->orderBy('environment')
|
|
->orderBy('is_active', 'desc')
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
// HTMX 요청 시 HTML 반환
|
|
if ($request->header('HX-Request')) {
|
|
return response(
|
|
view('barobill.config.partials.table', compact('configs'))->render(),
|
|
200,
|
|
['Content-Type' => 'text/html']
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $configs,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 설정 등록
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:50',
|
|
'environment' => 'required|in:test,production',
|
|
'cert_key' => 'required|string|max:100',
|
|
'corp_num' => 'nullable|string|max:20',
|
|
'base_url' => 'required|url|max:255',
|
|
'description' => 'nullable|string|max:500',
|
|
'is_active' => 'nullable|boolean',
|
|
], [
|
|
'name.required' => '설정 이름을 입력해주세요.',
|
|
'environment.required' => '환경을 선택해주세요.',
|
|
'cert_key.required' => '인증키(CERTKEY)를 입력해주세요.',
|
|
'base_url.required' => '서버 URL을 입력해주세요.',
|
|
'base_url.url' => '올바른 URL 형식을 입력해주세요.',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
// is_active가 true이면 같은 환경의 다른 설정들은 비활성화
|
|
if ($validated['is_active'] ?? false) {
|
|
BarobillConfig::where('environment', $validated['environment'])
|
|
->update(['is_active' => false]);
|
|
}
|
|
|
|
$config = BarobillConfig::create($validated);
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '바로빌 설정이 등록되었습니다.',
|
|
'data' => $config,
|
|
], 201);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('바로빌 설정 등록 실패', ['error' => $e->getMessage()]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정 등록 중 오류가 발생했습니다.',
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 설정 상세 조회
|
|
*/
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$config = BarobillConfig::find($id);
|
|
|
|
if (! $config) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정을 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $config,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 설정 수정
|
|
*/
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$config = BarobillConfig::find($id);
|
|
|
|
if (! $config) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정을 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:50',
|
|
'environment' => 'required|in:test,production',
|
|
'cert_key' => 'required|string|max:100',
|
|
'corp_num' => 'nullable|string|max:20',
|
|
'base_url' => 'required|url|max:255',
|
|
'description' => 'nullable|string|max:500',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
// is_active가 true이면 같은 환경의 다른 설정들은 비활성화
|
|
if ($validated['is_active'] ?? false) {
|
|
BarobillConfig::where('environment', $validated['environment'])
|
|
->where('id', '!=', $id)
|
|
->update(['is_active' => false]);
|
|
}
|
|
|
|
$config->update($validated);
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '바로빌 설정이 수정되었습니다.',
|
|
'data' => $config->fresh(),
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('바로빌 설정 수정 실패', ['error' => $e->getMessage()]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정 수정 중 오류가 발생했습니다.',
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 설정 삭제
|
|
*/
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$config = BarobillConfig::find($id);
|
|
|
|
if (! $config) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정을 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
if ($config->is_active) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '활성화된 설정은 삭제할 수 없습니다. 먼저 비활성화해주세요.',
|
|
], 422);
|
|
}
|
|
|
|
$config->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '바로빌 설정이 삭제되었습니다.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 설정 활성화/비활성화 토글
|
|
*/
|
|
public function toggleActive(int $id): JsonResponse
|
|
{
|
|
$config = BarobillConfig::find($id);
|
|
|
|
if (! $config) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정을 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
if (! $config->is_active) {
|
|
// 활성화하려면 같은 환경의 다른 설정들 비활성화
|
|
BarobillConfig::where('environment', $config->environment)
|
|
->where('id', '!=', $id)
|
|
->update(['is_active' => false]);
|
|
}
|
|
|
|
$config->update(['is_active' => ! $config->is_active]);
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => $config->is_active ? '설정이 활성화되었습니다.' : '설정이 비활성화되었습니다.',
|
|
'data' => $config->fresh(),
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('바로빌 설정 토글 실패', ['error' => $e->getMessage()]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '설정 변경 중 오류가 발생했습니다.',
|
|
], 500);
|
|
}
|
|
}
|
|
}
|