Skip to content

Commit 16a273d

Browse files
committed
Merge branch 'desarrollo' into produccion
2 parents 27b7ec4 + 67c58ac commit 16a273d

3 files changed

Lines changed: 214 additions & 141 deletions

File tree

Frontend/app/Http/Controllers/Inventario/RecetasController.php

Lines changed: 114 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,105 +4,164 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Services\Inventario\RecetasService;
7-
use App\Services\Productos\ProductoService;
87
use Illuminate\Http\Request;
98
use Illuminate\Support\Facades\Http;
109
use Illuminate\Support\Facades\Log;
1110

1211
class RecetasController extends Controller
1312
{
1413
protected $recetasService;
15-
protected $productosService;
1614

17-
public function __construct(RecetasService $recetasService, ProductoService $productosService)
15+
public function __construct(RecetasService $recetasService)
1816
{
1917
$this->recetasService = $recetasService;
20-
$this->productosService = $productosService;
2118
}
2219

23-
public function index()
24-
{
25-
// ✅ Recetas — si falla, carga vacío en lugar de redirigir
26-
$resRecetas = $this->recetasService->obtenerTodasLasRecetas();
27-
$recetas = $resRecetas['success'] ? $resRecetas['data'] : [];
28-
29-
// ✅ Productos — ya tiene try/catch
30-
try {
31-
$productos = $this->productosService->obtenerProductos();
32-
} catch (\Exception $e) {
33-
$productos = [];
34-
Log::error('Error al obtener productos en RecetasController: ' . $e->getMessage());
20+
// ─── Método privado centralizado para lista-modal ───────────────
21+
private function obtenerIngredientesModal(): array
22+
{
23+
try {
24+
$response = Http::timeout(5)->get('http://32.193.167.191:8080/recetas/lista-modal');
25+
return $response->successful() ? ($response->json() ?? []) : [];
26+
} catch (\Exception $e) {
27+
Log::error('RecetasController - lista-modal: ' . $e->getMessage());
28+
return [];
29+
}
3530
}
3631

37-
// ✅ Ingredientes para modal — ya tiene try/catch
38-
try {
39-
$responseIng = Http::get('http://32.193.167.191:8080/recetas/lista-modal');
40-
$ingredientesParaModal = $responseIng->successful() ? $responseIng->json() : [];
41-
} catch (\Exception $e) {
42-
$ingredientesParaModal = [];
43-
}
32+
public function index()
33+
{
34+
// Recetas
35+
try {
36+
$resRecetas = $this->recetasService->obtenerTodasLasRecetas();
37+
$recetas = $resRecetas['success'] ? $resRecetas['data'] : [];
38+
$errorRecetas = $resRecetas['success'] ? null : ($resRecetas['error'] ?? 'Error desconocido');
39+
} catch (\Exception $e) {
40+
$recetas = [];
41+
$errorRecetas = 'Error al obtener recetas';
42+
Log::error('RecetasController@index recetas: ' . $e->getMessage());
43+
}
4444

45-
// ✅ Siempre carga la vista — muestra error como alerta si hubo fallo
46-
return view('inventarioviews.recetas.index', [
47-
'recetas' => $recetas,
48-
'productos' => $productos,
49-
'ingredientes' => $ingredientesParaModal,
50-
'error' => $resRecetas['success'] ? null : $resRecetas['error'],
51-
]);
52-
}
45+
// Productos — la API devuelve JSON array, no objetos Eloquent
46+
try {
47+
$responseProd = Http::timeout(5)->get('http://32.193.167.191:8080/productos');
48+
$productos = $responseProd->successful() ? ($responseProd->json() ?? []) : [];
49+
} catch (\Exception $e) {
50+
$productos = [];
51+
Log::error('RecetasController@index productos: ' . $e->getMessage());
52+
}
53+
54+
// Ingredientes para modal (centralizado)
55+
$ingredientesParaModal = $this->obtenerIngredientesModal();
56+
57+
return view('inventarioviews.recetas.index', [
58+
'recetas' => $recetas,
59+
'productos' => $productos,
60+
'ingredientes' => $ingredientesParaModal,
61+
'error' => $errorRecetas ?? null,
62+
]);
63+
}
5364

5465
public function show($idProducto)
5566
{
56-
$res = $this->recetasService->obtenerRecetaPorIdProducto($idProducto);
67+
if (!is_numeric($idProducto)) {
68+
return redirect()->route('recetas.index')->with('error', 'ID de producto inválido');
69+
}
70+
71+
try {
72+
$res = $this->recetasService->obtenerRecetaPorIdProducto((int) $idProducto);
73+
} catch (\Exception $e) {
74+
Log::error('RecetasController@show: ' . $e->getMessage());
75+
return redirect()->route('recetas.index')->with('error', 'Error al obtener la receta');
76+
}
77+
78+
// Si la receta no existe, redirigir con mensaje claro
79+
if (!$res['success']) {
80+
return redirect()->route('recetas.index')->with('error', $res['error'] ?? 'Receta no encontrada');
81+
}
82+
83+
// Protección extra: si data viene vacío (Spring retornó 404 vacío)
84+
if (empty($res['data'])) {
85+
return redirect()->route('recetas.index')->with('error', 'No se encontraron ingredientes para esta receta');
86+
}
87+
88+
$ingredientes = $this->obtenerIngredientesModal();
89+
90+
return view('inventarioviews.recetas.show', [
91+
'detalles' => $res['data'],
92+
'idProducto' => (int) $idProducto,
93+
'ingredientes' => $ingredientes,
94+
]);
95+
}
96+
97+
public function store(Request $request)
98+
{
99+
// Validación básica antes de llamar al service
100+
if (empty($request->input('idProducto'))) {
101+
return back()->with('error', 'Debe seleccionar un producto');
102+
}
103+
104+
if (empty($request->input('detalles'))) {
105+
return back()->with('error', 'Debe agregar al menos un ingrediente');
106+
}
57107

58-
// ✅ Cargar ingredientes para el select del modal de edición
59108
try {
60-
$responseIng = Http::get('http://32.193.167.191:8080/recetas/lista-modal');
61-
$ingredientes = $responseIng->successful() ? $responseIng->json() : [];
109+
$res = $this->recetasService->crearReceta($request->all());
62110
} catch (\Exception $e) {
63-
$ingredientes = [];
111+
Log::error('RecetasController@store: ' . $e->getMessage());
112+
return back()->with('error', 'Error inesperado al crear la receta');
64113
}
65114

66115
if ($res['success']) {
67-
return view('inventarioviews.recetas.show', [
68-
'detalles' => $res['data'],
69-
'idProducto' => $idProducto,
70-
'ingredientes' => $ingredientes, // ✅ ahora disponible en la vista
71-
]);
116+
return redirect()->route('recetas.index')->with('success', 'Receta creada correctamente');
72117
}
73118

74-
return redirect()->route('recetas.index')->with('error', $res['error']);
119+
return back()->with('error', $res['error'] ?? 'Error al crear receta');
75120
}
76121

77-
// ✅ Método update que faltaba completamente
78122
public function update(Request $request, $idProducto)
79123
{
80-
$res = $this->recetasService->actualizarReceta($idProducto, $request->all());
124+
if (!is_numeric($idProducto)) {
125+
return back()->with('error', 'ID de producto inválido');
126+
}
127+
128+
if (empty($request->input('detalles'))) {
129+
return back()->with('error', 'Debe agregar al menos un ingrediente');
130+
}
131+
132+
try {
133+
$res = $this->recetasService->actualizarReceta((int) $idProducto, $request->all());
134+
} catch (\Exception $e) {
135+
Log::error('RecetasController@update: ' . $e->getMessage());
136+
return back()->with('error', 'Error inesperado al actualizar la receta');
137+
}
81138

82139
if ($res['success']) {
83140
return redirect()
84-
->route('recetas.show', $idProducto)
141+
->route('recetas.show', (int) $idProducto)
85142
->with('success', 'Receta actualizada correctamente');
86143
}
87144

88145
return back()->with('error', $res['error'] ?? 'Error al actualizar la receta');
89146
}
90147

91-
public function store(Request $request)
148+
public function destroy($idProducto)
92149
{
93-
$res = $this->recetasService->crearReceta($request->all());
94-
if ($res['success']) {
95-
return redirect()->route('recetas.index')->with('success', 'Receta creada correctamente');
150+
if (!is_numeric($idProducto)) {
151+
return back()->with('error', 'ID de producto inválido');
152+
}
153+
154+
try {
155+
$res = $this->recetasService->eliminarReceta((int) $idProducto);
156+
} catch (\Exception $e) {
157+
Log::error('RecetasController@destroy: ' . $e->getMessage());
158+
return back()->with('error', 'Error inesperado al eliminar la receta');
96159
}
97-
return back()->with('error', $res['error'] ?? 'Error al crear receta');
98-
}
99160

100-
public function destroy($idProducto)
101-
{
102-
$res = $this->recetasService->eliminarReceta($idProducto);
103161
if ($res['success']) {
104-
return redirect()->route('recetas.index')->with('success', 'Receta eliminada');
162+
return redirect()->route('recetas.index')->with('success', 'Receta eliminada correctamente');
105163
}
164+
106165
return back()->with('error', 'No se pudo eliminar la receta');
107166
}
108167
}

0 commit comments

Comments
 (0)