41 lines
1016 B
JavaScript
41 lines
1016 B
JavaScript
const express = require("express");
|
|
const Photo = require("../models/Photo");
|
|
|
|
const router = express.Router();
|
|
|
|
router.get("/", async (req, res) => {
|
|
try {
|
|
const filter = {};
|
|
if (req.query.active === "true") {
|
|
filter.active = true;
|
|
}
|
|
const photos = await Photo.find(filter).sort({ createdAt: -1 });
|
|
res.json(photos);
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Failed to fetch photos" });
|
|
}
|
|
});
|
|
|
|
router.post("/", async (req, res) => {
|
|
try {
|
|
const photo = await Photo.create(req.body);
|
|
res.status(201).json(photo);
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to create photo" });
|
|
}
|
|
});
|
|
|
|
router.delete("/:id", async (req, res) => {
|
|
try {
|
|
const photo = await Photo.findByIdAndDelete(req.params.id);
|
|
if (!photo) {
|
|
return res.status(404).json({ message: "Photo not found" });
|
|
}
|
|
res.json({ ok: true });
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to delete photo" });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|