- 添加 ESLint 和 Prettier 配置以统一代码风格 - 配置项目级 TypeScript 设置 - 更新前后端依赖版本 - 修复代码格式问题(引号、分号、尾随逗号等) - 优化文件结构和导入路径
43 lines
891 B
TypeScript
43 lines
891 B
TypeScript
import { Elysia, t } from 'elysia'
|
|
import { prisma } from './prisma'
|
|
|
|
export const posts = new Elysia({ prefix: '/posts' })
|
|
.get('/', async () => {
|
|
return await prisma.post.findMany({
|
|
include: { user: true },
|
|
orderBy: { createdAt: 'desc' },
|
|
})
|
|
})
|
|
.get('/:id', async ({ params: { id } }) => {
|
|
return await prisma.post.findUnique({
|
|
where: { id },
|
|
include: {
|
|
user: true,
|
|
comments: {
|
|
include: {
|
|
user: true,
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
})
|
|
.post(
|
|
'/',
|
|
async ({ body }) => {
|
|
return await prisma.post.create({
|
|
data: body,
|
|
})
|
|
},
|
|
{
|
|
body: t.Object({
|
|
title: t.String(),
|
|
content: t.String(),
|
|
userId: t.String(),
|
|
published: t.Optional(t.Boolean()),
|
|
}),
|
|
},
|
|
)
|