From 71deadce25178c85fff401c73f3b03a48df0acdc Mon Sep 17 00:00:00 2001 From: ibnu Date: Thu, 9 Feb 2023 16:01:04 +0700 Subject: [PATCH 1/2] update get location address when CICO --- app/Http/Controllers/Controller.php | 27 +++++++++++++++++++-- app/Http/Controllers/PresenceController.php | 19 +++++++++------ rest-client.http | 6 ++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index a1f786f..1ccbf9c 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; use Laravel\Lumen\Routing\Controller as BaseController; @@ -251,8 +252,30 @@ class Controller extends BaseController return $totalCost; } - private function getLoc($lat, $lng){ - $response = Http::get(config('api.nominatim') . "/reverse?lat=".$lat."&lon=".$lng."&format=json"); + protected function getLoc($lat, $lng){ + // $response = Http::get(config('api.nominatim') . "/reverse?lat=".$lat."&lon=".$lng."&format=json"); + // return $response->json; + + $url = config('api.nominatim') . "/reverse?lat=".$lat."&lon=".$lng."&format=json"; + // $token = config('api.adw_token'); + $response = $this->curlRequest($url); return $response; } + + private function curlRequest($url){ + $ch = curl_init(); + // $headers = [ + // 'Authorization: '.$token + // ]; + curl_setopt($ch, CURLOPT_URL, $url); + // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + + $response = curl_exec($ch); + if ($response === false) + $response = curl_error($ch); + curl_close($ch); + + return json_decode($response); + } } diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index 7fe5ae3..f7ea87b 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -18,6 +18,11 @@ class PresenceController extends Controller $this->validate($request, [ 'user_id' => 'required' ]); + + + // $test = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; + // // display_name + // return response()->json(["data"=>$test,'code'=>400], 400); $checkLocation = $this->checkLocation($request); $statusBoundary = false; @@ -28,8 +33,8 @@ class PresenceController extends Controller } if($request->type=="out"){ - // $clock_out_loc get data from api.nominatim url serach address payload (lat lot); - $clock_out_loc = "-"; + + $clock_out_loc = $this->getLoc($request->clock_out_lat, $request->clock_out_lng)->display_name; $dataUpdate = array( "clock_out"=>$request->clock_time, "clock_out_lat" => $request->clock_out_lat, @@ -67,10 +72,8 @@ class PresenceController extends Controller die(); } - $onlyDate = date_format($date,"Y-m-d"); - - // $clock_in_loc get data from api.nominatim url serach address payload (lat lot); - $clock_in_loc = "-"; + $onlyDate = date_format($date,"Y-m-d"); + $clock_in_loc = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; $dataAdd = array( 'user_id'=> $request->user_id, @@ -276,8 +279,8 @@ class PresenceController extends Controller $finalData = []; foreach($dataGet as $data){ - $responseIn = $this.getLoc($clock_in_lat, $clock_in_lng); - $responseOut = $this.getLoc($clock_in_lat, $clock_in_lng); + // $responseIn = $this->getLoc($clock_in_lat, $clock_in_lng); + // $responseOut = $this->getLoc($clock_in_lat, $clock_in_lng); $data->clock_in_loc = $responseIn->json()["display_name"]; $data->clock_out_loc = $responseOut->json()["display_name"]; } diff --git a/rest-client.http b/rest-client.http index dae46c8..214a772 100644 --- a/rest-client.http +++ b/rest-client.http @@ -804,10 +804,10 @@ Authorization: Bearer {{token}} content-type: application/json { - "clock_out_lat": -6.2622811, - "clock_out_lng": 106.7881746, + "clock_in_lat": -6.2622811, + "clock_in_lng": 106.7881746, "clock_time": "2023-02-06T16:40:17+07:00", - "type": "out", + "type": "in", "user_id": 1 } From 361ac26923b75ea45fa6f3ae0cfb6b8753b0b5b1 Mon Sep 17 00:00:00 2001 From: ibnu Date: Thu, 9 Feb 2023 16:29:14 +0700 Subject: [PATCH 2/2] update bulk location, add solving search, and update rest-client --- app/Http/Controllers/PresenceController.php | 25 +++++---------------- rest-client.http | 6 ++--- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index f7ea87b..6651154 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -18,11 +18,6 @@ class PresenceController extends Controller $this->validate($request, [ 'user_id' => 'required' ]); - - - // $test = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; - // // display_name - // return response()->json(["data"=>$test,'code'=>400], 400); $checkLocation = $this->checkLocation($request); $statusBoundary = false; @@ -276,14 +271,6 @@ class PresenceController extends Controller $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; $dataGet = $builder->get(); - $finalData = []; - - foreach($dataGet as $data){ - // $responseIn = $this->getLoc($clock_in_lat, $clock_in_lng); - // $responseOut = $this->getLoc($clock_in_lat, $clock_in_lng); - $data->clock_in_loc = $responseIn->json()["display_name"]; - $data->clock_out_loc = $responseOut->json()["display_name"]; - } $totalRecord = $countBuilder->count(); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); } @@ -310,17 +297,16 @@ class PresenceController extends Controller $clock_in_lat = $objRow->clock_in_lat; $clock_in_lng = $objRow->clock_in_lng; $objRow->clock_in_loc = ""; - if (isset($clock_in_lat) && isset($clock_in_lng)) { - $response = Http::get(config('api.nominatim') . "/reverse?lat=".$clock_in_lat."&lon=".$clock_in_lng."&format=json"); - $objRow->clock_in_loc = $response->json()["display_name"]; + if (isset($clock_in_lat) && isset($clock_in_lng)) { + $objRow->clock_in_loc = $this->getLoc($clock_in_lat, $clock_in_lng)->display_name; } $clock_out_lat = $objRow->clock_out_lat; $clock_out_lng = $objRow->clock_out_lng; $objRow->clock_out_loc = ""; - if (isset($clock_out_lat) && isset($clock_out_lng)) { - $response = Http::get(config('api.nominatim') . "/reverse?lat=".$clock_out_lat."&lon=".$clock_out_lng."&format=json"); - $objRow->clock_out_loc = $response->json()["display_name"]; + if (isset($clock_out_lat) && isset($clock_out_lng) && $clock_out_lng != null) { + $locAddress = $this->getLoc($clock_out_lat, $clock_out_lng); + $objRow->clock_out_loc = isset($locAddress->display_name) ? $locAddress->display_name : "-"; } @@ -328,5 +314,6 @@ class PresenceController extends Controller $presence->clock_out_loc = $objRow->clock_out_loc; $presence->save(); } + return response()->json(['status'=>'success','message'=>'success update!','code'=>200], 200); } } diff --git a/rest-client.http b/rest-client.http index 214a772..dae46c8 100644 --- a/rest-client.http +++ b/rest-client.http @@ -804,10 +804,10 @@ Authorization: Bearer {{token}} content-type: application/json { - "clock_in_lat": -6.2622811, - "clock_in_lng": 106.7881746, + "clock_out_lat": -6.2622811, + "clock_out_lng": 106.7881746, "clock_time": "2023-02-06T16:40:17+07:00", - "type": "in", + "type": "out", "user_id": 1 }