Initial commit
This commit is contained in:
40
backend/routes/photos.js
Normal file
40
backend/routes/photos.js
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user