92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
import { Router } from 'express';
|
|
import {
|
|
getAllRecipes, getRecipeById, createRecipe,
|
|
updateRecipe, deleteRecipe, getCategories
|
|
} from '../db.js';
|
|
import { getTikTokData } from '../services/tiktok.js';
|
|
import { isOllamaAvailable, extractRecipe } from '../services/ai.js';
|
|
|
|
const router = Router();
|
|
|
|
router.get('/', (req, res) => {
|
|
const { search, category } = req.query;
|
|
const recipes = getAllRecipes(search, category);
|
|
res.json(recipes.map(r => ({
|
|
...r,
|
|
ingredients: JSON.parse(r.ingredients),
|
|
steps: JSON.parse(r.steps),
|
|
})));
|
|
});
|
|
|
|
router.get('/categories', (req, res) => {
|
|
res.json(getCategories());
|
|
});
|
|
|
|
router.get('/ai-status', async (req, res) => {
|
|
const available = await isOllamaAvailable();
|
|
res.json({ ollama: available });
|
|
});
|
|
|
|
router.get('/:id', (req, res) => {
|
|
const recipe = getRecipeById(req.params.id);
|
|
if (!recipe) return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
res.json({
|
|
...recipe,
|
|
ingredients: JSON.parse(recipe.ingredients),
|
|
steps: JSON.parse(recipe.steps),
|
|
});
|
|
});
|
|
|
|
router.post('/', (req, res) => {
|
|
const recipe = createRecipe(req.body);
|
|
res.status(201).json(recipe);
|
|
});
|
|
|
|
router.put('/:id', (req, res) => {
|
|
const existing = getRecipeById(req.params.id);
|
|
if (!existing) return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
const updated = updateRecipe(req.params.id, req.body);
|
|
res.json(updated);
|
|
});
|
|
|
|
router.delete('/:id', (req, res) => {
|
|
const existing = getRecipeById(req.params.id);
|
|
if (!existing) return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
deleteRecipe(req.params.id);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
router.post('/from-tiktok', async (req, res) => {
|
|
try {
|
|
const { url } = req.body;
|
|
if (!url) return res.status(400).json({ error: 'URL erforderlich' });
|
|
|
|
const tiktokData = await getTikTokData(url);
|
|
|
|
const ollamaAvailable = await isOllamaAvailable();
|
|
let extracted = null;
|
|
let aiStatus = ollamaAvailable ? 'available' : 'unavailable';
|
|
|
|
if (ollamaAvailable && tiktokData.title) {
|
|
try {
|
|
extracted = await extractRecipe(tiktokData.title);
|
|
aiStatus = 'success';
|
|
} catch (e) {
|
|
console.error('Ollama Extraktion fehlgeschlagen:', e.message);
|
|
aiStatus = 'error';
|
|
}
|
|
}
|
|
|
|
res.json({
|
|
tiktok: tiktokData,
|
|
ollama: ollamaAvailable,
|
|
aiStatus,
|
|
extracted,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
export default router;
|