diff --git a/api/database.py b/api/database.py index 7412cd2..818f2e3 100644 --- a/api/database.py +++ b/api/database.py @@ -68,6 +68,15 @@ def initialize_db() -> None: ); """ ) + cursor.execute( + """ + CREATE TABLE IF NOT EXISTS settings ( + name TEXT PRIMARY KEY, + value TEXT + ); + """ + ) + conn.commit() # --------------------------------------------------------------------------- @@ -276,6 +285,45 @@ def update_visit(id: int, user_ids: Optional[List[int]] = None, provider_ids: Op conn.commit() return {"status": "ok", "id": id} +# --- Settings helper functions --- + +def add_setting(name: str, value: str) -> Dict[str, Any]: + """Insert a new setting. If a setting with the same name exists, it will be replaced. + Returns a status dictionary. + """ + with _get_connection() as conn: + cursor = conn.execute( + "INSERT OR REPLACE INTO settings (name, value) VALUES (?, ?)", + (name, value), + ) + conn.commit() + return {"status": "ok", "name": name} + + +def change_setting(name: str, value: str) -> Dict[str, Any]: + """Update the value of an existing setting. If the setting does not exist, return not-found. + """ + with _get_connection() as conn: + cursor = conn.execute( + "UPDATE settings SET value = ? WHERE name = ?", + (value, name), + ) + conn.commit() + if cursor.rowcount == 0: + return {"status": "not-found", "name": name} + return {"status": "ok", "name": name} + + +def view_settings(names: List[str] | None = None) -> List[Dict[str, Any]]: + """Return a list of settings. + If names is provided, only those names are returned. + """ + if not names: + return _select("settings") + placeholders = ", ".join(["?" for _ in names]) + rows = _select("settings", f"name IN ({placeholders})", tuple(names)) + return rows + # Example usage (to be removed or guarded by ``if __name__ == '__main__'``) if __name__ == "__main__": initialize_db() diff --git a/web/add_provider.php b/web/add_provider.php new file mode 100644 index 0000000..1c3d785 --- /dev/null +++ b/web/add_provider.php @@ -0,0 +1,107 @@ += 400) { + return null; // or throw an exception + } + + return json_decode($response, true); +} + +$created = null; +$errors = []; + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Collect form data + $name = trim($_POST['name'] ?? ''); + $phone = trim($_POST['phone'] ?? ''); + $age = trim($_POST['age'] ?? ''); + $photo = trim($_POST['photo'] ?? ''); + $rates = trim($_POST['rates'] ?? ''); + $private = isset($_POST['private']) ? true : false; + + // Basic validation + if ($name === '') { $errors[] = 'Name is required.'; } + if ($age !== '' && !is_numeric($age)) { $errors[] = 'Age must be a number.'; } + if ($rates !== '' && !is_numeric($rates)) { $errors[] = 'Rates must be a number.'; } + + if (empty($errors)) { + $body = [ + 'name' => $name, + 'phone' => $phone, + 'age' => $age === '' ? null : (int)$age, + 'photo' => $photo === '' ? null : $photo, + 'rates' => $rates === '' ? null : (float)$rates, + 'private'=> $private + ]; + $result = callApi('POST', '/providers', $body); + if ($result) { + $created = $result; // API returns created provider object + } else { + $errors[] = 'Failed to create provider. API error.'; + } + } +} +?> + + +
+ +=htmlspecialchars($e)?>
+ +Provider created successfully with ID =htmlspecialchars($created['id'] ?? '')?>.
+