prepare( 'INSERT INTO gallery_photos (gallery_project_id, image_path, caption, sort_order) VALUES (?, ?, ?, ?)' ); $stmt->execute([ $projectId, $imagePath, trim($body['caption'] ?? '') ?: null, (int) ($body['sort_order'] ?? 0), ]); Http::send(['id' => (int) $db->lastInsertId()], 201); } public static function update(array $params): void { $id = (int) $params['id']; $body = Http::jsonBody(); $stmt = Database::connection()->prepare( 'UPDATE gallery_photos SET caption = ?, sort_order = ? WHERE id = ?' ); $stmt->execute([ trim($body['caption'] ?? '') ?: null, (int) ($body['sort_order'] ?? 0), $id, ]); Http::send(['ok' => true]); } public static function destroy(array $params): void { $id = (int) $params['id']; $db = Database::connection(); $oldStmt = $db->prepare('SELECT image_path FROM gallery_photos WHERE id = ?'); $oldStmt->execute([$id]); $oldImagePath = $oldStmt->fetchColumn(); $stmt = $db->prepare('DELETE FROM gallery_photos WHERE id = ?'); $stmt->execute([$id]); Uploads::deleteIfOrphaned($oldImagePath ?: null, null); Http::send(['ok' => true]); } }