query("SELECT 1 FROM cities WHERE {$cond}");
sendResponseAndExitIf($mysqli->affected_rows>0, 403, "CANNOT DELETE COUNTRIES WITH CITIES OR REGIONS");
$mysqli->query("DELETE FROM countries WHERE {$cond}");
sendResponseAndExitIf($mysqli->affected_rows==0, 404, "NOT FOUND");
sendResponseAndExitIf(true, 204, "DELETED");
case "GET":
$countryCode = $_REQUEST['countrycode'] ?: "";
$countryNameLike = $_REQUEST['countrynamelike'] ?: "";
$cond = empty($countryCode) ? "true" : "countryCode IN ('" . str_replace (",", "','", $countryCode) . "')";
$cond .= empty($countryNameLike) ? "" : " AND countryName LIKE '%{$countryNameLike}%'";
$res= $mysqli->query("SELECT * FROM countries WHERE {$cond}");
sendResponseAndExitIf($mysqli->affected_rows==0, 404, "NOT FOUND");
for ($xml= ""; $row = $res->fetch_assoc(); ) {
$xml .= "";
$xml .= "";
$xml .= "{$row["countryCode"]}";
$xml .= "".xmlFormat($row["countryName"])."";
$xml .= "";
$res2 = $mysqli->query("SELECT * FROM regions WHERE countryCode='{$row["countryCode"]}' ORDER BY regionName, regionCode");
while ($row2 = $res2->fetch_assoc()) {
$xml .= "\n";
}
$xml .= "";
$xml .= "";
}
sendResponseAndExitIf(true, 200, "OK", "", "".$xml."");
case "POST":
case "PUT":
$countryCode = $_REQUEST['countrycode'] ?: "";
$countryName = $_REQUEST['countryname'] ?: "";
sendResponseAndExitIf(empty($countryCode) && $method=="POST", 405, "MUST SPECIFY COUNTRY CODE", "Allowed: GET,PUT,DELETE");
sendResponseAndExitIf(empty($countryCode), 403, "MUST SPECIFY COUNTRY CODE");
sendResponseAndExitIf(empty($countryName), 403, "MUST SPECIFY COUNTRY NAME");
$mysqli->query("INSERT INTO countries (countryCode, countryName) VALUE ('{$countryCode}', '{$countryName}') ");
sendResponseAndExitIf($mysqli->affected_rows>0, 201, "CREATED", "Location: {$baseUrl}/countries/{$countryCode}");
$mysqli->query("UPDATE countries SET countryName='{$countryName}' WHERE countryCode='{$countryCode}'");
sendResponseAndExitIf($mysqli->affected_rows>0, 204, "UPDATED", "Location: {$baseUrl}/countries/{$countryCode}");
sendResponseAndExitIf(true, 409, "COULDN'T UPDATE COUNTRY...");
default:
sendResponseAndExitIf(true, 400, "UNKNOWN METHOD {$method}");
}