vor Import/Export

This commit is contained in:
2025-10-24 12:39:10 +02:00
parent 77ad6b3823
commit 8abe0ca966
6 changed files with 214 additions and 113 deletions
+38 -69
View File
@@ -211,7 +211,7 @@ function issue_tokens(PDO $pdo, int $uid): array {
// =============================================================================
function igel_list(PDO $pdo, int $uid): void {
$stmt = $pdo->prepare('SELECT id, name, gender, feature, note, created_at, updated_at FROM igel WHERE user_id = ? ORDER BY created_at DESC');
$stmt = $pdo->prepare('SELECT id, name, gender, feature, note, rescued_at, location, created_at, updated_at FROM igel WHERE user_id = ? ORDER BY created_at DESC');
$stmt->execute([$uid]);
json($stmt->fetchAll(PDO::FETCH_ASSOC));
}
@@ -222,16 +222,36 @@ function igel_create(PDO $pdo, int $uid): void {
$gender = isset($in['gender']) ? (string)$in['gender'] : null;
$feature = isset($in['feature']) ? (string)$in['feature'] : null;
$note = isset($in['note']) ? (string)$in['note'] : null;
$rescuedAt = isset($in['rescued_at']) ? (string)$in['rescued_at'] : null; // "YYYY-MM-DD"
$location = isset($in['location']) ? trim((string)$in['location']) : null;
if ($name === '') { json(['error' => 'Name required'], 422); return; }
$stmt = $pdo->prepare('INSERT INTO igel(user_id, name, gender, feature, note) VALUES(?,?,?,?,?)');
$stmt->execute([$uid, $name, $gender, $feature, $note]);
if ($rescuedAt !== null && $rescuedAt !== '' && strtotime($rescuedAt) === false) {
json(['error' => 'Invalid rescued_at (expected YYYY-MM-DD)'], 422); return;
}
if ($location !== null && strlen($location) > 255) {
json(['error' => 'Location too long (max 255)'], 422); return;
}
$stmt = $pdo->prepare(
'INSERT INTO igel(user_id, name, gender, feature, note, rescued_at, location)
VALUES(?,?,?,?,?,?,?)'
);
$stmt->execute([$uid, $name, $gender, $feature, $note, $rescuedAt ?: null, $location ?: null]);
$id = (int)$pdo->lastInsertId();
json(['id' => $id, 'name' => $name, 'gender' => $gender, 'feature' => $feature, 'note' => $note], 201);
json([
'id' => $id,
'name' => $name,
'gender' => $gender,
'feature' => $feature,
'note' => $note,
'rescued_at' => $rescuedAt,
'location' => $location
], 201);
}
function igel_get(PDO $pdo, int $uid, int $id): void {
$stmt = $pdo->prepare('SELECT id, name, gender, feature, note, created_at, updated_at FROM igel WHERE id=? AND user_id=?');
$stmt = $pdo->prepare('SELECT id, name, gender, feature, note, rescued_at, location, created_at, updated_at FROM igel WHERE id=? AND user_id=?');
$stmt->execute([$id, $uid]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) { json(['error' => 'Not found'], 404); return; }
@@ -244,10 +264,21 @@ function igel_update(PDO $pdo, int $uid, int $id): void {
$gender = isset($in['gender']) ? (string)$in['gender'] : null;
$feature = isset($in['feature']) ? (string)$in['feature'] : null;
$note = isset($in['note']) ? (string)$in['note'] : null;
$rescuedAt = array_key_exists('rescued_at', $in) ? (string)$in['rescued_at'] : null;
$location = array_key_exists('location', $in) ? trim((string)$in['location']) : null;
if ($name === '') { json(['error' => 'Name required'], 422); return; }
$stmt = $pdo->prepare('UPDATE igel SET name=?, gender=?, feature=?, note=? WHERE id=? AND user_id=?');
$stmt->execute([$name, $gender, $feature, $note, $id, $uid]);
if ($rescuedAt !== null && $rescuedAt !== '' && strtotime($rescuedAt) === false) {
json(['error' => 'Invalid rescued_at (expected YYYY-MM-DD)'], 422); return;
}
if ($location !== null && strlen($location) > 255) {
json(['error' => 'Location too long (max 255)'], 422); return;
}
$stmt = $pdo->prepare(
'UPDATE igel SET name=?, gender=?, feature=?, note=?, rescued_at=?, location=? WHERE id=? AND user_id=?'
);
$stmt->execute([$name, $gender, $feature, $note, $rescuedAt ?: null, $location ?: null, $id, $uid]);
json(['ok' => true]);
}
@@ -625,65 +656,3 @@ function jwt_decode(string $token, string $secret): array {
if (isset($payload['exp']) && time() >= (int)$payload['exp']) throw new Exception('exp');
return $payload;
}
/*
-- SQL Reference (run once)
CREATE TABLE users (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(191) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE refresh_tokens (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
token VARCHAR(255) NOT NULL UNIQUE,
expires_at DATETIME NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX (user_id), INDEX (token)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE igel (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(120) NOT NULL,
gender VARCHAR(30) NULL,
feature VARCHAR(255) NULL,
note TEXT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX (user_id), INDEX (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE igel_images (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
igel_id BIGINT UNSIGNED NOT NULL,
url VARCHAR(500) NOT NULL,
thumb_url VARCHAR(500) NULL,
original_name VARCHAR(255) NULL,
mime VARCHAR(100) NULL,
size_bytes BIGINT UNSIGNED NULL,
taken_at DATETIME NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (igel_id) REFERENCES igel(id) ON DELETE CASCADE,
INDEX (igel_id),
INDEX (taken_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE messwerte (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
igel_id BIGINT UNSIGNED NOT NULL,
datum DATETIME NOT NULL,
gewicht INT UNSIGNED NOT NULL,
behandlung VARCHAR(255) NULL,
bemerkung TEXT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (igel_id) REFERENCES igel(id) ON DELETE CASCADE,
INDEX (igel_id), INDEX (datum)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
*/
?>