77
88class PedidosApiService
99{
10- protected $ baseUrl = 'http://localhost:8080/pedidos ' ;
11- protected $ productosUrl = 'http://localhost:8080/productos ' ;
10+ protected $ baseUrl ;
11+ protected $ productosUrl ;
12+ protected $ detallesUrl ;
13+
14+ public function __construct ()
15+ {
16+ // Usamos la variable de entorno, si no existe, usa la IP de tu instancia
17+ $ apiHost = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
18+
19+ $ this ->baseUrl = "{$ apiHost }/pedidos " ;
20+ $ this ->productosUrl = "{$ apiHost }/productos " ;
21+ $ this ->detallesUrl = "{$ apiHost }/detalles-pedidos " ;
22+ }
1223
1324 /* =========================
1425 PRODUCTOS
@@ -19,15 +30,12 @@ public function obtenerProductos()
1930 try {
2031 $ response = Http::get ($ this ->productosUrl );
2132 $ response ->throw ();
22-
2333 $ productosResponse = $ response ->json ();
2434
2535 if (isset ($ productosResponse ['content ' ]) && is_array ($ productosResponse ['content ' ])) {
2636 return $ productosResponse ['content ' ];
2737 }
28-
2938 return $ productosResponse ;
30-
3139 } catch (Exception $ e ) {
3240 throw new Exception ("Error al obtener productos de la API. " );
3341 }
@@ -68,27 +76,22 @@ public function crearPedido($data)
6876 }
6977 }
7078
71- public function actualizarPedido ($ id , $ data )
72- {
73- try {
74- // Agregamos logging para ver qué estamos enviando exactamente a Java
75- \Log::info ("Enviando actualización a API Java para Pedido # {$ id }: " , $ data );
76-
77- $ response = Http::put ("{$ this ->baseUrl }/ {$ id }" , $ data );
78-
79- if ($ response ->failed ()) {
80- // Esto imprimirá en storage/logs/laravel.log el error real de Java
81- \Log::error ("Error desde API Java (Pedido # {$ id }): " . $ response ->body ());
82-
83- $ mensajeError = $ response ->json ('message ' ) ?? "Error servidor Java: " . $ response ->status ();
84- throw new Exception ($ mensajeError );
85- }
79+ public function actualizarPedido ($ id , $ data )
80+ {
81+ try {
82+ \Log::info ("Enviando actualización a API Java para Pedido # {$ id }: " , $ data );
83+ $ response = Http::put ("{$ this ->baseUrl }/ {$ id }" , $ data );
8684
87- return $ response ->json ();
88- } catch (Exception $ e ) {
89- throw new Exception ($ e ->getMessage ());
85+ if ($ response ->failed ()) {
86+ \Log::error ("Error desde API Java (Pedido # {$ id }): " . $ response ->body ());
87+ $ mensajeError = $ response ->json ('message ' ) ?? "Error servidor Java: " . $ response ->status ();
88+ throw new Exception ($ mensajeError );
89+ }
90+ return $ response ->json ();
91+ } catch (Exception $ e ) {
92+ throw new Exception ($ e ->getMessage ());
93+ }
9094 }
91- }
9295
9396 public function eliminarPedido ($ id )
9497 {
@@ -100,24 +103,25 @@ public function eliminarPedido($id)
100103 }
101104
102105 /* =========================
103- CHECKOUT (CLAVE)
106+ CHECKOUT
104107 ========================= */
105108
106109 public function crearPedidoCheckout (array $ payload )
107- {
108- try {
109- return Http::withHeaders ([
110- 'Content-Type ' => 'application/json ' ,
111- 'Accept ' => 'application/json ' ,
112- ])
113- ->asJson ()
114- ->post ($ this ->baseUrl , $ payload )
115- ->throw ()
116- ->json ();
117- } catch (\Exception $ e ) {
118- throw new Exception ("Error API: " . ($ e ->response ?->body() ?? $ e ->getMessage ()));
110+ {
111+ try {
112+ return Http::withHeaders ([
113+ 'Content-Type ' => 'application/json ' ,
114+ 'Accept ' => 'application/json ' ,
115+ ])
116+ ->asJson ()
117+ ->post ($ this ->baseUrl , $ payload )
118+ ->throw ()
119+ ->json ();
120+ } catch (\Exception $ e ) {
121+ throw new Exception ("Error API: " . ($ e ->response ?->body() ?? $ e ->getMessage ()));
122+ }
119123 }
120- }
124+
121125 /* =========================
122126 DASHBOARD CLIENTE
123127 ========================= */
@@ -133,55 +137,47 @@ public function obtenerPedidosPorCliente($clienteId)
133137 }
134138 }
135139
136- protected $ detallesUrl = 'http://localhost:8080/detalles-pedidos ' ; // URL de tu nuevo Controller en Java
137-
138- public function obtenerTodosLosDetalles ()
139- {
140- try {
141- $ data = Http::get ($ this ->detallesUrl )->throw ()->json ();
142- \Log::info ("Detalles desde API Java: " , $ data ); // Esto te dirá exactamente cómo se llaman las llaves
143- return $ data ;
144- } catch (Exception $ e ) { }
145- }
146-
147- public function obtenerTodosLosPedidos ()
148- {
149- try {
150- // Usamos la baseUrl (http://localhost:8080/pedidos) que ya tienes definida
151- return Http::get ($ this ->baseUrl )->throw ()->json ();
152- } catch (Exception $ e ) {
153- throw new Exception ("Error al obtener el listado global de pedidos de la API. " );
140+ public function obtenerTodosLosDetalles ()
141+ {
142+ try {
143+ $ data = Http::get ($ this ->detallesUrl )->throw ()->json ();
144+ return $ data ;
145+ } catch (Exception $ e ) {
146+ return [];
147+ }
154148 }
155- }
156149
157- /* =========================
158- CATÁLOGOS PARA MODALES
159- ========================= */
150+ /* =========================
151+ CATÁLOGOS (CORREGIDOS)
152+ ========================= */
160153
161- public function obtenerEstados ()
162- {
163- try {
164- return Http::get ("http://localhost:8080/estadosPedidos " )->throw ()->json ();
165- } catch (Exception $ e ) {
166- return []; // Retorna array vacío si falla
154+ public function obtenerEstados ()
155+ {
156+ $ apiHost = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
157+ try {
158+ return Http::get ("{$ apiHost }/estadosPedidos " )->throw ()->json ();
159+ } catch (Exception $ e ) {
160+ return [];
161+ }
167162 }
168- }
169163
170- public function obtenerClientes ()
171- {
172- try {
173- return Http::get ("http://localhost:8080/detalle/cliente " )->throw ()->json ();
174- } catch (Exception $ e ) {
175- return [];
164+ public function obtenerClientes ()
165+ {
166+ $ apiHost = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
167+ try {
168+ return Http::get ("{$ apiHost }/detalle/cliente " )->throw ()->json ();
169+ } catch (Exception $ e ) {
170+ return [];
171+ }
176172 }
177- }
178173
179- public function obtenerEmpleados ()
180- {
181- try {
182- return Http::get ("http://localhost:8080/detalle/empleado " )->throw ()->json ();
183- } catch (Exception $ e ) {
184- return [];
174+ public function obtenerEmpleados ()
175+ {
176+ $ apiHost = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
177+ try {
178+ return Http::get ("{$ apiHost }/detalle/empleado " )->throw ()->json ();
179+ } catch (Exception $ e ) {
180+ return [];
181+ }
185182 }
186- }
187- }
183+ }
0 commit comments