trafficmonitor/app/Controllers/AbstractCRUDController.php

101 lines
3.2 KiB
PHP

<?php
namespace App\Controllers;
use App\Entities\CommonEntity;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\Validation\Exceptions\ValidationException;
/**
* AbstractCRUDController
* 단일 레코드 생성(C), 조회(R), 수정(U), 삭제(D) 로직을 담당합니다. (SRP: Single Record Management)
*/
abstract class AbstractCRUDController extends AbstractWebController
{
// --- 생성 (Create) ---
protected function create_form_process(array $formDatas = []): array
{
// Form Default값 설정 (오버라이드 포인트)
return $formDatas;
}
protected function create_form_result_process(string $action): string|RedirectResponse
{
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
protected function create_process(): CommonEntity
{
// POST 데이터를 DTO 객체로 변환 (getPost()는 POST 요청 본문만 가져옵니다.)
$dto = $this->service->createDTO($this->request->getPost());
return $this->service->create($dto);
}
protected function create_result_process(CommonEntity $entity): string|RedirectResponse
{
return $this->action_modal_process("{$this->getTitle()}에서 {$entity->getTitle()} 생성이 완료되었습니다.");
}
// --- 수정 (Modify) ---
protected function modify_form_process($uid): CommonEntity
{
if (!$uid) {
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
}
$entity = $this->service->getEntity($uid);
if (!$entity instanceof CommonEntity) {
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
}
return $entity;
}
protected function modify_form_result_process(string $action): string|RedirectResponse
{
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
protected function modify_process($uid): CommonEntity
{
// POST 데이터를 DTO 객체로 변환
$dto = $this->service->createDTO($this->request->getPost());
return $this->service->modify($uid, $dto);
}
protected function modify_result_process(CommonEntity $entity): string|RedirectResponse
{
return $this->action_modal_process("{$this->getTitle()}에서 {$entity->getTitle()} 수정이 완료되었습니다.");
}
// --- 삭제 (Delete) ---
protected function delete_process($uid): CommonEntity
{
return $this->service->delete($uid);
}
protected function delete_result_process(CommonEntity $entity): string|RedirectResponse
{
return $this->action_redirect_process('info', "{$this->getTitle()}에서 {$entity->getTitle()} 삭제가 완료되었습니다.");
}
// --- 상세보기 (View) ---
protected function view_process($uid): CommonEntity
{
if (!$uid) {
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
}
$entity = $this->service->getEntity($uid);
if (!$entity instanceof CommonEntity) {
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
}
return $entity;
}
protected function view_result_process(string $action): string
{
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
}