285 lines
8.2 KiB
PHP
285 lines
8.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Services\BoardService;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\View\View;
|
||
|
|
|
||
|
|
class BoardController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly BoardService $boardService
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 목록 (HTMX용)
|
||
|
|
*/
|
||
|
|
public function index(Request $request): View|JsonResponse
|
||
|
|
{
|
||
|
|
$filters = $request->only(['search', 'board_type', 'is_active', 'trashed', 'sort_by', 'sort_direction']);
|
||
|
|
$boards = $this->boardService->getBoards($filters, 15);
|
||
|
|
|
||
|
|
// HTMX 요청이면 HTML 파셜 반환
|
||
|
|
if ($request->header('HX-Request')) {
|
||
|
|
return view('boards.partials.table', compact('boards'));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 일반 요청이면 JSON
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'data' => $boards,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 통계
|
||
|
|
*/
|
||
|
|
public function stats(): JsonResponse
|
||
|
|
{
|
||
|
|
$stats = $this->boardService->getBoardStats();
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'data' => $stats,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 상세 조회
|
||
|
|
*/
|
||
|
|
public function show(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$board = $this->boardService->getBoardById($id, true);
|
||
|
|
|
||
|
|
if (! $board) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => '게시판을 찾을 수 없습니다.',
|
||
|
|
], 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'data' => $board,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 생성
|
||
|
|
*/
|
||
|
|
public function store(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'board_code' => 'required|string|max:50',
|
||
|
|
'name' => 'required|string|max:100',
|
||
|
|
'board_type' => 'nullable|string|max:50',
|
||
|
|
'description' => 'nullable|string|max:500',
|
||
|
|
'editor_type' => 'nullable|string|in:wysiwyg,markdown,text',
|
||
|
|
'allow_files' => 'nullable|boolean',
|
||
|
|
'max_file_count' => 'nullable|integer|min:0|max:100',
|
||
|
|
'max_file_size' => 'nullable|integer|min:0',
|
||
|
|
'extra_settings' => 'nullable|array',
|
||
|
|
'is_active' => 'nullable|boolean',
|
||
|
|
]);
|
||
|
|
|
||
|
|
// 코드 중복 체크
|
||
|
|
if ($this->boardService->isCodeExists($validated['board_code'])) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => '이미 사용 중인 게시판 코드입니다.',
|
||
|
|
], 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
$board = $this->boardService->createBoard($validated);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '게시판이 생성되었습니다.',
|
||
|
|
'data' => $board,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 수정
|
||
|
|
*/
|
||
|
|
public function update(Request $request, int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'board_code' => 'required|string|max:50',
|
||
|
|
'name' => 'required|string|max:100',
|
||
|
|
'board_type' => 'nullable|string|max:50',
|
||
|
|
'description' => 'nullable|string|max:500',
|
||
|
|
'editor_type' => 'nullable|string|in:wysiwyg,markdown,text',
|
||
|
|
'allow_files' => 'nullable|boolean',
|
||
|
|
'max_file_count' => 'nullable|integer|min:0|max:100',
|
||
|
|
'max_file_size' => 'nullable|integer|min:0',
|
||
|
|
'extra_settings' => 'nullable|array',
|
||
|
|
'is_active' => 'nullable|boolean',
|
||
|
|
]);
|
||
|
|
|
||
|
|
// 코드 중복 체크 (자신 제외)
|
||
|
|
if ($this->boardService->isCodeExists($validated['board_code'], $id)) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => '이미 사용 중인 게시판 코드입니다.',
|
||
|
|
], 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->boardService->updateBoard($id, $validated);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '게시판이 수정되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 삭제 (Soft Delete)
|
||
|
|
*/
|
||
|
|
public function destroy(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$this->boardService->deleteBoard($id);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '게시판이 삭제되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 복원
|
||
|
|
*/
|
||
|
|
public function restore(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$this->boardService->restoreBoard($id);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '게시판이 복원되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 영구 삭제
|
||
|
|
*/
|
||
|
|
public function forceDestroy(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$this->boardService->forceDeleteBoard($id);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '게시판이 영구 삭제되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 활성/비활성 토글
|
||
|
|
*/
|
||
|
|
public function toggleActive(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$board = $this->boardService->toggleActive($id);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => $board->is_active ? '게시판이 활성화되었습니다.' : '게시판이 비활성화되었습니다.',
|
||
|
|
'data' => ['is_active' => $board->is_active],
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =========================================================================
|
||
|
|
// 필드 관리 API
|
||
|
|
// =========================================================================
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 필드 목록
|
||
|
|
*/
|
||
|
|
public function fields(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$fields = $this->boardService->getBoardFields($id);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'data' => $fields,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 필드 추가
|
||
|
|
*/
|
||
|
|
public function storeField(Request $request, int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'name' => 'required|string|max:100',
|
||
|
|
'field_key' => 'required|string|max:50',
|
||
|
|
'field_type' => 'required|string|in:text,number,select,date,textarea,checkbox,radio,file',
|
||
|
|
'field_meta' => 'nullable|array',
|
||
|
|
'is_required' => 'nullable|boolean',
|
||
|
|
'sort_order' => 'nullable|integer',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$field = $this->boardService->addBoardField($id, $validated);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '필드가 추가되었습니다.',
|
||
|
|
'data' => $field,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 필드 수정
|
||
|
|
*/
|
||
|
|
public function updateField(Request $request, int $id, int $fieldId): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'name' => 'required|string|max:100',
|
||
|
|
'field_key' => 'required|string|max:50',
|
||
|
|
'field_type' => 'required|string|in:text,number,select,date,textarea,checkbox,radio,file',
|
||
|
|
'field_meta' => 'nullable|array',
|
||
|
|
'is_required' => 'nullable|boolean',
|
||
|
|
'sort_order' => 'nullable|integer',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->boardService->updateBoardField($fieldId, $validated);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '필드가 수정되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 필드 삭제
|
||
|
|
*/
|
||
|
|
public function destroyField(int $id, int $fieldId): JsonResponse
|
||
|
|
{
|
||
|
|
$this->boardService->deleteBoardField($fieldId);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '필드가 삭제되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 게시판 필드 순서 변경
|
||
|
|
*/
|
||
|
|
public function reorderFields(Request $request, int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'field_ids' => 'required|array',
|
||
|
|
'field_ids.*' => 'integer',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->boardService->reorderBoardFields($id, $validated['field_ids']);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => '필드 순서가 변경되었습니다.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|