77 lines
2.7 KiB
PHP
77 lines
2.7 KiB
PHP
<?php
|
|
|
|
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
|
|
header('Content-Type: application/json');
|
|
|
|
if (!Login::is_logged()) {
|
|
header('HTTP/1.1 401 Unauthorized');
|
|
echo json_encode(['error' => 'No se ha iniciado sesión']);
|
|
exit();
|
|
}
|
|
$user = Login::get_user();
|
|
|
|
try {
|
|
switch ($_SERVER['REQUEST_METHOD']) {
|
|
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');
|
|
echo json_encode($puestos);
|
|
break;
|
|
|
|
case 'POST':
|
|
$raw_input = file_get_contents('php://input');
|
|
$input_data = json_decode($raw_input, true);
|
|
|
|
if (!$input_data || !isset($input_data['puesto_nombre'])) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(['error' => 'Datos inválidos']);
|
|
exit();
|
|
}
|
|
|
|
$puesto_id = $db->insert('puestos', ['puesto_nombre' => $input_data['puesto_nombre']]);
|
|
echo json_encode(['msg' => 'Puesto creado exitosamente', 'puesto_id' => $puesto_id]);
|
|
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'])) {
|
|
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']]);
|
|
echo json_encode(['msg' => 'Puesto actualizado exitosamente']);
|
|
break;
|
|
|
|
case 'DELETE':
|
|
$raw_input = file_get_contents('php://input');
|
|
$input_data = json_decode($raw_input, true);
|
|
|
|
if (!$input_data || !isset($input_data['puesto_id'])) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(['error' => 'Datos inválidos']);
|
|
exit();
|
|
}
|
|
|
|
$db->where('puesto_id', $input_data['puesto_id'])->delete('puestos');
|
|
echo json_encode(['msg' => 'Puesto eliminado exitosamente']);
|
|
break;
|
|
|
|
default:
|
|
header('HTTP/1.1 405 Method Not Allowed');
|
|
echo json_encode(['error' => 'Método no permitido']);
|
|
break;
|
|
}
|
|
} catch (PDOException $e) {
|
|
echo json_encode([
|
|
'error' => $e->getMessage(),
|
|
'query' => $db->getLastQuery(),
|
|
'exception' => $e->getTraceAsString()
|
|
]);
|
|
} |