Refactor database handling to require PostgreSQL connection, removing fallback to JSON. Implement error handling for database unavailability in key files. Update .env.example to reflect mandatory DATABASE_URL for local development. Remove Firebase configuration and related code from the project.

This commit is contained in:
2026-05-23 10:23:28 +02:00
parent 547778bba5
commit 6577db475a
20 changed files with 823 additions and 789 deletions
+37 -37
View File
@@ -1,6 +1,12 @@
<?php
require __DIR__ . '/helpers.php';
try {
$allRecipes = load_recipes();
} catch (DatabaseUnavailableException $e) {
handle_database_unavailable($e);
}
// Language & inputs
$lang = strtolower((string) (filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?: 'en')) === 'de' ? 'de' : 'en';
$q = trim((string) (filter_input(INPUT_GET, 'q', FILTER_UNSAFE_RAW) ?? ''));
@@ -84,7 +90,6 @@ $copy = [
$t = $copy[$lang];
// Data
$allRecipes = load_recipes();
$localizedAll = localize_recipes($allRecipes, $lang);
$recipes = filter_recipes($allRecipes, $q ?: null, $tag ?: null, $lang);
$recipes = array_values(array_filter($recipes, fn($r) => empty($r['coming_soon'])));
@@ -920,43 +925,38 @@ $recipesJson = json_encode(array_values(array_map(function($r) use ($lang) {
});
}
// Hook into auth state
window.addEventListener('DOMContentLoaded', function() {
if (window.auth && window.db) {
window.auth.onAuthStateChanged(function(user) {
if (user) {
window.db.collection('users').doc(user.uid).get()
.then(function(doc) {
if (doc.exists) {
var goal = doc.data().goal;
var filtered = indexRecipeBank;
if (goal === 'weight_loss') {
filtered = indexRecipeBank.filter(r =>
r.tags.some(t => t.toLowerCase().includes('low-carb') || t.toLowerCase().includes('diet')) ||
(r.category && r.category.toLowerCase().includes('salad'))
);
} else if (goal === 'muscle_gain') {
filtered = indexRecipeBank.filter(r =>
r.tags.some(t => t.toLowerCase().includes('high-protein') || t.toLowerCase().includes('meat') || t.toLowerCase().includes('fleisch'))
);
} else if (goal === 'healthy') {
filtered = indexRecipeBank.filter(r =>
r.tags.some(t => t.toLowerCase().includes('vegetarian') || t.toLowerCase().includes('healthy'))
);
}
// fallback if empty
if (filtered.length === 0) filtered = indexRecipeBank;
renderSwipeCarousel(filtered, i18nGoal[goal] || null);
}
});
} else {
// Default unauthenticated view
renderSwipeCarousel(indexRecipeBank, null);
}
function filterRecipesByGoal(goal) {
var filtered = indexRecipeBank;
if (goal === 'weight_loss') {
filtered = indexRecipeBank.filter(function(r) {
return r.tags.some(function(t) {
var lower = t.toLowerCase();
return lower.includes('low-carb') || lower.includes('diet');
}) || (r.category && r.category.toLowerCase().includes('salad'));
});
} else if (goal === 'muscle_gain') {
filtered = indexRecipeBank.filter(function(r) {
return r.tags.some(function(t) {
var lower = t.toLowerCase();
return lower.includes('high-protein') || lower.includes('meat') || lower.includes('fleisch');
});
});
} else if (goal === 'healthy') {
filtered = indexRecipeBank.filter(function(r) {
return r.tags.some(function(t) {
var lower = t.toLowerCase();
return lower.includes('vegetarian') || lower.includes('healthy');
});
});
}
if (filtered.length === 0) filtered = indexRecipeBank;
return filtered;
}
window.addEventListener('DOMContentLoaded', function() {
var goal = window.fcLocal ? window.fcLocal.getGoal() : '';
if (goal) {
renderSwipeCarousel(filterRecipesByGoal(goal), i18nGoal[goal] || null);
} else {
renderSwipeCarousel(indexRecipeBank, null);
}