From 63734c87bc1d758f1b8f5a496cbdc69ca575b9ee Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 16 Oct 2026 03:40:21 -0700 Subject: [PATCH] add settings table to database --- api/database.py | 48 ++++++++++ web/add_provider.php | 107 ++++++++++++++++++++++ web/{providers.php => list_providers.php} | 0 3 files changed, 155 insertions(+) create mode 100644 web/add_provider.php rename web/{providers.php => list_providers.php} (100%) 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.'; + } + } +} +?> + + + + +Add Provider + + + + +
+

Add New Provider

+ +
+ +

+ +
+ + +
+

Provider created successfully with ID .

+
+ +
+ + + + + + + +
+
+ + diff --git a/web/providers.php b/web/list_providers.php similarity index 100% rename from web/providers.php rename to web/list_providers.php