178 lines
4.3 KiB
JavaScript
178 lines
4.3 KiB
JavaScript
kimport express from 'express';
|
|
import { ObjectId } from 'mongodb';
|
|
import { getDB } from '../db.js';
|
|
|
|
const router = express.Router();
|
|
|
|
// Alle Rezepte abrufen (mit Suche und Sortierung)
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const db = getDB();
|
|
const { search, sort = 'rezeptnummer' } = req.query;
|
|
|
|
let query = {};
|
|
if (search) {
|
|
query.$text = { $search: search };
|
|
}
|
|
|
|
const sortField = ['rezeptnummer', 'bezeichnung', 'kategorie'].includes(sort)
|
|
? sort
|
|
: 'rezeptnummer';
|
|
|
|
const recipes = await db.collection('recipes')
|
|
.find(query)
|
|
.sort({ [sortField]: 1 })
|
|
.project({ _id: 1, rezeptnummer: 1, bezeichnung: 1, kategorie: 1 })
|
|
.toArray();
|
|
|
|
res.json(recipes);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Einzelnes Rezept abrufen
|
|
router.get('/:id', async (req, res) => {
|
|
try {
|
|
const db = getDB();
|
|
const recipe = await db.collection('recipes').findOne({
|
|
_id: new ObjectId(req.params.id)
|
|
});
|
|
|
|
if (!recipe) {
|
|
return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
}
|
|
|
|
// Bilder abrufen
|
|
const images = await db.collection('images')
|
|
.find({ rezeptId: new ObjectId(req.params.id) })
|
|
.sort({ _id: 1 })
|
|
.toArray();
|
|
|
|
res.json({ ...recipe, bilder: images });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Neues Rezept erstellen
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const db = getDB();
|
|
const {
|
|
rezeptnummer,
|
|
bezeichnung,
|
|
beschreibung,
|
|
kategorie,
|
|
vorbereitung,
|
|
anzahl,
|
|
zutaten,
|
|
zubereitung,
|
|
kommentar
|
|
} = req.body;
|
|
|
|
// Zubereitungsschritte aufteilen
|
|
const zubereitungArray = zubereitung
|
|
.split('\n')
|
|
.map(step => step.trim())
|
|
.filter(step => step.length > 0)
|
|
.map((text, index) => ({ schritt: index + 1, text }));
|
|
|
|
const recipe = {
|
|
rezeptnummer: parseInt(rezeptnummer),
|
|
bezeichnung,
|
|
beschreibung,
|
|
kategorie,
|
|
vorbereitung,
|
|
anzahl: parseInt(anzahl) || 2,
|
|
zutaten,
|
|
zubereitung: zubereitungArray,
|
|
kommentar,
|
|
erstelltAm: new Date(),
|
|
aktualisiertAm: new Date()
|
|
};
|
|
|
|
const result = await db.collection('recipes').insertOne(recipe);
|
|
res.status(201).json({ _id: result.insertedId, ...recipe });
|
|
} catch (error) {
|
|
if (error.code === 11000) {
|
|
res.status(400).json({ error: 'Rezeptnummer existiert bereits' });
|
|
} else {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
}
|
|
});
|
|
|
|
// Rezept aktualisieren
|
|
router.put('/:id', async (req, res) => {
|
|
try {
|
|
const db = getDB();
|
|
const {
|
|
rezeptnummer,
|
|
bezeichnung,
|
|
beschreibung,
|
|
kategorie,
|
|
vorbereitung,
|
|
anzahl,
|
|
zutaten,
|
|
zubereitung,
|
|
kommentar
|
|
} = req.body;
|
|
|
|
const zubereitungArray = zubereitung
|
|
.split('\n')
|
|
.map(step => step.trim())
|
|
.filter(step => step.length > 0)
|
|
.map((text, index) => ({ schritt: index + 1, text }));
|
|
|
|
const updateData = {
|
|
rezeptnummer: parseInt(rezeptnummer),
|
|
bezeichnung,
|
|
beschreibung,
|
|
kategorie,
|
|
vorbereitung,
|
|
anzahl: parseInt(anzahl) || 2,
|
|
zutaten,
|
|
zubereitung: zubereitungArray,
|
|
kommentar,
|
|
aktualisiertAm: new Date()
|
|
};
|
|
|
|
const result = await db.collection('recipes').updateOne(
|
|
{ _id: new ObjectId(req.params.id) },
|
|
{ $set: updateData }
|
|
);
|
|
|
|
if (result.matchedCount === 0) {
|
|
return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
}
|
|
|
|
res.json({ message: 'Rezept aktualisiert' });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Rezept löschen
|
|
router.delete('/:id', async (req, res) => {
|
|
try {
|
|
const db = getDB();
|
|
const recipeId = new ObjectId(req.params.id);
|
|
|
|
// Lösche zugehörige Bilder
|
|
await db.collection('images').deleteMany({ rezeptId: recipeId });
|
|
|
|
const result = await db.collection('recipes').deleteOne({ _id: recipeId });
|
|
|
|
if (result.deletedCount === 0) {
|
|
return res.status(404).json({ error: 'Rezept nicht gefunden' });
|
|
}
|
|
|
|
res.json({ message: 'Rezept gelöscht' });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
export default router;
|