feat: 评论路由
This commit is contained in:
parent
2178247093
commit
2e3517ead5
37
server/routes/comments.ts
Normal file
37
server/routes/comments.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { Hono } from 'hono'
|
||||
import { desc, eq } from 'drizzle-orm'
|
||||
import { db, comments, users } from '../db'
|
||||
import { requireAuth } from '../middleware/auth'
|
||||
|
||||
export const commentsRouter = new Hono()
|
||||
|
||||
commentsRouter.get('/', async (c) => {
|
||||
const rows = db
|
||||
.select({
|
||||
id: comments.id,
|
||||
content: comments.content,
|
||||
createdAt: comments.createdAt,
|
||||
username: users.username,
|
||||
})
|
||||
.from(comments)
|
||||
.innerJoin(users, eq(comments.userId, users.id))
|
||||
.orderBy(desc(comments.createdAt))
|
||||
.limit(100)
|
||||
.all()
|
||||
|
||||
return c.json(rows)
|
||||
})
|
||||
|
||||
commentsRouter.post('/', requireAuth, async (c) => {
|
||||
const userId = c.get('userId')
|
||||
const username = c.get('username')
|
||||
const { content } = await c.req.json<{ content: string }>()
|
||||
|
||||
if (!content?.trim()) return c.json({ error: '评论不能为空' }, 400)
|
||||
if (content.length > 500) return c.json({ error: '评论不超过 500 字' }, 400)
|
||||
|
||||
const id = Math.random().toString(36).slice(2, 11) + Date.now().toString(36)
|
||||
db.insert(comments).values({ id, userId, content: content.trim(), createdAt: Date.now() }).run()
|
||||
|
||||
return c.json({ id, username, content: content.trim(), createdAt: Date.now() })
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user