Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\News;
  6. use App\Model\NewsLog;
  7. use App\Model\PointLog;
  8. use App\Model\Raharja\NewsLikes;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\Auth;
  11. use Helper;
  12. use Request;
  13. use Validator;
  14. class NewsController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. DB::enableQueryLog();
  19. }
  20. public function getAll()
  21. {
  22. $query = News::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 = News::where('id', Helper::hash($data_id, 'decode'))->get();
  46. $query = array_map(function ($item) {
  47. $item['likes'] = NewsLikes::where('news_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. 'news_id' => ['required'],
  68. 'point' => ['required'],
  69. ]);
  70. if(!$validator->fails()){
  71. $news_log = new NewsLog;
  72. $news_log->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  73. $news_log->news_id = Helper::hash(Request::all()['news_id'], 'decode');
  74. $news_log->point = Request::all()['point'];
  75. $news_log->save();
  76. $news_log->user_id = Request::all()['user_id'];
  77. $news_log->news_id = Request::all()['news_id'];
  78. $news_log->hash_id = Helper::hash($news_log->id, 'encode');
  79. $total = Request::all()['point'];
  80. $point = new PointLog();
  81. $point->user_id = Helper::hash($news_log->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 ($news_log && $point) {
  88. $res_status = true;
  89. $msg = 'Mendapatkan Data';
  90. $status_msg = $msg;
  91. $result = [
  92. 'news_log' => $news_log,
  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_news = News::where('id', Helper::hash($data_id, 'decode'))->first();
  122. if($arr_news){
  123. $get_news_by_user = NewsLikes::where('news_id', $arr_news->id)->where('user_id', 123456)->first();
  124. if($get_news_by_user){
  125. $query = $get_news_by_user->delete();
  126. }else {
  127. $query = NewsLikes::create([
  128. 'news_id' => $arr_news->id,
  129. 'user_id' => 123456,
  130. ]);
  131. }
  132. $result = $query;
  133. $res_status = true;
  134. $msg = 'Berasil likes atau unlikes news';
  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. }