| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
- import { authMiddleware } from '@d8d/auth-module-mt';
- import { AppDataSource, parseWithAwait } from '@d8d/shared-utils';
- import { AuthContext } from '@d8d/shared-types';
- import { OrderMtService } from '../../services';
- import { CreateOrderRequestDto, CreateOrderResponseDto } from '../../schemas/create-order.schema';
- const createOrderRoute = createRoute({
- method: 'post',
- path: '/create-order',
- middleware: [authMiddleware],
- request: {
- body: {
- content: {
- 'application/json': {
- schema: CreateOrderRequestDto
- }
- }
- }
- },
- responses: {
- 201: {
- description: '订单创建成功',
- content: {
- 'application/json': {
- schema: CreateOrderResponseDto
- }
- }
- },
- 400: {
- description: '请求参数错误'
- },
- 500: {
- description: '服务器内部错误'
- }
- }
- })
- const createOrderRoutes = new OpenAPIHono<AuthContext>()
- // 创建订单路由
- .openapi( createOrderRoute, async (c) => {
- const data = c.req.valid('json');
- const user = c.get('user');
- try {
- const orderService = new OrderMtService(AppDataSource);
- const result = await orderService.createOrder(data, user.id, user.tenantId);
- // 验证响应格式
- const validatedResult = await parseWithAwait(CreateOrderResponseDto, result);
- return c.json(validatedResult, 201);
- } catch (error) {
- console.error('创建订单失败:', error);
- return c.json(
- { error: error instanceof Error ? error.message : '创建订单失败' },
- 500
- );
- }
- }
- );
- export default createOrderRoutes;
|