first commit

This commit is contained in:
2026-01-26 10:54:31 +03:30
commit 1db3038221
20 changed files with 4026 additions and 0 deletions

159
routes/placeRoutes.js Normal file
View File

@@ -0,0 +1,159 @@
const express = require("express");
const router = express.Router();
const Place = require("../models/place");
const UpdateData = require("../models/UpdateData");
const { verifyToken } = require("../lib/jwtUtils");
router.use(verifyToken);
router.post("/place", async (req, res) => {
try {
const { name, description, location, category } = req.body;
const newPlace = new Place({ name, description, location, category });
await newPlace.save();
res
.status(201)
.json({ message: "Place created successfully", place: newPlace });
} catch (error) {
console.error("Error creating place:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/place_data/:placeId", async (req, res) => {
try {
const placeId = req.params.placeId;
const place = await Place.findById(placeId);
if (!place) {
return res.status(404).json({ message: "Place not found" });
}
res.status(200).json({ place });
} catch (error) {
console.error("Error retrieving place:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/places", async (req, res) => {
try {
const places = await Place.find();
res.status(200).json(places);
} catch (error) {
console.error("Error retrieving places:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.delete("/places/:placeId", async (req, res) => {
try {
const placeId = req.params.placeId;
const deletedPlace = await Place.findByIdAndDelete(placeId);
if (!deletedPlace) {
return res.status(404).json({ message: "Place not found" });
}
res
.status(200)
.json({ message: "Place deleted successfully", place: deletedPlace });
} catch (error) {
console.error("Error deleting place:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.put("/places/:placeId", async (req, res) => {
try {
const placeId = req.params.placeId;
const { name, description, location, category } = req.body;
const updatedPlace = await Place.findByIdAndUpdate(
placeId,
{ name, description, location, category },
{ new: true } // Set { new: true } to return the updated document
);
if (!updatedPlace) {
return res.status(404).json({ message: "Place not found" });
}
res
.status(200)
.json({ message: "Place updated successfully", place: updatedPlace });
} catch (error) {
console.error("Error updating place:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.post("/update-data", async (req, res) => {
const { province } = req.body;
if (!province) {
return res.status(400).send("Province is required");
}
let api = `https://pos.rasadyaar.ir/api/report/pos/${province}`;
console.log("Fetching data from:", api);
try {
const response = await fetch(api);
const newProvinceData = await response.json();
await UpdateData.updateOne(
{},
{ $set: { [`records.${province}`]: newProvinceData } },
{ upsert: true }
);
res.status(200).send(`Data for ${province} successfully updated!`);
} catch (error) {
console.error("Error fetching or updating province data:", error);
res.status(500).send("Failed to update data.");
}
});
router.get("/stewards/:key", async (req, res) => {
const { key } = req.params;
try {
const data = await UpdateData.findOne();
if (data && data.records) {
const recordData = data.records.get(key);
if (recordData) {
res.status(200).json(recordData);
} else {
res.status(404).json({ message: `No data found for key: ${key}` });
}
} else {
res.status(404).json({ message: "No data found" });
}
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server error" });
}
});
module.exports = router;
// router
// .route("/:id")
// .get((req, res) => {
// res.send(`User Get ${req.params.id}`);
// })
// .post((req, res) => {
// res.send(`User Get ${req.params.id}`);
// })
// .put((req, res) => {
// res.send(`User Get ${req.params.id}`);
// })
// .delete((req, res) => {
// res.send(`User Get ${req.params.id}`);
// });
// module.exports = router;