| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class IrController extends BaseApiController
- {
- /**
- * Get IR list (Public)
- */
- public function index()
- {
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('ir');
- // Search
- $searchType = $this->request->getGet('search_type');
- $searchKeyword = $this->request->getGet('search_keyword');
- if ($searchType && $searchKeyword) {
- if ($searchType === 'title') {
- $builder->like('title', $searchKeyword);
- } elseif ($searchType === 'name') {
- $builder->like('name', $searchKeyword);
- } elseif ($searchType === 'content') {
- $builder->like('content', $searchKeyword);
- }
- }
- $builder->orderBy('is_notice', 'DESC');
- $builder->orderBy('id', 'DESC');
- $result = $this->paginatedResponse($builder, $params);
- return $this->respondSuccess($result);
- }
- /**
- * Get single IR (Public)
- */
- public function show($id = null)
- {
- $builder = $this->getDB()->table('ir');
- $ir = $builder->where('id', $id)->get()->getRow();
- if (!$ir) {
- return $this->respondError('IR 자료를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Parse file_urls JSON
- $ir->file_urls = json_decode($ir->file_urls ?? '[]');
- // Increment view count
- $builder->where('id', $id)->set('views', 'views + 1', false)->update();
- return $this->respondSuccess($ir);
- }
- /**
- * Create IR
- */
- public function create()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- $data = [
- 'allow_comment' => $json->allow_comment ?? 0,
- 'is_notice' => $json->is_notice ?? 0,
- 'name' => $json->name ?? '',
- 'email' => $json->email ?? '',
- 'url' => $json->url ?? '',
- 'title' => $json->title ?? '',
- 'content' => $json->content ?? '',
- 'file_urls' => json_encode($json->file_urls ?? []),
- 'views' => 0,
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('ir');
- $builder->insert($data);
- return $this->respondSuccess(['id' => $this->getDB()->insertID()], 'IR 자료가 등록되었습니다.');
- }
- /**
- * Update IR
- */
- public function update($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- $data = [
- 'allow_comment' => $json->allow_comment ?? 0,
- 'is_notice' => $json->is_notice ?? 0,
- 'name' => $json->name ?? '',
- 'email' => $json->email ?? '',
- 'url' => $json->url ?? '',
- 'title' => $json->title ?? '',
- 'content' => $json->content ?? '',
- 'file_urls' => json_encode($json->file_urls ?? []),
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('ir');
- $builder->where('id', $id)->update($data);
- return $this->respondSuccess(null, 'IR 자료가 수정되었습니다.');
- }
- /**
- * Delete IR
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('ir');
- $builder->where('id', $id)->delete();
- return $this->respondSuccess(null, 'IR 자료가 삭제되었습니다.');
- }
- }
|