Notes/src/etapi/special_notes.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-01-07 19:33:59 +01:00
const specialNotesService = require("../services/special_notes");
const dateNotesService = require("../services/date_notes");
2022-01-10 17:09:20 +01:00
const eu = require("./etapi_utils");
2022-01-07 19:33:59 +01:00
const mappers = require("./mappers");
2022-01-10 17:09:20 +01:00
const getDateInvalidError = date => new eu.EtapiError(400, "DATE_INVALID", `Date "${date}" is not valid.`);
const getMonthInvalidError = month => new eu.EtapiError(400, "MONTH_INVALID", `Month "${month}" is not valid.`);
const getYearInvalidError = year => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`);
2022-01-07 19:33:59 +01:00
function isValidDate(date) {
if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) {
return false;
}
return !!Date.parse(date);
}
function register(router) {
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/inbox/:date', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const {date} = req.params;
if (!isValidDate(date)) {
2023-04-08 19:51:39 +08:00
throw getDateInvalidError(date);
2022-01-07 19:33:59 +01:00
}
const note = specialNotesService.getInboxNote(date);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/days/:date', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const {date} = req.params;
if (!isValidDate(date)) {
2023-04-08 19:51:39 +08:00
throw getDateInvalidError(date);
2022-01-07 19:33:59 +01:00
}
2022-01-10 17:09:20 +01:00
const note = dateNotesService.getDayNote(date);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/weeks/:date', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const {date} = req.params;
if (!isValidDate(date)) {
2023-04-08 19:51:39 +08:00
throw getDateInvalidError(date);
2022-01-07 19:33:59 +01:00
}
const note = dateNotesService.getWeekNote(date);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/months/:month', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const {month} = req.params;
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
2023-04-08 19:51:39 +08:00
throw getMonthInvalidError(month);
2022-01-07 19:33:59 +01:00
}
const note = dateNotesService.getMonthNote(month);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/years/:year', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const {year} = req.params;
if (!/[0-9]{4}/.test(year)) {
2023-04-08 19:51:39 +08:00
throw getYearInvalidError(year);
2022-01-07 19:33:59 +01:00
}
const note = dateNotesService.getYearNote(year);
res.json(mappers.mapNoteToPojo(note));
});
}
module.exports = {
register
};