From eb514ad4e3b02c4ed2be79ad746162d81db2ea45 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Thu, 3 Apr 2025 22:07:11 +0200 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20improve=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/etapi/special_notes.ts | 52 +++++++++++++++----------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/src/etapi/special_notes.ts b/src/etapi/special_notes.ts index 1af422ed6..df2f75f9a 100644 --- a/src/etapi/special_notes.ts +++ b/src/etapi/special_notes.ts @@ -11,62 +11,50 @@ const getMonthInvalidError = (month: string) => new eu.EtapiError(400, "MONTH_IN const getYearInvalidError = (year: string) => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`); function isValidDate(date: string) { - if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) { - return false; - } - - return !!Date.parse(date); -} - -function isValidWeek(week: string) { - if (!/[0-9]{4}-W[0-9]{2}/.test(week)) { - return false; - } - return true; + return /[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date) && !!Date.parse(date); } function register(router: Router) { - eu.route(router, "get", "/etapi/inbox/:date", (req, res, next) => { + eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => { + const { date } = req.params; + + if (!isValidDate(date)) { + throw getDateInvalidError(date); + } + const note = await specialNotesService.getInboxNote(date); + res.json(mappers.mapNoteToPojo(note)); + }); + + eu.route(router, "get", "/etapi/calendar/days/:date", async (req, res, next) => { const { date } = req.params; if (!isValidDate(date)) { throw getDateInvalidError(date); } - const note = specialNotesService.getInboxNote(date); + const note = await dateNotesService.getDayNote(date); res.json(mappers.mapNoteToPojo(note)); }); - eu.route(router, "get", "/etapi/calendar/days/:date", (req, res, next) => { + eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (req, res, next) => { const { date } = req.params; if (!isValidDate(date)) { throw getDateInvalidError(date); } - const note = dateNotesService.getDayNote(date); + const note = await dateNotesService.getWeekFirstDayNote(date); res.json(mappers.mapNoteToPojo(note)); }); - eu.route(router, "get", "/etapi/calendar/week-first-day/:date", (req, res, next) => { - const { date } = req.params; - - if (!isValidDate(date)) { - throw getDateInvalidError(date); - } - - const note = dateNotesService.getWeekFirstDayNote(date); - res.json(mappers.mapNoteToPojo(note)); - }); - - eu.route(router, "get", "/etapi/calendar/weeks/:week", (req, res, next) => { + eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => { const { week } = req.params; - if (!isValidWeek(week)) { + if (!/[0-9]{4}-W[0-9]{2}/.test(week)) { throw getWeekInvalidError(week); } - const note = dateNotesService.getWeekNote(week); + const note = await dateNotesService.getWeekNote(week); if (!note) { throw getWeekNotFoundError(week); @@ -75,14 +63,14 @@ function register(router: Router) { res.json(mappers.mapNoteToPojo(note)); }); - eu.route(router, "get", "/etapi/calendar/months/:month", (req, res, next) => { + eu.route(router, "get", "/etapi/calendar/months/:month", async (req, res, next) => { const { month } = req.params; if (!/[0-9]{4}-[0-9]{2}/.test(month)) { throw getMonthInvalidError(month); } - const note = dateNotesService.getMonthNote(month); + const note = await dateNotesService.getMonthNote(month); res.json(mappers.mapNoteToPojo(note)); });