IrController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class IrController extends BaseApiController
  5. {
  6. /**
  7. * Get IR list (Public)
  8. */
  9. public function index()
  10. {
  11. $params = $this->getPaginationParams();
  12. $builder = $this->getDB()->table('ir');
  13. // Search
  14. $searchType = $this->request->getGet('search_type');
  15. $searchKeyword = $this->request->getGet('search_keyword');
  16. if ($searchType && $searchKeyword) {
  17. if ($searchType === 'title') {
  18. $builder->like('title', $searchKeyword);
  19. } elseif ($searchType === 'name') {
  20. $builder->like('name', $searchKeyword);
  21. } elseif ($searchType === 'content') {
  22. $builder->like('content', $searchKeyword);
  23. }
  24. }
  25. $builder->orderBy('is_notice', 'DESC');
  26. $builder->orderBy('id', 'DESC');
  27. $result = $this->paginatedResponse($builder, $params);
  28. return $this->respondSuccess($result);
  29. }
  30. /**
  31. * Get single IR (Public)
  32. */
  33. public function show($id = null)
  34. {
  35. $builder = $this->getDB()->table('ir');
  36. $ir = $builder->where('id', $id)->get()->getRow();
  37. if (!$ir) {
  38. return $this->respondError('IR 자료를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  39. }
  40. // Parse file_urls JSON
  41. $ir->file_urls = json_decode($ir->file_urls ?? '[]');
  42. // Increment view count
  43. $builder->where('id', $id)->set('views', 'views + 1', false)->update();
  44. return $this->respondSuccess($ir);
  45. }
  46. /**
  47. * Create IR
  48. */
  49. public function create()
  50. {
  51. $auth = $this->requireAuth();
  52. if ($auth instanceof ResponseInterface) {
  53. return $auth;
  54. }
  55. $json = $this->request->getJSON();
  56. $data = [
  57. 'allow_comment' => $json->allow_comment ?? 0,
  58. 'is_notice' => $json->is_notice ?? 0,
  59. 'name' => $json->name ?? '',
  60. 'email' => $json->email ?? '',
  61. 'url' => $json->url ?? '',
  62. 'title' => $json->title ?? '',
  63. 'content' => $json->content ?? '',
  64. 'file_urls' => json_encode($json->file_urls ?? []),
  65. 'views' => 0,
  66. 'created_at' => date('Y-m-d H:i:s')
  67. ];
  68. $builder = $this->getDB()->table('ir');
  69. $builder->insert($data);
  70. return $this->respondSuccess(['id' => $this->getDB()->insertID()], 'IR 자료가 등록되었습니다.');
  71. }
  72. /**
  73. * Update IR
  74. */
  75. public function update($id = null)
  76. {
  77. $auth = $this->requireAuth();
  78. if ($auth instanceof ResponseInterface) {
  79. return $auth;
  80. }
  81. $json = $this->request->getJSON();
  82. $data = [
  83. 'allow_comment' => $json->allow_comment ?? 0,
  84. 'is_notice' => $json->is_notice ?? 0,
  85. 'name' => $json->name ?? '',
  86. 'email' => $json->email ?? '',
  87. 'url' => $json->url ?? '',
  88. 'title' => $json->title ?? '',
  89. 'content' => $json->content ?? '',
  90. 'file_urls' => json_encode($json->file_urls ?? []),
  91. 'updated_at' => date('Y-m-d H:i:s')
  92. ];
  93. $builder = $this->getDB()->table('ir');
  94. $builder->where('id', $id)->update($data);
  95. return $this->respondSuccess(null, 'IR 자료가 수정되었습니다.');
  96. }
  97. /**
  98. * Delete IR
  99. */
  100. public function delete($id = null)
  101. {
  102. $auth = $this->requireAuth();
  103. if ($auth instanceof ResponseInterface) {
  104. return $auth;
  105. }
  106. $builder = $this->getDB()->table('ir');
  107. $builder->where('id', $id)->delete();
  108. return $this->respondSuccess(null, 'IR 자료가 삭제되었습니다.');
  109. }
  110. }