Files
bini-google-tv/backend/routes/schedules.js
kihong.kim 32e67bfcc2 fix: Asia/Seoul 타임존 지원을 위해 luxon 라이브러리 도입
- backend/routes/todos.js: 오늘의 할일 조회 시 KST 기준으로 날짜 계산
- backend/routes/schedules.js: 주간/월간 일정 조회 시 KST 기준으로 날짜 범위 계산
- backend/routes/bible.js: 오늘의 성경 구절 조회 시 KST 기준으로 날짜 계산
- flutter_app: Mock 데이터 사용 시 타임존 관련 주석 추가
2026-02-01 01:21:14 +09:00

120 lines
3.3 KiB
JavaScript

const express = require("express");
const { DateTime } = require("luxon");
const Schedule = require("../models/Schedule");
const router = express.Router();
// Korea Standard Time zone
const KST_TIMEZONE = "Asia/Seoul";
const startOfWeek = () => {
// Get current time in KST and find the start of the week (Monday)
const nowKst = DateTime.now().setZone(KST_TIMEZONE);
// Luxon uses 1 = Monday, 7 = Sunday
const startOfWeekKst = nowKst.startOf("week"); // Monday 00:00:00.000
return startOfWeekKst.toJSDate();
};
const endOfWeek = () => {
// Get current time in KST and find the end of the week (Sunday)
const nowKst = DateTime.now().setZone(KST_TIMEZONE);
const endOfWeekKst = nowKst.endOf("week"); // Sunday 23:59:59.999
return endOfWeekKst.toJSDate();
};
const startOfMonth = () => {
const nowKst = DateTime.now().setZone(KST_TIMEZONE);
return nowKst.startOf("month").toJSDate();
};
const endOfMonth = () => {
const nowKst = DateTime.now().setZone(KST_TIMEZONE);
return nowKst.endOf("month").toJSDate();
};
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;