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