public function data(): ResponseInterface
{
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
$perPage = max(5, min(50, (int) ($this->request->getGet('per_page') ?? 10)));
$search = trim($this->request->getGet('q') ?? '');
$sort = $this->request->getGet('sort') ?? 'id';
$dir = $this->request->getGet('dir') ?? 'desc';
// 허용 컬럼만 정렬 (SQL Injection 방지)
$allowed = ['id', 'title', 'author', 'views', 'created_at'];
if (! in_array($sort, $allowed, true)) $sort = 'id';
$dir = $dir === 'asc' ? 'asc' : 'desc';
$model = new PostModel();
if ($search !== '') {
$model->groupStart()
->like('title', $search)
->orLike('author', $search)
->groupEnd();
}
$model->orderBy($sort, $dir);
$posts = $model->paginate($perPage, 'default', $page);
$pager = $model->pager;
return $this->response->setJSON([
'data' => $rows, // 현재 페이지 행
'total' => $pager->getTotal(),
'page' => $pager->getCurrentPage(),
'per_page' => $pager->getPerPage(),
'last_page' => $pager->getPageCount(),
'from' => ($page-1)*$perPage + 1,
'to' => min($page*$perPage, $total),
]);
}