/home/techb158/workloadmatch.com/api/routes
Edit: /home/techb158/workloadmatch.com/api/routes/programs.php (4882B)
prepare("SELECT COUNT(*) FROM Programs");
$stmt->execute();
$stmt->bind_result($total);
$stmt->fetch();
$stmt->close();
$stmt = $mysqli->prepare("SELECT * FROM Programs ORDER BY $sort $dir LIMIT ? OFFSET ?");
$stmt->bind_param('ii', $perPage, $offset);
$stmt->execute();
$result = $stmt->get_result();
$items = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
Response::paginated($items, $total, $page, $perPage);
}
function getProgram(string $id, mysqli $mysqli): void
{
Auth::requireLogin();
$stmt = $mysqli->prepare("SELECT * FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
if (!$item) Response::notFound('Program not found');
Response::success($item);
}
function createProgram(?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile', 'manager_profile']);
$name = trim($body['Program_Name'] ?? '');
if (!$name) Response::validationError(['Program_Name' => 'Required']);
$stmt = $mysqli->prepare("INSERT INTO Programs (Program_Name) VALUES (?)");
$stmt->bind_param('s', $name);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
log_activity($mysqli, 'create', 'program', $newId, $name, 'Program created via API');
$stmt = $mysqli->prepare("SELECT * FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $newId);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
Response::created($item);
}
function updateProgram(?string $id, ?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile', 'manager_profile']);
if (!$id) Response::error('Program ID required');
$existing = null;
$stmt = $mysqli->prepare("SELECT * FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$existing = $result->fetch_assoc();
$stmt->close();
if (!$existing) Response::notFound('Program not found');
$name = trim($body['Program_Name'] ?? $existing['Program_Name']);
$stmt = $mysqli->prepare("UPDATE Programs SET Program_Name = ? WHERE Program_ID = ?");
$stmt->bind_param('si', $name, $id);
$stmt->execute();
$stmt->close();
log_activity($mysqli, 'update', 'program', $id, $name, 'Program updated via API');
$stmt = $mysqli->prepare("SELECT * FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
Response::success($item, 'Program updated');
}
function deleteProgram(?string $id, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
if (!$id) Response::error('Program ID required');
$stmt = $mysqli->prepare("SELECT Program_Name FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($name);
$stmt->fetch();
$stmt->close();
if (!$name) Response::notFound('Program not found');
// Check for related courses
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM Courses WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($courseCount);
$stmt->fetch();
$stmt->close();
if ($courseCount > 0) {
Response::error("Cannot delete program: $courseCount course(s) are linked to it", 409);
}
$stmt = $mysqli->prepare("DELETE FROM Programs WHERE Program_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
log_activity($mysqli, 'delete', 'program', $id, $name, 'Program deleted via API');
Response::success(null, 'Program deleted');
}