/home/techb158/workloadmatch.com/api/routes
Edit: /home/techb158/workloadmatch.com/api/routes/timeslots.php (4807B)
prepare("SELECT * FROM time_slot_programs $whereClause ORDER BY Time_From ASC");
if ($params) $stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$items = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
Response::success($items);
}
function getTimeSlot(string $id, mysqli $mysqli): void
{
Auth::requireLogin();
$stmt = $mysqli->prepare("SELECT * FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
if (!$item) Response::notFound('Time slot not found');
Response::success($item);
}
function createTimeSlot(?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
$timeSlot = trim($body['Time_Slot'] ?? '');
$timeFrom = $body['Time_From'] ?? null;
$timeTo = $body['Time_To'] ?? null;
$programId = $body['Program_ID'] ?? null;
$timeSlotId = $body['Time_Slot_ID'] ?? null;
if (!$timeSlot || !$timeFrom || !$timeTo) {
Response::validationError(['Time_Slot', 'Time_From', 'Time_To' => 'Required']);
}
$stmt = $mysqli->prepare("INSERT INTO time_slot_programs (Time_Slot, Time_From, Time_To, Program_ID, Time_Slot_ID) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssii', $timeSlot, $timeFrom, $timeTo, $programId, $timeSlotId);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
$stmt = $mysqli->prepare("SELECT * FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $newId);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
Response::created($item);
}
function updateTimeSlot(?string $id, ?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
if (!$id) Response::error('Time slot ID required');
$stmt = $mysqli->prepare("SELECT * FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$existing = $result->fetch_assoc();
$stmt->close();
if (!$existing) Response::notFound('Time slot not found');
$timeSlot = trim($body['Time_Slot'] ?? $existing['Time_Slot']);
$timeFrom = $body['Time_From'] ?? $existing['Time_From'];
$timeTo = $body['Time_To'] ?? $existing['Time_To'];
$stmt = $mysqli->prepare("UPDATE time_slot_programs SET Time_Slot = ?, Time_From = ?, Time_To = ? WHERE time_slot_programs_ID = ?");
$stmt->bind_param('sssi', $timeSlot, $timeFrom, $timeTo, $id);
$stmt->execute();
$stmt->close();
$stmt = $mysqli->prepare("SELECT * FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
Response::success($item, 'Time slot updated');
}
function deleteTimeSlot(?string $id, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
if (!$id) Response::error('Time slot ID required');
$stmt = $mysqli->prepare("SELECT * FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$existing = $result->fetch_assoc();
$stmt->close();
if (!$existing) Response::notFound('Time slot not found');
$stmt = $mysqli->prepare("DELETE FROM time_slot_programs WHERE time_slot_programs_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
Response::success(null, 'Time slot deleted');
}