123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Helpers\Helper;
- use App\Http\Controllers\Controller;
- use App\Model\Raharja\Message;
- use Illuminate\Support\Facades\DB;
- use App\Model\Raharja\UserReceive;
- use Illuminate\Support\Facades\Auth;
-
- class MessageController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = UserReceive::get();
- $arr_id = collect($query)->pluck('id')->toArray();
- $query = array_map(function ($item)use ($arr_id) {
- $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
- $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
- $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
- $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
- public function getSingle($data_id)
- {
- $query = UserReceive::where('message_id', Helper::hash($data_id, 'decode'))->get();
- $arr_id = collect($query)->pluck('message_id')->toArray();
- $query = array_map(function ($item)use ($arr_id) {
- $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
- $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
- $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
- $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
- $item['is_check'] = $item['is_check'];
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
- public function userViewMessage($message_id, $user_id)
- {
- $query = UserReceive::where('message_id', $message_id)
- ->where('user_id', $user_id)
- ->get();
- $update_status = $query->first()->update(['is_check' => 1]);
-
- $arr_id = collect($query)->pluck('message_id')->toArray();
- $query = array_map(function ($item)use ($arr_id) {
- $arr_message = Message::whereIn('id', $arr_id)->with('categoryMessage')->get();
- $item['title'] = $arr_message->where('id', $item['message_id'])->first()['title'];
- $item['content'] = $arr_message->where('id', $item['message_id'])->first()['content'];
- $item['category'] = $arr_message->where('id', $item['message_id'])->first()['categoryMessage']['name'];
- $item['is_check'] = $item['is_check'];
- return $item;
- }, $query->toArray());
-
-
- if($query && $update_status){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
- public function countUserMessage($data_id)
- {
- $query = UserReceive::where('user_id', $data_id)
- ->where('is_check', 0)
- ->count();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getByUser($user_id)
- {
- $query = Message::whereHas('userReceive', function($item) use ($user_id) {
- $item->where('user_id', Helper::hash($user_id, 'decode'));
- })->get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
-
-
- public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
- {
- $response['response'] = array(
- 'status' => $status,
- 'message' => $msg,
- 'status_msg' => $status_msg,
- );
- $response['param'] = !empty($param) ? $param : '';
-
- $response['results'] = $result;
-
- return response()->json($response, 200);
- }
-
- }
|