Neue API fuer Gallery
This commit is contained in:
parent
a28e4bb593
commit
8291e201c3
66
index.js
66
index.js
@ -779,6 +779,72 @@ app.post("/api/players/:id/assign-team", async (req, res) => {
|
||||
|
||||
|
||||
|
||||
//Multer Storage fuer Galleriebilder
|
||||
const galleryStorage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
const dir = "./uploads/gallery";
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
cb(null, dir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const ext = path.extname(file.originalname);
|
||||
const filename = Date.now() + ext;
|
||||
cb(null, filename);
|
||||
},
|
||||
});
|
||||
const uploadGalleryImage = multer({ storage: galleryStorage });
|
||||
|
||||
|
||||
//Gallery Image Upload
|
||||
app.post("/api/gallery", uploadGalleryImage.single("image"), async (req, res) => {
|
||||
const { title } = req.body;
|
||||
if (!req.file) return res.status(400).send("Kein Bild hochgeladen");
|
||||
|
||||
const imageUrl = `/uploads/gallery/${req.file.filename}`;
|
||||
|
||||
try {
|
||||
const result = await pool.query(
|
||||
"INSERT INTO gallery_images (image_url, title) VALUES ($1, $2) RETURNING *",
|
||||
[imageUrl, title || null]
|
||||
);
|
||||
res.status(201).json(result.rows[0]);
|
||||
} catch (err) {
|
||||
console.error("Fehler beim Speichern des Galeriebilds:", err);
|
||||
res.status(500).send("Fehler beim Speichern");
|
||||
}
|
||||
});
|
||||
|
||||
//Get all gallery images
|
||||
app.get("/api/gallery", async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query("SELECT * FROM gallery_images ORDER BY uploaded_at DESC");
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
console.error("Fehler beim Laden der Galerie:", err);
|
||||
res.status(500).send("Fehler beim Laden");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//Delete image
|
||||
app.delete("/api/gallery/:id", async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
const result = await pool.query("DELETE FROM gallery_images WHERE id = $1 RETURNING *", [id]);
|
||||
|
||||
if (result.rowCount === 0) return res.status(404).send("Bild nicht gefunden");
|
||||
|
||||
// Optional: Datei auch vom Dateisystem löschen
|
||||
const filePath = path.join(__dirname, result.rows[0].image_url);
|
||||
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
|
||||
|
||||
res.send("Bild erfolgreich gelöscht");
|
||||
} catch (err) {
|
||||
console.error("Fehler beim Löschen des Bildes:", err);
|
||||
res.status(500).send("Fehler beim Löschen");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Server starten
|
||||
|
||||
BIN
uploads/news/1745994332500.jpg
Normal file
BIN
uploads/news/1745994332500.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
Loading…
Reference in New Issue
Block a user