2.23
This commit is contained in:
commit
ec1e352102
|
@ -4,19 +4,27 @@ import { TrpcRouter } from './trpc/trpc.router';
|
|||
import { WebSocketService } from './socket/websocket.service';
|
||||
|
||||
async function bootstrap() {
|
||||
// 创建NestJS应用实例
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
// 启用 CORS 并允许所有来源
|
||||
app.enableCors({
|
||||
origin: '*',
|
||||
});
|
||||
|
||||
// 获取WebSocket服务实例并初始化
|
||||
const wsService = app.get(WebSocketService);
|
||||
await wsService.initialize(app.getHttpServer());
|
||||
|
||||
// 获取tRPC路由实例并应用中间件
|
||||
const trpc = app.get(TrpcRouter);
|
||||
trpc.applyMiddleware(app);
|
||||
|
||||
// 设置服务器端口,优先使用环境变量中的值,默认3000
|
||||
const port = process.env.SERVER_PORT || 3000;
|
||||
|
||||
// 启动应用,监听指定端口
|
||||
await app.listen(port);
|
||||
}
|
||||
// 启动应用
|
||||
bootstrap();
|
||||
|
|
|
@ -1,45 +1,41 @@
|
|||
import { Controller, Get, Query, Param } from '@nestjs/common';
|
||||
|
||||
// 定义商品相关的控制器,路由前缀为 /goods
|
||||
@Controller('goods')
|
||||
export class GoodsController {
|
||||
// 构造函数,在控制器实例化时执行
|
||||
constructor() {
|
||||
console.log('goods controller') // 打印日志,用于调试
|
||||
console.log('goods controller')
|
||||
}
|
||||
|
||||
// 示例1:基本查询参数
|
||||
// GET /goods/hello?name=xxx
|
||||
@Get('hello')
|
||||
getHello(@Query('name') name?: string) {
|
||||
return {
|
||||
message: 'Hello World!', // 固定返回消息
|
||||
name: name || 'Guest' // 返回传入的name参数,如果未传入则返回'Guest'
|
||||
message: 'Hello World!',
|
||||
name: name || 'Guest'
|
||||
};
|
||||
}
|
||||
|
||||
// 示例2:路径参数
|
||||
// GET /goods/detail/123
|
||||
@Get('detail/:id')
|
||||
getDetail(@Param('id') id: string) {
|
||||
return {
|
||||
id: id, // 返回路径参数中的id
|
||||
detail: `Detail for product ${id}` // 返回包含id的详细信息
|
||||
id: id,
|
||||
detail: `Detail for product ${id}`
|
||||
};
|
||||
}
|
||||
|
||||
// 示例3:多个查询参数
|
||||
// GET /goods/search?keyword=xxx&page=2&limit=20
|
||||
@Get('search')
|
||||
searchProducts(
|
||||
@Query('keyword') keyword: string, // 搜索关键词
|
||||
@Query('page') page: number = 1, // 页码,默认值为1
|
||||
@Query('limit') limit: number = 10 // 每页数量,默认值为10
|
||||
@Query('keyword') keyword: string,
|
||||
@Query('page') page: number = 1,
|
||||
@Query('limit') limit: number = 10
|
||||
) {
|
||||
return {
|
||||
keyword, // 返回搜索关键词
|
||||
page, // 返回当前页码
|
||||
limit, // 返回每页数量
|
||||
results: [] // 返回搜索结果(示例中为空数组)
|
||||
keyword,
|
||||
page,
|
||||
limit,
|
||||
results: []
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { GoodsService } from './goods.service';
|
||||
import { GoodsController } from './goods.controller';
|
||||
|
||||
@Module({
|
||||
providers: [GoodsService],
|
||||
controllers: [GoodsController]
|
||||
providers: [GoodsService],
|
||||
controllers: [GoodsController],
|
||||
})
|
||||
export class GoodsModule {}
|
||||
export class GoodsModule {}
|
||||
|
|
|
@ -3,4 +3,4 @@ import { Injectable } from '@nestjs/common';
|
|||
@Injectable()
|
||||
export class GoodsService {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,4 +39,4 @@ import { GoodsModule } from '@server/models/goods/goods.module';
|
|||
controllers: [],
|
||||
providers: [TrpcService, TrpcRouter, Logger],
|
||||
})
|
||||
export class TrpcModule {}
|
||||
export class TrpcModule { }
|
||||
|
|
|
@ -1,34 +1,45 @@
|
|||
import { api } from '@nice/client';
|
||||
import { useState, useEffect, useMemo } from 'react';
|
||||
import { Button } from 'antd';
|
||||
import People from '../component/People';
|
||||
import { apiClient } from '@web/src/utils';
|
||||
// 主页入口组件,负责渲染首页内容
|
||||
import { api } from "@nice/client"
|
||||
import { apiClient } from "@web/src/utils"
|
||||
import { Button, Tag } from "antd"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
|
||||
function HomePage() {
|
||||
|
||||
const [counter, setCounter] = useState<number>(0);
|
||||
const { data } = api.staff.findMany.useQuery({
|
||||
take: 10
|
||||
})
|
||||
const [counter, setCounter] = useState<number>(0)
|
||||
const counterText = useMemo(() => {
|
||||
return `当前值:${counter}`
|
||||
}, [counter]);
|
||||
const getData = async () => {
|
||||
const res = await apiClient.get('/goods/hello', {
|
||||
return `当前计数为:${counter}`
|
||||
}, [counter])
|
||||
|
||||
});
|
||||
const getData = async () => {
|
||||
const res = await apiClient.get("/goods/hello")
|
||||
console.log(res)
|
||||
}
|
||||
useEffect(() => {
|
||||
getData();
|
||||
},[])
|
||||
console.log(res);
|
||||
return (
|
||||
<div className='p-2 space-y-2'>
|
||||
<div className='flex space-x-2'>
|
||||
<Button type="primary" onClick={() => { setCounter(counter + 1) }}>+1</Button>
|
||||
<div className='flex items-center'>{counterText}</div>
|
||||
<Button danger onClick={() => { setCounter(counter - 1) }}>-1</Button>
|
||||
</div>
|
||||
<People></People>
|
||||
getData()
|
||||
}, [])
|
||||
return <div className="p-2 space-y-2">
|
||||
<Tag>{counterText}</Tag>
|
||||
<div className="space-x-2" >
|
||||
<Button type="primary" onClick={() => {
|
||||
setCounter(counter + 1)
|
||||
}}>加1</Button>
|
||||
<Button danger
|
||||
onClick={() => {
|
||||
setCounter(counter - 1)
|
||||
}}
|
||||
>减1</Button>
|
||||
</div>
|
||||
);
|
||||
|
||||
{
|
||||
data?.map(i => {
|
||||
return <div className="p-2 rounded border shadow">
|
||||
<Tag>{i.username}</Tag>
|
||||
</div>
|
||||
})
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
//export {HomePage}; MainRoute也要+{}
|
||||
export default HomePage;
|
||||
// export { HomePage }
|
||||
export default HomePage
|
||||
|
|
|
@ -330,12 +330,14 @@ model Resource {
|
|||
@@map("resource")
|
||||
}
|
||||
|
||||
|
||||
|
||||
//商品表
|
||||
model Goods {
|
||||
id String @id @default(cuid()) // 商品ID
|
||||
name String @unique // 商品名称
|
||||
description String? // 商品描述
|
||||
price Float @default(0.0)// 商品价格
|
||||
price Float @default(0.0) // 商品价格
|
||||
images String[] @default([]) // 商品图片
|
||||
tags Tag[] @relation("GoodsTags") // 多对多关系
|
||||
reviews Review[] // 一对多关系
|
||||
|
|
Loading…
Reference in New Issue