9
9
10
10
class IndexController extends Controller
11
11
{
12
-
12
+
13
13
public function Index (Request $ request , $ cp3 , $ cp4 )
14
14
{
15
15
$ results = DB ::table ('ctt ' )
@@ -25,18 +25,23 @@ public function Index(Request $request, $cp3, $cp4)
25
25
['ctt.cp3 ' , '= ' , $ cp3 ],
26
26
])->get ();
27
27
28
- return response ()->json ($ results );
28
+ $ data = [];
29
+ foreach ($ results as $ result ) {
30
+ $ data [] = $ this ->normalizeData ($ result );
31
+ }
32
+
33
+ return response ()->json ($ data );
29
34
}
30
-
31
-
35
+
36
+
32
37
public function Random ()
33
38
{
34
39
$ id = DB ::table ('ctt ' )
35
40
->select ('id ' )
36
41
->inRandomOrder ()
37
42
->first ();
38
43
39
- $ results = DB ::table ('ctt ' )
44
+ $ result = DB ::table ('ctt ' )
40
45
->leftJoin ('districts ' , 'ctt.district_id ' , '= ' , 'districts.district_id ' )
41
46
->leftJoin ('locations ' , 'ctt.location_id ' , '= ' , 'locations.location_id ' )
42
47
->leftJoin ('municipalities ' , function ($ join ) {
@@ -46,8 +51,52 @@ public function Random()
46
51
->select ('locations.name ' , 'districts.name AS district ' , 'municipalities.name AS municipality ' , 'ctt.* ' )
47
52
->where ('ctt.id ' , $ id ->id )->first ();
48
53
49
- return response ()->json ($ results );
54
+ return response ()->json ($ this -> normalizeData ( $ result ) );
50
55
}
51
-
52
- }
53
56
57
+
58
+ protected function normalizeData ($ result )
59
+ {
60
+ $ fullAddress = [];
61
+ if (!empty (trim ($ result ->art_tipo ))) {
62
+ $ fullAddress [] = trim ($ result ->art_tipo );
63
+ }
64
+
65
+ if (!empty (trim ($ result ->pri_prep ))) {
66
+ $ fullAddress [] = trim ($ result ->pri_prep );
67
+ }
68
+
69
+ if (!empty (trim ($ result ->art_titulo ))) {
70
+ $ fullAddress [] = trim ($ result ->art_titulo );
71
+ }
72
+
73
+ if (!empty (trim ($ result ->seg_prep ))) {
74
+ $ fullAddress [] = trim ($ result ->seg_prep );
75
+ }
76
+
77
+ if (!empty (trim ($ result ->art_desig ))) {
78
+ $ fullAddress [] = trim ($ result ->art_desig );
79
+ }
80
+
81
+ if (!empty (trim ($ result ->porta ))) {
82
+ $ fullAddress [] = ', ' .trim ($ result ->porta );
83
+ }
84
+
85
+ return [
86
+ 'locationName ' => trim ($ result ->name ),
87
+ 'fullAddress ' => implode (' ' , $ fullAddress ),
88
+ 'zone ' => trim ($ result ->art_local ),
89
+ 'section ' => trim ($ result ->troco ),
90
+ 'doorNumber ' => trim ($ result ->porta ),
91
+ 'clientName ' => trim ($ result ->cliente ),
92
+ 'cp4 ' => trim ($ result ->cp4 ),
93
+ 'cp3 ' => trim ($ result ->cp3 ),
94
+ 'cpalf ' => trim ($ result ->cpalf ),
95
+ 'districtName ' => trim ($ result ->district ),
96
+ 'municipalityName ' => trim ($ result ->municipality ),
97
+ 'latitude ' => $ result ->latitude ,
98
+ 'longitude ' => $ result ->longitude ,
99
+ ];
100
+ }
101
+
102
+ }
0 commit comments