+ +
+ +
diff --git a/action/puesto.php b/action/puesto.php index 5e137f6..c51a638 100644 --- a/action/puesto.php +++ b/action/puesto.php @@ -15,9 +15,21 @@ try { case 'GET': // Fetch all puestos $facultad_id = $user->facultad['facultad_id'] ?? -1; - $puestos = $db->orderBy('puesto_id', 'desc') - ->where('facultad_id', $facultad_id) - ->get('puesto'); + $carreras = array_map(fn($c) => $c['carrera_id'], $db->where('facultad_id', $facultad_id)->get(tableName: 'carrera', columns: 'carrera_id')); + $puestos = array_map( + fn($p) => array( + ...$p, + 'materias' => $db->where('puesto_id', $p['puesto_id']) + ->join('puesto_materia', 'puesto_materia.materia_id = materia.materia_id', 'LEFT') + ->get(tableName: 'materia', columns: ['materia.materia_id', 'materia_nombre', 'clave_materia',]), + 'encargado' => $db->where('puesto_id', $p['puesto_id']) + ->join('puesto_usuario', 'puesto_usuario.usuario_id = usuario.usuario_id', 'LEFT') + ->getOne('usuario', ['usuario.usuario_id', 'usuario_nombre', 'usuario_clave']), + ), + $db->orderBy('puesto_id', 'desc') + ->where('facultad_id', $facultad_id) + ->get(tableName: 'puesto', numRows: count($carreras), columns: 'puesto_id, nombre'), + ); echo json_encode($puestos); break; @@ -31,21 +43,45 @@ try { exit(); } - $puesto_id = $db->insert('puestos', ['puesto_nombre' => $input_data['puesto_nombre']]); - echo json_encode(['msg' => 'Puesto creado exitosamente', 'puesto_id' => $puesto_id]); + $puesto = $db->insert('puesto', [ + 'nombre' => $input_data['puesto_nombre'], + 'facultad_id' => $user->facultad['facultad_id'], + ], ['puesto_id', 'nombre', 'facultad_id']); + + echo json_encode( + array( + ...$puesto, + 'materias' => [], + 'encargado' => null, + ), + ); break; case 'PUT': $raw_input = file_get_contents('php://input'); $input_data = json_decode($raw_input, true); - if (!$input_data || !isset($input_data['puesto_id'], $input_data['puesto_nombre'])) { + if (!$input_data || !isset($input_data['puesto_id'], $input_data['materias'], $input_data['usuario_id'])) { header('HTTP/1.1 400 Bad Request'); echo json_encode(['error' => 'Datos inválidos']); exit(); } - $db->where('puesto_id', $input_data['puesto_id'])->update('puestos', ['puesto_nombre' => $input_data['puesto_nombre']]); + $db->where('puesto_id', $input_data['puesto_id'])->delete('puesto_materia'); + $db->where('puesto_id', $input_data['puesto_id'])->delete('puesto_usuario'); + + foreach ($input_data['materias'] as $materia_id) { + $db->insert('puesto_materia', [ + 'puesto_id' => $input_data['puesto_id'], + 'materia_id' => $materia_id, + ]); + } + + $db->insert('puesto_usuario', [ + 'puesto_id' => $input_data['puesto_id'], + 'usuario_id' => $input_data['usuario_id'], + ]); + echo json_encode(['msg' => 'Puesto actualizado exitosamente']); break; diff --git a/js/puestos.js b/js/puestos.js new file mode 100644 index 0000000..c0020ca --- /dev/null +++ b/js/puestos.js @@ -0,0 +1,50 @@ +import { createApp } from 'https://unpkg.com/petite-vue?module'; +const app = createApp({ + message: null, + puestos: [], + carreras: [], + materias: [], + usuarios: [], + async nuevoPuesto(nuevoPuesto) { + try { + const res = await fetch('action/puesto.php', { + method: 'POST', + body: JSON.stringify({ + puesto_nombre: nuevoPuesto + }) + }); + const data = await res.json(); + this.puestos.push(data); + } + catch (error) { + alert(`Error: ${error}`); + } + }, + async actualizarPuesto(puesto_id, materias, usuario_id) { + try { + const res = await fetch('action/puesto.php', { + method: 'PUT', + body: JSON.stringify({ + puesto_id, + materias: materias.map(m => m.materia_id), + usuario_id + }) + }); + const data = await res.json(); + this.message = data.msg; + // after 3 seconds, remove the message + setTimeout(() => { + this.message = null; + }, 3000); + } + catch (error) { + alert(`Error: ${error}`); + } + }, + async mounted() { + this.puestos = await fetch('action/puesto.php').then(res => res.json()); + this.carreras = await fetch('action/action_carreras.php').then(res => res.json()); + this.materias = await fetch('action/action_materias.php').then(res => res.json()); + this.usuarios = await fetch('action/usuarios.php').then(res => res.json()); + } +}).mount('#app'); diff --git a/puestos.php b/puestos.php new file mode 100644 index 0000000..b895b5c --- /dev/null +++ b/puestos.php @@ -0,0 +1,196 @@ + + + +
+ + +