Bez popisu
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.

TipsController.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Model\Raharja\Tips;
  6. use App\Model\TipsLog;
  7. use App\Model\PointLog;
  8. use App\Model\Raharja\TipsLikes;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\Auth;
  11. use Helper;
  12. use Request;
  13. use Validator;
  14. class TipsController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. DB::enableQueryLog();
  19. }
  20. public function getAll()
  21. {
  22. $query = Tips::where('date_publish', '>=', Carbon::now()->format('Y-m-d'))
  23. ->get();
  24. $query = array_map(function ($item) {
  25. $value['thumbnail'] = $item['thumbnail'];
  26. $value['title'] = $item['title'];
  27. $value['publish_date'] = $item['date_publish'];
  28. return $value;
  29. }, $query->toArray());
  30. if($query){
  31. $result = $query;
  32. $res_status = true;
  33. $msg = 'Mendapatkan Data';
  34. $status_msg = $msg;
  35. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  36. }else {
  37. $res_status = false;
  38. $msg = 'Data tidak ditemukan';
  39. $status_msg = $msg;
  40. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  41. }
  42. }
  43. public function getSingle($data_id)
  44. {
  45. $query = Tips::where('id', Helper::hash($data_id, 'decode'))->get();
  46. $query = array_map(function ($item) {
  47. $item['likes'] = TipsLikes::where('tips_id', $item['id'])->count();
  48. return $item;
  49. }, $query->toArray());
  50. if($query){
  51. $result = $query;
  52. $res_status = true;
  53. $msg = 'Mendapatkan Data';
  54. $status_msg = $msg;
  55. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  56. }else {
  57. $res_status = false;
  58. $msg = 'Data tidak ditemukan';
  59. $status_msg = $msg;
  60. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  61. }
  62. }
  63. public function postLog(Request $request)
  64. {
  65. $validator = Validator::make(Request::all(), [
  66. 'user_id' => ['required'],
  67. 'tips_id' => ['required'],
  68. 'point' => ['required'],
  69. ]);
  70. if(!$validator->fails()){
  71. $tips_logs = new TipsLog;
  72. $tips_logs->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  73. $tips_logs->tips_id = Helper::hash(Request::all()['tips_id'], 'decode');
  74. $tips_logs->point = Request::all()['point'];
  75. $tips_logs->save();
  76. $tips_logs->user_id = Request::all()['user_id'];
  77. $tips_logs->tips_id = Request::all()['tips_id'];
  78. $tips_logs->hash_id = Helper::hash($tips_logs->id, 'encode');
  79. $total = Request::all()['point'];
  80. $point = new PointLog();
  81. $point->user_id = Helper::hash($tips_logs->user_id, 'decode');
  82. $point->type_point = 'reading_news';
  83. $point->point = $total;
  84. $point->save();
  85. $point->user_id = Helper::hash($point->user_id, 'encode');
  86. $point->hash_id = Helper::hash($point->id, 'encode');
  87. if ($tips_logs && $point) {
  88. $res_status = true;
  89. $msg = 'Mendapatkan Data';
  90. $status_msg = $msg;
  91. $result = [
  92. 'tips_logs' => $tips_logs,
  93. 'point' => $point
  94. ];
  95. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  96. }else{
  97. $res_status = false;
  98. $msg = 'Data tidak ditemukan';
  99. $status_msg = $msg;
  100. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  101. }
  102. }else {
  103. $res_status = false;
  104. $msg = 'Error';
  105. $status_msg = $validator->errors();
  106. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  107. }
  108. }
  109. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  110. {
  111. $response['response'] = array(
  112. 'status' => $status,
  113. 'message' => $msg,
  114. 'status_msg' => $status_msg,
  115. );
  116. $response['param'] = !empty($param) ? $param : '';
  117. $response['results'] = $result;
  118. return response()->json($response, 200);
  119. }
  120. public function postLike($data_id){
  121. $arr_tips = Tips::where('id', Helper::hash($data_id, 'decode'))->first();
  122. if($arr_tips){
  123. $get_tips_by_user = TipsLikes::where('tips_id', $arr_tips->id)->where('user_id', 123456)->first();
  124. if($get_tips_by_user){
  125. $query = $get_tips_by_user->delete();
  126. }else {
  127. $query = TipsLikes::create([
  128. 'tips_id' => $arr_tips->id,
  129. 'user_id' => 123456,
  130. ]);
  131. }
  132. $result = $query;
  133. $res_status = true;
  134. $msg = 'Berasil likes atau unlikes tips';
  135. $status_msg = $msg;
  136. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  137. }else {
  138. $res_status = false;
  139. $msg = 'Data tidak ditemukan';
  140. $status_msg = $msg;
  141. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  142. }
  143. }
  144. }