/home/techb158/workloadmatch.com/api/routes
Edit: /home/techb158/workloadmatch.com/api/routes/masters.php (6685B)
prepare("SELECT COUNT(*) FROM master_profile");
$stmt->execute();
$stmt->bind_result($total);
$stmt->fetch();
$stmt->close();
$stmt = $mysqli->prepare("SELECT * FROM master_profile 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();
foreach ($items as &$item) {
unset($item['Password'], $item['salt']);
$s = $mysqli->prepare("SELECT COUNT(*) FROM manager_profile WHERE Master_ID = ?");
$s->bind_param('i', $item['Master_ID']);
$s->execute();
$s->bind_result($item['manager_count']);
$s->fetch();
$s->close();
}
Response::paginated($items, $total, $page, $perPage);
}
function getMaster(string $id, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
$stmt = $mysqli->prepare("SELECT * FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
if (!$item) Response::notFound('Master not found');
unset($item['Password'], $item['salt']);
$s = $mysqli->prepare("SELECT COUNT(*) FROM manager_profile WHERE Master_ID = ?");
$s->bind_param('i', $id);
$s->execute();
$s->bind_result($item['manager_count']);
$s->fetch();
$s->close();
Response::success($item);
}
function createMaster(?array $body, mysqli $mysqli): void
{
Auth::requireRole('admin_profile');
$username = trim($body['username'] ?? $body['User_Name'] ?? '');
$password = $body['password'] ?? $body['p'] ?? '';
if (!$username || !$password) {
Response::validationError(['username' => 'Required', 'password' => 'Required']);
}
$passwordHash = password_hash($password, PASSWORD_BCRYPT);
// Check uniqueness
foreach (['master_profile' => 'User_Name', 'admin_profile' => 'User_Name'] as $table => $col) {
$stmt = $mysqli->prepare("SELECT 1 FROM $table WHERE $col = ? LIMIT 1");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) { $stmt->close(); Response::error("Username already exists", 409); }
$stmt->close();
}
$dateTime = date('Y/m/d H:i:s');
$adminId = Auth::getUserId();
$stmt = $mysqli->prepare("INSERT INTO master_profile (Admin_ID, User_Name, Password, Reg_Date, Login_Date, User_Access) VALUES (?, ?, ?, ?, ?, 1)");
$stmt->bind_param('sssss', $adminId, $username, $passwordHash, $dateTime, $dateTime);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
log_activity($mysqli, 'create', 'master', $newId, $username, 'Master created via API');
$stmt = $mysqli->prepare("SELECT * FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $newId);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
unset($item['Password'], $item['salt']);
Response::created($item);
}
function updateMaster(?string $id, ?array $body, mysqli $mysqli): void
{
Auth::requireRole('admin_profile');
if (!$id) Response::error('Master ID required');
$stmt = $mysqli->prepare("SELECT * FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$existing = $result->fetch_assoc();
$stmt->close();
if (!$existing) Response::notFound('Master not found');
$userAccess = (int)($body['User_Access'] ?? $existing['User_Access']);
$stmt = $mysqli->prepare("UPDATE master_profile SET User_Access = ? WHERE Master_ID = ?");
$stmt->bind_param('ii', $userAccess, $id);
$stmt->execute();
$stmt->close();
if (!empty($body['password'])) {
$newHash = password_hash($body['password'], PASSWORD_BCRYPT);
$stmt = $mysqli->prepare("UPDATE master_profile SET Password = ?, salt = '' WHERE Master_ID = ?");
$stmt->bind_param('si', $newHash, $id);
$stmt->execute();
$stmt->close();
}
log_activity($mysqli, 'update', 'master', $id, $existing['User_Name'], 'Master updated via API');
$stmt = $mysqli->prepare("SELECT * FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
unset($item['Password'], $item['salt']);
Response::success($item, 'Master updated');
}
function deleteMaster(?string $id, mysqli $mysqli): void
{
Auth::requireRole('admin_profile');
if (!$id) Response::error('Master ID required');
$stmt = $mysqli->prepare("SELECT User_Name FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($name);
$stmt->fetch();
$stmt->close();
if (!$name) Response::notFound('Master not found');
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM manager_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($mCount);
$stmt->fetch();
$stmt->close();
if ($mCount > 0) {
Response::error("Cannot delete master: $mCount manager(s) are assigned", 409);
}
$stmt = $mysqli->prepare("DELETE FROM master_profile WHERE Master_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
log_activity($mysqli, 'delete', 'master', $id, $name, 'Master deleted via API');
Response::success(null, 'Master deleted');
}