From 82256cf437a0734c7b7590420a987f60a3359b8b Mon Sep 17 00:00:00 2001 From: Helder Correia Date: Fri, 1 Jun 2018 23:36:00 +0100 Subject: [PATCH 1/3] Add output info --- resources/views/index.blade.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 900fad8..905893c 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -15,6 +15,9 @@ padding: 3rem 1.5rem; text-align: center; } + .label { + color:#145222 + } @@ -56,6 +59,23 @@ } ]

+

2.1 Labels (portuguese only)

+

+ name Nome da localidade
+ art_cod Código da Artéria
+ art_tipo Artéria - Tipo (Rua, Praça, etc)
+ pri_prep Primeira preposição
+ art_titulo Artéria - Titulo (Doutor, Eng.º, Professor, etc)
+ seg_prep Segunda preposição
+ art_desig Artéria - Designação
+ art_local Artéria - Informação do Local/Zona
+ troco Descrição do troço
+ porta Número da porta do cliente
+ cliente Nome do cliente
+ cp4 N.º do código postal
+ cp3 Extensão do n.º do código postal
+ cpalf Designação Postal +

3. Random ZipCode

If you need data from a random zipcode:

{{$host}}/api/v1/random

From f935aea804db7faf47a8a2cc0c861215a7aa52b7 Mon Sep 17 00:00:00 2001 From: Helder Correia Date: Sat, 2 Jun 2018 11:44:16 +0100 Subject: [PATCH 2/3] Normalize output --- .../Controllers/ApiV1/IndexController.php | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ApiV1/IndexController.php b/app/Http/Controllers/ApiV1/IndexController.php index ed61362..67b216f 100644 --- a/app/Http/Controllers/ApiV1/IndexController.php +++ b/app/Http/Controllers/ApiV1/IndexController.php @@ -25,7 +25,14 @@ public function Index(Request $request, $cp3, $cp4) ['ctt.cp3', '=', $cp3], ])->get(); - return response()->json($results); + + + $data = []; + foreach($results as $result) { + $data[] = $this->normalizeData($result); + } + + return response()->json($data); } @@ -36,7 +43,7 @@ public function Random() ->inRandomOrder() ->first(); - $results = DB::table('ctt') + $result = DB::table('ctt') ->leftJoin('districts', 'ctt.district_id', '=', 'districts.district_id') ->leftJoin('locations', 'ctt.location_id', '=', 'locations.location_id') ->leftJoin('municipalities', function ($join) { @@ -46,7 +53,51 @@ public function Random() ->select('locations.name', 'districts.name AS district', 'municipalities.name AS municipality', 'ctt.*') ->where('ctt.id', $id->id)->first(); - return response()->json($results); + return response()->json($this->normalizeData($result)); + } + + protected function normalizeData($result) + { + $fullAddress = []; + if(!empty(trim($result->art_tipo))) { + $fullAddress[] = trim($result->art_tipo); + } + + if(!empty(trim($result->pri_prep))) { + $fullAddress[] = trim($result->pri_prep); + } + + if(!empty(trim($result->art_titulo))) { + $fullAddress[] = trim($result->art_titulo); + } + + if(!empty(trim($result->seg_prep))) { + $fullAddress[] = trim($result->seg_prep); + } + + if(!empty(trim($result->art_desig))) { + $fullAddress[] = trim($result->art_desig); + } + + if(!empty(trim($result->porta))) { + $fullAddress[] = trim($result->porta); + } + + return [ + 'locationName' => trim($result->name), + 'fullAddress' => implode(' ', $fullAddress), + 'zone' => trim($result->art_local), + 'section' => trim($result->troco), + 'doorNumber' => trim($result->porta), + 'clientName' => trim($result->cliente), + 'cp4' => trim($result->cp4), + 'cp3' => trim($result->cp3), + 'cpalf' => trim($result->cpalf), + 'districtName' => trim($result->district), + 'municipalityName' => trim($result->municipality), + 'latitude' => $result->latitude, + 'longitude' => $result->longitude, + ]; } } From fccdd2da2af587638f9cb82182575c5695053451 Mon Sep 17 00:00:00 2001 From: Helder Correia Date: Sat, 2 Jun 2018 18:49:57 +0100 Subject: [PATCH 3/3] Update documentation --- app/Console/Commands/GpsCommand.php | 70 ------------------- .../Controllers/ApiV1/IndexController.php | 16 ++--- app/Http/Controllers/ExampleController.php | 18 ----- app/Http/Middleware/ExampleMiddleware.php | 20 ------ resources/views/index.blade.php | 42 +++-------- 5 files changed, 15 insertions(+), 151 deletions(-) delete mode 100644 app/Console/Commands/GpsCommand.php delete mode 100644 app/Http/Controllers/ExampleController.php delete mode 100644 app/Http/Middleware/ExampleMiddleware.php diff --git a/app/Console/Commands/GpsCommand.php b/app/Console/Commands/GpsCommand.php deleted file mode 100644 index a33ac1d..0000000 --- a/app/Console/Commands/GpsCommand.php +++ /dev/null @@ -1,70 +0,0 @@ -select("SELECT DISTINCT cp4, cp3 FROM ctt where district_id = 8 AND latitude IS NULL ORDER BY cp3;"); - - $client = new \GuzzleHttp\Client(); - foreach($results as $result) { - $url = sprintf($gpsUrl, $result->cp4, $result->cp3); - $res = $client->request('GET', $url); - $body = (string)$res->getBody(); - $dom = HtmlDomParser::str_get_html($body); - $gps = $dom->find('span[class="pull-right gps"]',0)->innertext; - if($gps) { - $gps = trim(str_replace('GPS:','',$gps)); - $parts = explode(',',$gps); - if(!is_array($parts) || count($parts) != 2){ - continue; - } - list($lat, $long) = $parts; - $lat = floatval($lat); - $long = floatval($long); - DB::table('ctt')->where([ - ['cp4', '=', $result->cp4], - ['cp3', '=', $result->cp3], - ])->update(['latitude' => $lat,'longitude' => $long]); - echo($result->cp4.'-'.$result->cp3.' -> '.$lat.'/'.$long."\n"); - - } else { - echo($result->cp4.'-'.$result->cp3." -> \n"); - } - - sleep(1); - - } - } - -} diff --git a/app/Http/Controllers/ApiV1/IndexController.php b/app/Http/Controllers/ApiV1/IndexController.php index 67b216f..2f2a0ea 100644 --- a/app/Http/Controllers/ApiV1/IndexController.php +++ b/app/Http/Controllers/ApiV1/IndexController.php @@ -9,7 +9,7 @@ class IndexController extends Controller { - + public function Index(Request $request, $cp3, $cp4) { $results = DB::table('ctt') @@ -25,8 +25,6 @@ public function Index(Request $request, $cp3, $cp4) ['ctt.cp3', '=', $cp3], ])->get(); - - $data = []; foreach($results as $result) { $data[] = $this->normalizeData($result); @@ -34,8 +32,8 @@ public function Index(Request $request, $cp3, $cp4) return response()->json($data); } - - + + public function Random() { $id = DB::table('ctt') @@ -55,7 +53,8 @@ public function Random() return response()->json($this->normalizeData($result)); } - + + protected function normalizeData($result) { $fullAddress = []; @@ -80,7 +79,7 @@ protected function normalizeData($result) } if(!empty(trim($result->porta))) { - $fullAddress[] = trim($result->porta); + $fullAddress[] = ', '.trim($result->porta); } return [ @@ -99,6 +98,5 @@ protected function normalizeData($result) 'longitude' => $result->longitude, ]; } - -} +} diff --git a/app/Http/Controllers/ExampleController.php b/app/Http/Controllers/ExampleController.php deleted file mode 100644 index de8f6da..0000000 --- a/app/Http/Controllers/ExampleController.php +++ /dev/null @@ -1,18 +0,0 @@ -
 [
   {
-    "name": "Lisboa",
-    "district": "Lisboa",
-    "municipality": "Lisboa",
-    "id": 136883,
-    "location_id": 21696,
-    "municipality_id": 6,
-    "district_id": 11,
-    "art_cod": "300460611",
-    "art_tipo": "Rossio",
-    "pri_prep": "dos",
-    "art_titulo": "",
-    "seg_prep": "",
-    "art_desig": "Olivais",
-    "art_local": "",
-    "troco": "",
-    "porta": "",
-    "cliente": "",
+    "locationName": "Lisboa",
+    "fullAddress": "Rossio dos Olivais",
+    "zone": "",
+    "section": "",
+    "doorNumber": "",
+    "clientName": "",
     "cp4": "1990",
     "cp3": "231",
     "cpalf": "LISBOA",
+    "districtName": "Lisboa",
+    "municipalityName": "Lisboa",
     "latitude": null,
     "longitude": null
   }
 ]
 

-

2.1 Labels (portuguese only)

-

- name Nome da localidade
- art_cod Código da Artéria
- art_tipo Artéria - Tipo (Rua, Praça, etc)
- pri_prep Primeira preposição
- art_titulo Artéria - Titulo (Doutor, Eng.º, Professor, etc)
- seg_prep Segunda preposição
- art_desig Artéria - Designação
- art_local Artéria - Informação do Local/Zona
- troco Descrição do troço
- porta Número da porta do cliente
- cliente Nome do cliente
- cp4 N.º do código postal
- cp3 Extensão do n.º do código postal
- cpalf Designação Postal -

3. Random ZipCode

If you need data from a random zipcode:

{{$host}}/api/v1/random