38671-vm/api/supplements.php
2026-02-21 16:43:49 +00:00

41 lines
1.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$pdo = db();
$action = $_GET['action'] ?? 'get_status';
$today = date('Y-m-d');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
if ($action === 'log') {
$id = $input['id'] ?? null;
$name = $input['name'] ?? '';
$stmt = $pdo->prepare("INSERT INTO supplement_logs (supplement_id, name, taken_at) VALUES (?, ?, ?)");
$stmt->execute([$id, $name, $today]);
echo json_encode(['success' => true]);
exit;
}
}
if ($action === 'get_status') {
// Get all supplements
$stmt = $pdo->query("SELECT * FROM supplement_list ORDER BY name ASC");
$list = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get today's logs
$stmt = $pdo->prepare("SELECT name FROM supplement_logs WHERE taken_at = ?");
$stmt->execute([$today]);
$taken = $stmt->fetchAll(PDO::FETCH_COLUMN);
$results = [];
foreach ($list as $sup) {
$sup['taken'] = in_array($sup['name'], $taken);
$results[] = $sup;
}
echo json_encode($results);
exit;
}