add settings table to database
This commit is contained in:
1 parent
602d7d4aa4
commit
63734c87bc
3 files changed
+155
No files matched your search
@@ -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()
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
// add_providers.php
|
||||
// Simple page that displays a form to create a new fitness provider.
|
||||
// It uses the same Flask API as list_providers.php (http://172.25.1.105:5000).
|
||||
// When the form is submitted, it POSTs JSON to /providers.
|
||||
|
||||
// Helper to call the API -------------------------------------------
|
||||
function callApi(string $method, string $path, $body = null)
|
||||
{
|
||||
$url = 'http://172.25.1.105:5000' . $path;
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
|
||||
if ($body !== null) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode >= 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.';
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Add Provider</title>
|
||||
<link rel="stylesheet" href="https://cosmostat.matt-cloud.com/src/styles.css">
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 2rem; }
|
||||
form { max-width: 600px; }
|
||||
label { display: block; margin-top: 1rem; }
|
||||
input[type=text], input[type=number] { width: 100%; padding: 0.5rem; }
|
||||
.error { color: red; }
|
||||
.success { color: green; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>Add New Provider</h1>
|
||||
<?php if ($errors): ?>
|
||||
<div class="error">
|
||||
<?php foreach ($errors as $e): ?>
|
||||
<p><?=htmlspecialchars($e)?></p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($created): ?>
|
||||
<div class="success">
|
||||
<p>Provider created successfully with ID <?=htmlspecialchars($created['id'] ?? '')?>.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<form method="post" action="">
|
||||
<label>Name:<br><input type="text" name="name" value="<?=htmlspecialchars($_POST['name'] ?? '')?>" required></label>
|
||||
<label>Phone:<br><input type="text" name="phone" value="<?=htmlspecialchars($_POST['phone'] ?? '')?>"></label>
|
||||
<label>Age:<br><input type="number" name="age" value="<?=htmlspecialchars($_POST['age'] ?? '')?>"></label>
|
||||
<label>Photo URL:<br><input type="text" name="photo" value="<?=htmlspecialchars($_POST['photo'] ?? '')?>"></label>
|
||||
<label>Rates:<br><input type="text" name="rates" value="<?=htmlspecialchars($_POST['rates'] ?? '')?>"></label>
|
||||
<label><input type="checkbox" name="private" <?=isset($_POST['private'])?'checked':''?>> Private</label>
|
||||
<button type="submit">Create Provider</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
File renamed without changes.
Reference in new issue
Block a user