更新首页
This commit is contained in:
+13
-8
@@ -8,16 +8,21 @@ router.use(authRequired)
|
||||
// 报警列表: 按时间最新优先, cursor 分页 (cursor = id)
|
||||
router.get('/alerts', async (req, res, next) => {
|
||||
try {
|
||||
const limit = Math.min(Number(req.query.limit) || 10, 50)
|
||||
const cursor = req.query.cursor ? Number(req.query.cursor) : null
|
||||
const rawLimit = Number(req.query.limit)
|
||||
const limit = Number.isFinite(rawLimit) && rawLimit >= 1 ? Math.min(Math.floor(rawLimit), 50) : 10
|
||||
const rawCursor = Number(req.query.cursor)
|
||||
const cursor = Number.isInteger(rawCursor) && rawCursor > 0 ? rawCursor : null
|
||||
const where = cursor ? { id: { lt: cursor } } : {}
|
||||
const items = await prisma.alert.findMany({
|
||||
where,
|
||||
orderBy: { id: 'desc' },
|
||||
take: limit,
|
||||
})
|
||||
const [items, total] = await Promise.all([
|
||||
prisma.alert.findMany({
|
||||
where,
|
||||
orderBy: { id: 'desc' },
|
||||
take: limit,
|
||||
}),
|
||||
prisma.alert.count(),
|
||||
])
|
||||
const nextCursor = items.length === limit ? String(items[items.length - 1].id) : null
|
||||
res.json({ items, nextCursor })
|
||||
res.json({ items, nextCursor, total })
|
||||
} catch (e) { next(e) }
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user