- 페이지네이션 로직을 별도 JS 파일로 분리 (public/js/pagination.js)
* 쿠키 기반 per_page 값 저장 및 유지
* 페이지네이션 이벤트 핸들러 통합
* 중복 코드 제거
- 페이지네이션 UI 개선
* 처음으로/끝으로 이동 버튼 추가
* selectbox 너비 조정 (90px)
* 서버사이드 selected 속성으로 옵션 매칭 개선
- 초기 페이지당 항목 수를 10개로 변경
* TenantController, UserController, DepartmentController, RoleController 기본값 수정
- layouts/app.blade.php
* pagination.js 로드 추가
* 기존 인라인 스크립트 제거
변경 파일:
- public/js/pagination.js (신규)
- resources/views/layouts/app.blade.php
- resources/views/partials/pagination.blade.php
- app/Http/Controllers/Api/Admin/{Tenant,User,Department,Role}Controller.php
157 lines
4.6 KiB
PHP
157 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreDepartmentRequest;
|
|
use App\Http\Requests\UpdateDepartmentRequest;
|
|
use App\Services\DepartmentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class DepartmentController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DepartmentService $departmentService
|
|
) {}
|
|
|
|
/**
|
|
* 부서 목록 조회
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$departments = $this->departmentService->getDepartments(
|
|
$request->all(),
|
|
$request->integer('per_page', 10)
|
|
);
|
|
|
|
// HTMX 요청 시 HTML 반환
|
|
if ($request->header('HX-Request')) {
|
|
$html = view('departments.partials.table', compact('departments'))->render();
|
|
|
|
return response()->json([
|
|
'html' => $html,
|
|
]);
|
|
}
|
|
|
|
// 일반 요청 시 JSON 반환
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $departments->items(),
|
|
'meta' => [
|
|
'current_page' => $departments->currentPage(),
|
|
'last_page' => $departments->lastPage(),
|
|
'per_page' => $departments->perPage(),
|
|
'total' => $departments->total(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 부서 생성
|
|
*/
|
|
public function store(StoreDepartmentRequest $request): JsonResponse
|
|
{
|
|
$department = $this->departmentService->createDepartment($request->validated());
|
|
|
|
// HTMX 요청 시 성공 메시지와 리다이렉트 헤더 반환
|
|
if ($request->header('HX-Request')) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 생성되었습니다.',
|
|
'redirect' => route('departments.index'),
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 생성되었습니다.',
|
|
'data' => $department,
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* 특정 부서 조회
|
|
*/
|
|
public function show(Request $request, int $id): JsonResponse
|
|
{
|
|
$department = $this->departmentService->getDepartmentById($id);
|
|
|
|
if (!$department) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '부서를 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
// HTMX 요청 시 HTML 반환
|
|
if ($request->header('HX-Request')) {
|
|
return response()->json([
|
|
'html' => view('departments.partials.detail', compact('department'))->render(),
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $department,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 부서 수정
|
|
*/
|
|
public function update(UpdateDepartmentRequest $request, int $id): JsonResponse
|
|
{
|
|
$result = $this->departmentService->updateDepartment($id, $request->validated());
|
|
|
|
if (!$result) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '부서 수정에 실패했습니다.',
|
|
], 400);
|
|
}
|
|
|
|
// HTMX 요청 시 성공 메시지와 리다이렉트 헤더 반환
|
|
if ($request->header('HX-Request')) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 수정되었습니다.',
|
|
'redirect' => route('departments.index'),
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 수정되었습니다.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 부서 삭제 (Soft Delete)
|
|
*/
|
|
public function destroy(Request $request, int $id): JsonResponse
|
|
{
|
|
$result = $this->departmentService->deleteDepartment($id);
|
|
|
|
if (!$result) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '부서 삭제에 실패했습니다. (하위 부서가 존재할 수 있습니다)',
|
|
], 400);
|
|
}
|
|
|
|
// HTMX 요청 시 테이블 행 제거 트리거
|
|
if ($request->header('HX-Request')) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 삭제되었습니다.',
|
|
'action' => 'remove',
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '부서가 삭제되었습니다.',
|
|
]);
|
|
}
|
|
} |