FormController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // app/Http/Controllers/FormController.php
  3. namespace App\Http\Controllers;
  4. use App\Models\Form;
  5. use App\Models\FormResponse;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Validation\ValidationException;
  9. use Illuminate\Support\Facades\DB;
  10. class FormController extends Controller
  11. {
  12. public function store(Request $request): JsonResponse
  13. {
  14. try {
  15. $validated = $request->validate([
  16. 'title' => 'required|string|max:255',
  17. 'tabs' => 'required|array',
  18. 'tabs.*.id' => 'required|string',
  19. 'tabs.*.title' => 'required|string',
  20. 'tabs.*.rows' => 'required|integer|min:1',
  21. 'tabs.*.columns' => 'required|integer|min:1',
  22. 'tabs.*.elements' => 'required|array',
  23. 'tabs.*.elements.*.position' => 'required|array',
  24. 'tabs.*.elements.*.element' => 'required|array',
  25. ]);
  26. // Asumiendo que la tabla se llama 'forms'
  27. $formId = DB::table('forms')->insertGetId([
  28. 'title' => $validated['title'],
  29. 'configuration' => json_encode([
  30. 'tabs' => $validated['tabs']
  31. ]),
  32. 'is_published' => false,
  33. 'created_at' => now(),
  34. 'updated_at' => now()
  35. ]);
  36. return response()->json([
  37. 'success' => true,
  38. 'message' => 'Formulario guardado exitosamente',
  39. 'form_id' => $formId
  40. ], 201);
  41. } catch (ValidationException $e) {
  42. return response()->json([
  43. 'success' => false,
  44. 'message' => 'Error de validación',
  45. 'errors' => $e->errors()
  46. ], 422);
  47. } catch (\Exception $e) {
  48. return response()->json([
  49. 'success' => false,
  50. 'message' => 'Error interno del servidor',
  51. 'error' => $e->getMessage()
  52. ], 500);
  53. }
  54. }
  55. public function getAll(): JsonResponse
  56. {
  57. try {
  58. $forms = DB::table('forms')->get();
  59. // Decodificamos el campo 'configuration' de cada formulario
  60. $forms = $forms->map(function ($form) {
  61. $form->configuration = json_decode($form->configuration, true);
  62. return $form;
  63. });
  64. return response()->json([
  65. 'success' => true,
  66. 'forms' => $forms
  67. ]);
  68. } catch (\Exception $e) {
  69. return response()->json([
  70. 'success' => false,
  71. 'message' => 'Error al obtener los formularios',
  72. 'error' => $e->getMessage()
  73. ], 500);
  74. }
  75. }
  76. public function getFormById($id): JsonResponse
  77. {
  78. // Buscar el formulario por ID con Query Builder
  79. $form = DB::table('forms')->where('id', $id)->first();
  80. if (!$form) {
  81. return response()->json([
  82. 'success' => false,
  83. 'message' => 'Formulario no encontrado',
  84. ], 404);
  85. }
  86. // Decodificar el campo 'configuration' (JSON)
  87. $configuration = json_decode($form->configuration, true);
  88. return response()->json([
  89. 'success' => true,
  90. 'form' => [
  91. 'id' => $form->id,
  92. 'title' => $form->title,
  93. 'is_published' => $form->is_published,
  94. 'configuration' => $configuration
  95. ]
  96. ]);
  97. }
  98. public function update(Request $request, $id): JsonResponse
  99. {
  100. try {
  101. $validated = $request->validate([
  102. 'title' => 'required|string|max:255',
  103. 'tabs' => 'required|array',
  104. 'tabs.*.id' => 'required|string',
  105. 'tabs.*.title' => 'required|string',
  106. 'tabs.*.rows' => 'required|integer|min:1',
  107. 'tabs.*.columns' => 'required|integer|min:1',
  108. 'tabs.*.elements' => 'required|array',
  109. 'tabs.*.elements.*.position' => 'required|array',
  110. 'tabs.*.elements.*.element' => 'required|array',
  111. ]);
  112. $affected = DB::table('forms')
  113. ->where('id', $id)
  114. ->update([
  115. 'title' => $validated['title'],
  116. 'configuration' => json_encode([
  117. 'tabs' => $validated['tabs']
  118. ]),
  119. 'updated_at' => now()
  120. ]);
  121. if ($affected === 0) {
  122. return response()->json([
  123. 'success' => false,
  124. 'message' => 'No se encontró el formulario para actualizar'
  125. ], 404);
  126. }
  127. return response()->json([
  128. 'success' => true,
  129. 'message' => 'Formulario actualizado exitosamente'
  130. ], 200);
  131. } catch (ValidationException $e) {
  132. return response()->json([
  133. 'success' => false,
  134. 'message' => 'Error de validación',
  135. 'errors' => $e->errors()
  136. ], 422);
  137. } catch (\Exception $e) {
  138. return response()->json([
  139. 'success' => false,
  140. 'message' => 'Error interno del servidor',
  141. 'error' => $e->getMessage()
  142. ], 500);
  143. }
  144. }
  145. public function destroy($id)
  146. {
  147. try {
  148. $form = Form::findOrFail($id);
  149. if ($form->is_published) {
  150. return response()->json([
  151. 'success' => false,
  152. 'message' => 'No se puede eliminar un formulario ya publicado.'
  153. ], 403);
  154. }
  155. // Eliminar el formulario (configuración incluida)
  156. $form->delete();
  157. return response()->json([
  158. 'success' => true,
  159. 'message' => 'Formulario eliminado correctamente.'
  160. ], 200);
  161. } catch (\Exception $e) {
  162. return response()->json([
  163. 'success' => false,
  164. 'message' => 'Error al eliminar el formulario.',
  165. 'error' => $e->getMessage()
  166. ], 500);
  167. }
  168. }
  169. }