123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Raharja\News;
- use App\Model\NewsLog;
- use App\Model\PointLog;
- use App\Model\Raharja\NewsLikes;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Helper;
- use Request;
- use Validator;
-
- class NewsController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = News::where('date_publish', '>=', Carbon::now()->format('Y-m-d'))
- ->get();
- $query = array_map(function ($item) {
- $value['thumbnail'] = $item['thumbnail'];
- $value['title'] = $item['title'];
- $value['publish_date'] = $item['date_publish'];
- return $value;
- }, $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 = News::where('id', Helper::hash($data_id, 'decode'))->get();
- $query = array_map(function ($item) {
- $item['likes'] = NewsLikes::where('news_id', $item['id'])->count();
- 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 postLog(Request $request)
- {
- $validator = Validator::make(Request::all(), [
- 'user_id' => ['required'],
- 'news_id' => ['required'],
- 'point' => ['required'],
- ]);
-
- if(!$validator->fails()){
- $news_log = new NewsLog;
- $news_log->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $news_log->news_id = Helper::hash(Request::all()['news_id'], 'decode');
- $news_log->point = Request::all()['point'];
- $news_log->save();
- $news_log->user_id = Request::all()['user_id'];
- $news_log->news_id = Request::all()['news_id'];
- $news_log->hash_id = Helper::hash($news_log->id, 'encode');
-
- $total = Request::all()['point'];
-
- $point = new PointLog();
- $point->user_id = Helper::hash($news_log->user_id, 'decode');
- $point->type_point = 'reading_news';
- $point->point = $total;
- $point->save();
- $point->user_id = Helper::hash($point->user_id, 'encode');
- $point->hash_id = Helper::hash($point->id, 'encode');
-
- if ($news_log && $point) {
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
- $result = [
- 'news_log' => $news_log,
- 'point' => $point
- ];
-
- 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, $result);
- }
- }else {
-
- $res_status = false;
- $msg = 'Error';
- $status_msg = $validator->errors();
-
- 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);
- }
-
- public function postLike($data_id){
- $arr_news = News::where('id', Helper::hash($data_id, 'decode'))->first();
-
- if($arr_news){
- $get_news_by_user = NewsLikes::where('news_id', $arr_news->id)->where('user_id', 123456)->first();
-
- if($get_news_by_user){
- $query = $get_news_by_user->delete();
- }else {
- $query = NewsLikes::create([
- 'news_id' => $arr_news->id,
- 'user_id' => 123456,
- ]);
- }
-
- $result = $query;
-
- $res_status = true;
- $msg = 'Berasil likes atau unlikes news';
- $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);
- }
- }
-
- }
|