Skip to content

Commit 7878d74

Browse files
committed
feat: add pagination, month filtering, and single record lookup to words count list API.
1 parent 6952554 commit 7878d74

File tree

1 file changed

+47
-2
lines changed
  • apps/congrong-private-api/server/api/words-count

1 file changed

+47
-2
lines changed

apps/congrong-private-api/server/api/words-count/list.get.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
1+
import { and, count, desc, eq, like } from 'drizzle-orm'
12
import { wordsCountTable } from '~~/db/schema'
23

34
/**
4-
* 查询所有字数统计记录
5+
* 查询字数统计记录
6+
* 支持分页、按月查询、ID查询
57
* GET /api/words-count/list
8+
* Query: id, month, page, limit
69
*/
710
export default defineEventHandler(async (event) => {
11+
const query = getQuery(event)
12+
const { id, month, page = 1, limit = 20 } = query
13+
814
try {
15+
// ID 查询单条数据
16+
if (id) {
17+
const result = await event.context.db
18+
.select()
19+
.from(wordsCountTable)
20+
.where(eq(wordsCountTable.id, Number(id)))
21+
.get()
22+
return createSuccessResponse(result, '获取详情成功')
23+
}
24+
25+
const pageNum = Number(page)
26+
const pageSize = Number(limit)
27+
const conditions = []
28+
29+
if (month) {
30+
conditions.push(like(wordsCountTable.createTime, `${month}%`))
31+
}
32+
33+
const whereClause = conditions.length > 0 ? and(...conditions) : undefined
34+
35+
// 查询总数
36+
const totalRes = await event.context.db
37+
.select({ count: count() })
38+
.from(wordsCountTable)
39+
.where(whereClause)
40+
.get()
41+
const total = totalRes?.count || 0
42+
43+
// 分页查询
944
const list = await event.context.db
1045
.select()
1146
.from(wordsCountTable)
12-
return createSuccessResponse(list, '字数统计记录获取成功')
47+
.where(whereClause)
48+
.orderBy(desc(wordsCountTable.createTime))
49+
.limit(pageSize)
50+
.offset((pageNum - 1) * pageSize)
51+
52+
return createSuccessResponse({
53+
list,
54+
total,
55+
page: pageNum,
56+
pageSize,
57+
}, '字数统计记录获取成功')
1358
}
1459
catch (error) {
1560
return createErrorResponse(error instanceof Error ? error.message : '获取字数统计记录失败', 500)

0 commit comments

Comments
 (0)