1- <?php
1+ <?php
22
3- namespace App \Services \Inventario ;
3+ namespace App \Services \Inventario ;
44
5- use Illuminate \Support \Facades \Http ;
5+ use Illuminate \Support \Facades \Http ;
66
7- class CategoriaIngredientesService
8- {
9- protected $ baseUrl ;
10-
11- public function __construct ()
12- {
13- // Obtiene la URL base de la API desde el archivo .env
14- $ this ->baseUrl = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
15- }
16-
17- /**
18- * Helper para obtener el cliente HTTP.
19- * @return \Illuminate\Http\Client\PendingRequest
20- */
21- protected function getApiClient ()
7+ class CategoriaIngredientesService
228 {
23- return Http::baseUrl ($ this ->baseUrl );
24- }
25-
26- // =========================================================================
27- // CRUD: OBTENER TODAS (GET /categorias/ingredientes)
28- // =========================================================================
29-
30- /**
31- * Obtiene el listado de categorías de ingredientes desde la API.
32- * @return array Retorna un array con 'success' y 'data' o 'error'.
33- */
34- public function obtenerCategoriasIngredientes (): array
35- {
36- try {
37- $ response = $ this ->getApiClient ()->get ('/categorias/ingredientes ' );
9+ protected $ baseUrl ;
10+
11+ public function __construct ()
12+ {
13+ // Obtiene la URL base de la API desde el archivo .env
14+ $ this ->baseUrl = env ('API_BASE_URL ' , 'http://32.193.167.191:8080 ' );
15+ }
16+
17+ /**
18+ * Helper para obtener el cliente HTTP.
19+ * @return \Illuminate\Http\Client\PendingRequest
20+ */
21+ protected function getApiClient ()
22+ {
23+ return Http::baseUrl ($ this ->baseUrl )
24+ ->acceptJson ()
25+ ->contentType ('application/json ' ); }
26+
27+ // =========================================================================
28+ // CRUD: OBTENER TODAS (GET /categorias/ingredientes)
29+ // =========================================================================
30+
31+ /**
32+ * Obtiene el listado de categorías de ingredientes desde la API.
33+ * @return array Retorna un array con 'success' y 'data' o 'error'.
34+ */
35+ public function obtenerCategoriasIngredientes (): array
36+ {
37+ try {
38+ $ response = $ this ->getApiClient ()->get ('/categorias/ingredientes ' );
39+
40+ if ($ response ->successful ()) {
41+ return [
42+ 'success ' => true ,
43+ 'data ' => $ response ->json ()
44+ ];
45+ }
3846
39- if ($ response ->successful ()) {
4047 return [
41- 'success ' => true ,
42- 'data ' => $ response ->json ()
48+ 'success ' => false ,
49+ 'error ' => $ response ->json ()[ ' error ' ] ?? ' Error desconocido al obtener categorías ( ' . $ response -> status () . ' ) '
4350 ];
44- }
45-
46- return [
47- 'success ' => false ,
48- 'error ' => $ response ->json ()['error ' ] ?? 'Error desconocido al obtener categorías ( ' . $ response ->status () . ') '
49- ];
5051
51- } catch (\Exception $ e ) {
52- return [
53- 'success ' => false ,
54- 'error ' => 'No se pudo conectar con el servidor de la API: ' . $ e ->getMessage ()
55- ];
52+ } catch (\Exception $ e ) {
53+ return [
54+ 'success ' => false ,
55+ 'error ' => 'No se pudo conectar con el servidor de la API: ' . $ e ->getMessage ()
56+ ];
57+ }
5658 }
57- }
5859
59- // =========================================================================
60- // CRUD: CREAR (POST /nuevacategoriaingrediente)
61- // =========================================================================
60+ // =========================================================================
61+ // CRUD: CREAR (POST /nuevacategoriaingrediente)
62+ // =========================================================================
63+
64+ /**
65+ * Crea una nueva categoría de ingrediente.
66+ * @param array $data Array con nombreCategoria.
67+ */
68+ public function crearCategoriaIngrediente (array $ data ): array
69+ {
70+ try {
71+ $ payload = [
72+ 'nombreCategoria ' => $ data ['nombreCategoria ' ]
73+ ];
6274
63- /**
64- * Crea una nueva categoría de ingrediente.
65- * @param array $data Array con nombreCategoria.
66- */
67- public function crearCategoriaIngrediente (array $ data ): array
68- {
69- try {
70- $ payload = [
71- 'nombreCategoria ' => $ data ['nombreCategoria ' ]
72- ];
75+ $ response = $ this ->getApiClient ()->post ('/nuevacategoriaingrediente ' , $ payload );
7376
74- $ response = $ this ->getApiClient ()->post ('/nuevacategoriaingrediente ' , $ payload );
77+ if ($ response ->successful ()) {
78+ return [
79+ 'success ' => true ,
80+ 'response ' => $ response ->body ()
81+ ];
82+ }
7583
76- if ($ response ->successful ()) {
7784 return [
78- 'success ' => true ,
79- 'response ' => $ response ->body ()
85+ 'success ' => false ,
86+ 'error ' => $ response ->json ()[ ' error ' ] ?? ' Error al crear la categoría ( ' . $ response -> status () . ' ) '
8087 ];
81- }
82-
83- return [
84- 'success ' => false ,
85- 'error ' => $ response ->json ()['error ' ] ?? 'Error al crear la categoría ( ' . $ response ->status () . ') '
86- ];
8788
88- } catch (\Exception $ e ) {
89- return [
90- 'success ' => false ,
91- 'error ' => 'Fallo de conexión al intentar crear: ' . $ e ->getMessage ()
92- ];
89+ } catch (\Exception $ e ) {
90+ return [
91+ 'success ' => false ,
92+ 'error ' => 'Fallo de conexión al intentar crear: ' . $ e ->getMessage ()
93+ ];
94+ }
9395 }
94- }
9596
96- // =========================================================================
97- // CRUD: ACTUALIZAR (PUT /categoriaingrediente/{id})
98- // =========================================================================
97+ // =========================================================================
98+ // CRUD: ACTUALIZAR (PUT /categoriaingrediente/{id})
99+ // =========================================================================
100+
101+ /**
102+ * Actualiza una categoría de ingrediente existente por ID.
103+ */
104+ public function actualizarCategoriaIngrediente (int $ id , array $ data ): array
105+ {
106+ try {
107+ $ payload = [
108+ 'nombreCategoria ' => $ data ['nombreCategoria ' ]
109+ ];
99110
100- /**
101- * Actualiza una categoría de ingrediente existente por ID.
102- */
103- public function actualizarCategoriaIngrediente (int $ id , array $ data ): array
104- {
105- try {
106- $ payload = [
107- 'nombreCategoria ' => $ data ['nombreCategoria ' ]
108- ];
111+ $ response = $ this ->getApiClient ()->put ("/categoriaingrediente/ {$ id }" , $ payload );
109112
110- $ response = $ this ->getApiClient ()->put ("/categoriaingrediente/ {$ id }" , $ payload );
113+ if ($ response ->successful ()) {
114+ return [
115+ 'success ' => true ,
116+ 'response ' => $ response ->body ()
117+ ];
118+ }
111119
112- if ($ response ->successful ()) {
113120 return [
114- 'success ' => true ,
115- 'response ' => $ response ->body ()
121+ 'success ' => false ,
122+ 'error ' => $ response ->json ()[ ' error ' ] ?? ' Error al actualizar la categoría ( ' . $ response -> status () . ' ) '
116123 ];
117- }
118124
119- return [
120- 'success ' => false ,
121- 'error ' => $ response ->json ()['error ' ] ?? 'Error al actualizar la categoría ( ' . $ response ->status () . ') '
122- ];
123-
124- } catch (\Exception $ e ) {
125- return [
126- 'success ' => false ,
127- 'error ' => 'Fallo de conexión al intentar actualizar: ' . $ e ->getMessage ()
128- ];
125+ } catch (\Exception $ e ) {
126+ return [
127+ 'success ' => false ,
128+ 'error ' => 'Fallo de conexión al intentar actualizar: ' . $ e ->getMessage ()
129+ ];
130+ }
129131 }
130- }
131132
132- // =========================================================================
133- // CRUD: ELIMINAR (DELETE /eliminarcategoria/{id})
134- // =========================================================================
133+ // =========================================================================
134+ // CRUD: ELIMINAR (DELETE /eliminarcategoria/{id})
135+ // =========================================================================
136+
137+ /**
138+ * Elimina una categoría de ingrediente por ID.
139+ */
140+ public function eliminarCategoriaIngrediente (int $ id ): array
141+ {
142+ try {
143+ $ response = $ this ->getApiClient ()->delete ("/eliminarcategoria/ {$ id }" );
144+
145+ if ($ response ->successful ()) {
146+ return [
147+ 'success ' => true ,
148+ 'response ' => $ response ->body ()
149+ ];
150+ }
135151
136- /**
137- * Elimina una categoría de ingrediente por ID.
138- */
139- public function eliminarCategoriaIngrediente (int $ id ): array
140- {
141- try {
142- $ response = $ this ->getApiClient ()->delete ("/eliminarcategoria/ {$ id }" );
152+ return [
153+ 'success ' => false ,
154+ 'error ' => $ response ->json ()['error ' ] ?? 'Error al eliminar la categoría ( ' . $ response ->status () . ') '
155+ ];
143156
144- if ( $ response -> successful () ) {
157+ } catch ( \ Exception $ e ) {
145158 return [
146- 'success ' => true ,
147- 'response ' => $ response -> body ()
159+ 'success ' => false ,
160+ 'error ' => ' Fallo de conexión al intentar eliminar: ' . $ e -> getMessage ()
148161 ];
149162 }
150-
151- return [
152- 'success ' => false ,
153- 'error ' => $ response ->json ()['error ' ] ?? 'Error al eliminar la categoría ( ' . $ response ->status () . ') '
154- ];
155-
156- } catch (\Exception $ e ) {
157- return [
158- 'success ' => false ,
159- 'error ' => 'Fallo de conexión al intentar eliminar: ' . $ e ->getMessage ()
160- ];
161163 }
162- }
163- }
164+ }
0 commit comments