FavoriteController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Controllers\Admin\Sms;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\SmsFavorite;
  6. use App\Models\DTO\SearchData;
  7. class FavoriteController extends Controller
  8. {
  9. private SmsFavorite $smsFavoriteModel;
  10. public function __construct(SmsFavorite $smsFavorite)
  11. {
  12. $this->smsFavoriteModel = $smsFavorite;
  13. }
  14. /**
  15. * SMS > 자주보내는 문자
  16. * @method GET
  17. * @see /admin/sms/favorite
  18. */
  19. public function index(Request $request)
  20. {
  21. $params = SearchData::fromRequest($request);
  22. $smsFavoriteData = $this->smsFavoriteModel->data($params);
  23. if ($smsFavoriteData->rows > 0) {
  24. $num = listNum($smsFavoriteData->total, $params->page, $params->perPage);
  25. foreach ($smsFavoriteData->list as $i => $row) {
  26. $row->num = $num--;
  27. $row->editURL = route('admin.sms.favorite.edit', $row->id);
  28. $smsFavoriteData->list[$i] = $row;
  29. }
  30. }
  31. return view('admin.sms.favorite.index', [
  32. 'smsFavoriteData' => $smsFavoriteData,
  33. 'params' => $params
  34. ]);
  35. }
  36. /**
  37. * 자주 보내는 문자 등록
  38. * @method GET
  39. * @see /admin/sms/favorite/create
  40. */
  41. public function create()
  42. {
  43. return view('admin.sms.favorite.write', [
  44. 'actionURL' => route('admin.sms.favorite.store'),
  45. 'smsFavoriteData' => [],
  46. 'favoriteID' => null
  47. ]);
  48. }
  49. /**
  50. * 자주 보내는 문자 수정
  51. * @method GET
  52. * @see /admin/sms/favorite/{pk}/edit
  53. */
  54. public function edit(int $favoriteID)
  55. {
  56. return view('admin.sms.favorite.write', [
  57. 'actionURL' => route('admin.sms.favorite.update', $favoriteID),
  58. 'smsFavoriteData' => $this->smsFavoriteModel->find($favoriteID),
  59. 'favoriteID' => $favoriteID
  60. ]);
  61. }
  62. /**
  63. * 자주 보내는 문자 등록 저장
  64. * @method POST
  65. * @see /admin/sms/favorite
  66. */
  67. public function store(Request $request)
  68. {
  69. $rules = [
  70. 'type' => 'required|numeric|in:1,2',
  71. 'subject' => 'required|string|max:255',
  72. 'content' => 'required|string'
  73. ];
  74. $attributes = [
  75. 'type' => '형식',
  76. 'subject' => '제목',
  77. 'content' => '내용'
  78. ];
  79. $posts = $this->validate($request, $rules, [], $attributes);
  80. $this->smsFavoriteModel->insert([
  81. 'type' => $posts['type'],
  82. 'subject' => $posts['subject'],
  83. 'content' => $posts['content'],
  84. 'created_at' => now()
  85. ]);
  86. $message = '자주 보내는 문자가 등록되었습니다.';
  87. return redirect()->route('admin.sms.favorite.index')->with('message', $message);
  88. }
  89. /**
  90. * 자주 보내는 문자 수정 저장
  91. * @method PUT
  92. * @see /admin/sms/favorite
  93. */
  94. public function update(Request $request)
  95. {
  96. $rules = [
  97. 'favorite_id' => 'required|numeric|exists:tb_sms_favorite,id',
  98. 'type' => 'required|numeric|in:1,2',
  99. 'subject' => 'required|string|max:255',
  100. 'content' => 'required|string'
  101. ];
  102. $attributes = [
  103. 'favorite_id' => '번호',
  104. 'type' => '형식',
  105. 'subject' => '제목',
  106. 'content' => '내용'
  107. ];
  108. $posts = $this->validate($request, $rules, [], $attributes);
  109. $this->smsFavoriteModel->find($posts['favorite_id'])->update([
  110. 'type' => $posts['type'],
  111. 'subject' => $posts['subject'],
  112. 'content' => $posts['content'],
  113. 'updated_at' => now()
  114. ]);
  115. $message = '자주 보내는 문자가 수정되었습니다.';
  116. return redirect()->route('admin.sms.favorite.edit', $posts['favorite_id'])->with('message', $message);
  117. }
  118. /**
  119. * 자주 보내는 문자 삭제
  120. * @method DELETE
  121. * @see /admin/sms/favorite
  122. */
  123. public function destroy(Request $request)
  124. {
  125. $chk = $request->post('chk');
  126. if ($chk) {
  127. foreach ($chk as $favoriteID) {
  128. $this->smsFavoriteModel->find($favoriteID)->delete();
  129. }
  130. }
  131. $message = '자주 보내는 문자 정보가 삭제되었습니다.';
  132. return redirect()->route('admin.sms.favorite.index')->with('message', $message);
  133. }
  134. }