optimize: api resp size.
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
This commit is contained in:
@@ -5,31 +5,58 @@ const router = express.Router();
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
const userId = req.user.userId;
|
||||
const { year, month } = req.query;
|
||||
|
||||
db.all(
|
||||
'SELECT date, status, updated_at FROM calendar_marks WHERE user_id = ?',
|
||||
[userId],
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ success: false, error: '获取数据失败' });
|
||||
}
|
||||
// Build query based on whether year and month are provided
|
||||
let query = 'SELECT date, status, updated_at FROM calendar_marks WHERE user_id = ?';
|
||||
const params = [userId];
|
||||
|
||||
const markedDates = {};
|
||||
rows.forEach((row) => {
|
||||
markedDates[row.date] = {
|
||||
status: row.status,
|
||||
updatedAt: row.updated_at
|
||||
};
|
||||
});
|
||||
// If year and month are provided, filter by that month plus adjacent months
|
||||
if (year && month) {
|
||||
const yearNum = parseInt(year, 10);
|
||||
const monthNum = parseInt(month, 10);
|
||||
|
||||
res.json({ success: true, markedDates });
|
||||
// Validate year and month
|
||||
if (isNaN(yearNum) || isNaN(monthNum) || monthNum < 1 || monthNum > 12) {
|
||||
return res.status(400).json({ success: false, error: '无效的年份或月份' });
|
||||
}
|
||||
);
|
||||
|
||||
// Calculate the date range: previous month to next month
|
||||
// This covers all dates that might be visible in the calendar view
|
||||
const prevMonth = monthNum === 1 ? 12 : monthNum - 1;
|
||||
const prevYear = monthNum === 1 ? yearNum - 1 : yearNum;
|
||||
const nextMonth = monthNum === 12 ? 1 : monthNum + 1;
|
||||
const nextYear = monthNum === 12 ? yearNum + 1 : yearNum;
|
||||
|
||||
const startDate = `${prevYear}-${String(prevMonth).padStart(2, '0')}-01`;
|
||||
// Get the last day of next month
|
||||
const lastDayOfNextMonth = new Date(nextYear, nextMonth, 0).getDate();
|
||||
const endDate = `${nextYear}-${String(nextMonth).padStart(2, '0')}-${String(lastDayOfNextMonth).padStart(2, '0')}`;
|
||||
|
||||
query += ' AND date >= ? AND date <= ?';
|
||||
params.push(startDate, endDate);
|
||||
}
|
||||
|
||||
db.all(query, params, (err, rows) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ success: false, error: '获取数据失败' });
|
||||
}
|
||||
|
||||
const markedDates = {};
|
||||
rows.forEach((row) => {
|
||||
markedDates[row.date] = {
|
||||
status: row.status,
|
||||
updatedAt: row.updated_at
|
||||
};
|
||||
});
|
||||
|
||||
res.json({ success: true, markedDates });
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
const userId = req.user.userId;
|
||||
const { markedDates } = req.body || {};
|
||||
const { markedDates, deletedDates } = req.body || {};
|
||||
|
||||
if (!markedDates || typeof markedDates !== 'object') {
|
||||
return res.status(400).json({ success: false, error: '无效的数据格式' });
|
||||
@@ -40,75 +67,67 @@ router.post('/', (req, res) => {
|
||||
return new Date().toISOString();
|
||||
};
|
||||
|
||||
const dbOperations = Object.entries(markedDates).map(([date, status]) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// First check if this date exists and if status has changed
|
||||
db.get(
|
||||
'SELECT status FROM calendar_marks WHERE user_id = ? AND date = ?',
|
||||
[userId, date],
|
||||
(err, row) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
// Only update timestamp if status changed or it's a new entry
|
||||
const statusChanged = !row || row.status !== status;
|
||||
|
||||
if (statusChanged) {
|
||||
// Status changed - update with new timestamp
|
||||
const currentTimestamp = getLocalTimestamp();
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
updated_at = excluded.updated_at`,
|
||||
[userId, date, status, currentTimestamp],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Status unchanged - keep existing timestamp
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, (SELECT updated_at FROM calendar_marks WHERE user_id = ? AND date = ?))
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status`,
|
||||
[userId, date, status, userId, date],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
const dbOperations = [];
|
||||
|
||||
const dates = Object.keys(markedDates);
|
||||
if (dates.length > 0) {
|
||||
const placeholders = dates.map(() => '?').join(',');
|
||||
// Update or insert marked dates
|
||||
Object.entries(markedDates).forEach(([date, status]) => {
|
||||
dbOperations.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.run(
|
||||
`DELETE FROM calendar_marks WHERE user_id = ? AND date NOT IN (${placeholders})`,
|
||||
[userId, ...dates],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
// First check if this date exists and if status has changed
|
||||
db.get(
|
||||
'SELECT status FROM calendar_marks WHERE user_id = ? AND date = ?',
|
||||
[userId, date],
|
||||
(err, row) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
// Only update timestamp if status changed or it's a new entry
|
||||
const statusChanged = !row || row.status !== status;
|
||||
|
||||
if (statusChanged) {
|
||||
// Status changed - update with new timestamp
|
||||
const currentTimestamp = getLocalTimestamp();
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
updated_at = excluded.updated_at`,
|
||||
[userId, date, status, currentTimestamp],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Status unchanged - keep existing timestamp
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, (SELECT updated_at FROM calendar_marks WHERE user_id = ? AND date = ?))
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status`,
|
||||
[userId, date, status, userId, date],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
});
|
||||
|
||||
// Delete explicitly removed dates
|
||||
if (deletedDates && Array.isArray(deletedDates) && deletedDates.length > 0) {
|
||||
const placeholders = deletedDates.map(() => '?').join(',');
|
||||
dbOperations.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.run(
|
||||
'DELETE FROM calendar_marks WHERE user_id = ?',
|
||||
[userId],
|
||||
`DELETE FROM calendar_marks WHERE user_id = ? AND date IN (${placeholders})`,
|
||||
[userId, ...deletedDates],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
|
||||
Reference in New Issue
Block a user