123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Raharja\Tips;
- use App\Model\TipsLog;
- use App\Model\PointLog;
- use App\Model\Raharja\TipsLikes;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Helper;
- use Request;
- use Validator;
-
- class TipsController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = Tips::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 = Tips::where('id', Helper::hash($data_id, 'decode'))->get();
-
- $query = array_map(function ($item) {
- $item['likes'] = TipsLikes::where('tips_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'],
- 'tips_id' => ['required'],
- 'point' => ['required'],
- ]);
-
- if(!$validator->fails()){
- $tips_logs = new TipsLog;
- $tips_logs->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $tips_logs->tips_id = Helper::hash(Request::all()['tips_id'], 'decode');
- $tips_logs->point = Request::all()['point'];
- $tips_logs->save();
- $tips_logs->user_id = Request::all()['user_id'];
- $tips_logs->tips_id = Request::all()['tips_id'];
- $tips_logs->hash_id = Helper::hash($tips_logs->id, 'encode');
-
- $total = Request::all()['point'];
-
- $point = new PointLog();
- $point->user_id = Helper::hash($tips_logs->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 ($tips_logs && $point) {
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
- $result = [
- 'tips_logs' => $tips_logs,
- '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_tips = Tips::where('id', Helper::hash($data_id, 'decode'))->first();
-
- if($arr_tips){
- $get_tips_by_user = TipsLikes::where('tips_id', $arr_tips->id)->where('user_id', 123456)->first();
-
- if($get_tips_by_user){
- $query = $get_tips_by_user->delete();
- }else {
- $query = TipsLikes::create([
- 'tips_id' => $arr_tips->id,
- 'user_id' => 123456,
- ]);
- }
-
- $result = $query;
-
- $res_status = true;
- $msg = 'Berasil likes atau unlikes tips';
- $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);
- }
- }
-
- }
|