Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageController.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Helpers\Helper;
  4. use App\Http\Controllers\Controller;
  5. use App\Model\Raharja\Message;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Model\Raharja\UserReceive;
  8. use Illuminate\Support\Facades\Auth;
  9. class MessageController extends Controller
  10. {
  11. public function __construct()
  12. {
  13. DB::enableQueryLog();
  14. }
  15. public function getAll()
  16. {
  17. $query = UserReceive::get();
  18. $arr_id = collect($query)->pluck('id')->toArray();
  19. $query = array_map(function ($item)use ($arr_id) {
  20. $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
  21. $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
  22. $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
  23. $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
  24. return $item;
  25. }, $query->toArray());
  26. if($query){
  27. $result = $query;
  28. $res_status = true;
  29. $msg = 'Mendapatkan Data';
  30. $status_msg = $msg;
  31. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  32. }else {
  33. $res_status = false;
  34. $msg = 'Data tidak ditemukan';
  35. $status_msg = $msg;
  36. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  37. }
  38. }
  39. public function getSingle($data_id)
  40. {
  41. $query = UserReceive::where('message_id', Helper::hash($data_id, 'decode'))->get();
  42. $arr_id = collect($query)->pluck('message_id')->toArray();
  43. $query = array_map(function ($item)use ($arr_id) {
  44. $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
  45. $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
  46. $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
  47. $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
  48. $item['is_check'] = $item['is_check'];
  49. return $item;
  50. }, $query->toArray());
  51. if($query){
  52. $result = $query;
  53. $res_status = true;
  54. $msg = 'Mendapatkan Data';
  55. $status_msg = $msg;
  56. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  57. }else {
  58. $res_status = false;
  59. $msg = 'Data tidak ditemukan';
  60. $status_msg = $msg;
  61. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  62. }
  63. }
  64. public function userViewMessage($message_id, $user_id)
  65. {
  66. $query = UserReceive::where('message_id', $message_id)
  67. ->where('user_id', $user_id)
  68. ->get();
  69. $update_status = $query->first()->update(['is_check' => 1]);
  70. $arr_id = collect($query)->pluck('message_id')->toArray();
  71. $query = array_map(function ($item)use ($arr_id) {
  72. $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
  73. $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
  74. $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
  75. $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
  76. $item['is_check'] = $item['is_check'];
  77. return $item;
  78. }, $query->toArray());
  79. if($query && $update_status){
  80. $result = $query;
  81. $res_status = true;
  82. $msg = 'Mendapatkan Data';
  83. $status_msg = $msg;
  84. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  85. }else {
  86. $res_status = false;
  87. $msg = 'Data tidak ditemukan';
  88. $status_msg = $msg;
  89. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  90. }
  91. }
  92. public function countUserMessage($data_id)
  93. {
  94. $query = UserReceive::where('user_id', $data_id)
  95. ->where('is_check', 0)
  96. ->count();
  97. if($query){
  98. $result = $query;
  99. $res_status = true;
  100. $msg = 'Mendapatkan Data';
  101. $status_msg = $msg;
  102. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  103. }else {
  104. $res_status = false;
  105. $msg = 'Data tidak ditemukan';
  106. $status_msg = $msg;
  107. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  108. }
  109. }
  110. public function getByUser($user_id)
  111. {
  112. $query = Message::whereHas('userReceive', function($item) use ($user_id) {
  113. $item->where('user_id', Helper::hash($user_id, 'decode'));
  114. })->get();
  115. if($query){
  116. $result = $query;
  117. $res_status = true;
  118. $msg = 'Mendapatkan Data';
  119. $status_msg = $msg;
  120. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  121. }else {
  122. $res_status = false;
  123. $msg = 'Data tidak ditemukan';
  124. $status_msg = $msg;
  125. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  126. }
  127. }
  128. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  129. {
  130. $response['response'] = array(
  131. 'status' => $status,
  132. 'message' => $msg,
  133. 'status_msg' => $status_msg,
  134. );
  135. $response['param'] = !empty($param) ? $param : '';
  136. $response['results'] = $result;
  137. return response()->json($response, 200);
  138. }
  139. }