Initial commit
This commit is contained in:
115
backend/routes/schedules.js
Normal file
115
backend/routes/schedules.js
Normal file
@@ -0,0 +1,115 @@
|
||||
const express = require("express");
|
||||
const Schedule = require("../models/Schedule");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const startOfWeek = (date = new Date()) => {
|
||||
const copy = new Date(date);
|
||||
const day = copy.getDay();
|
||||
const diff = day === 0 ? -6 : 1 - day;
|
||||
copy.setDate(copy.getDate() + diff);
|
||||
copy.setHours(0, 0, 0, 0);
|
||||
return copy;
|
||||
};
|
||||
|
||||
const endOfWeek = (date = new Date()) => {
|
||||
const start = startOfWeek(date);
|
||||
const end = new Date(start);
|
||||
end.setDate(end.getDate() + 6);
|
||||
end.setHours(23, 59, 59, 999);
|
||||
return end;
|
||||
};
|
||||
|
||||
const startOfMonth = (date = new Date()) => {
|
||||
return new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
|
||||
};
|
||||
|
||||
const endOfMonth = (date = new Date()) => {
|
||||
return new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59, 999);
|
||||
};
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const schedules = await Schedule.find().sort({ startDate: 1 });
|
||||
res.json(schedules);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to fetch schedules" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/week", async (req, res) => {
|
||||
try {
|
||||
const start = startOfWeek();
|
||||
const end = endOfWeek();
|
||||
const schedules = await Schedule.find({
|
||||
startDate: { $lte: end },
|
||||
endDate: { $gte: start },
|
||||
}).sort({ startDate: 1 });
|
||||
res.json(schedules);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to fetch weekly schedules" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/month", async (req, res) => {
|
||||
try {
|
||||
const start = startOfMonth();
|
||||
const end = endOfMonth();
|
||||
const schedules = await Schedule.find({
|
||||
startDate: { $lte: end },
|
||||
endDate: { $gte: start },
|
||||
}).sort({ startDate: 1 });
|
||||
res.json(schedules);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Failed to fetch monthly schedules" });
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
try {
|
||||
const schedule = await Schedule.create(req.body);
|
||||
res.status(201).json(schedule);
|
||||
} catch (error) {
|
||||
res.status(400).json({ message: "Failed to create schedule" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
try {
|
||||
const schedule = await Schedule.findById(req.params.id);
|
||||
if (!schedule) {
|
||||
return res.status(404).json({ message: "Schedule not found" });
|
||||
}
|
||||
res.json(schedule);
|
||||
} catch (error) {
|
||||
res.status(400).json({ message: "Failed to fetch schedule" });
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/:id", async (req, res) => {
|
||||
try {
|
||||
const schedule = await Schedule.findByIdAndUpdate(req.params.id, req.body, {
|
||||
new: true,
|
||||
});
|
||||
if (!schedule) {
|
||||
return res.status(404).json({ message: "Schedule not found" });
|
||||
}
|
||||
res.json(schedule);
|
||||
} catch (error) {
|
||||
res.status(400).json({ message: "Failed to update schedule" });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
try {
|
||||
const schedule = await Schedule.findByIdAndDelete(req.params.id);
|
||||
if (!schedule) {
|
||||
return res.status(404).json({ message: "Schedule not found" });
|
||||
}
|
||||
res.json({ ok: true });
|
||||
} catch (error) {
|
||||
res.status(400).json({ message: "Failed to delete schedule" });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user