This commit is contained in:
Li1304553726 2025-02-23 20:20:19 +08:00
parent 2b7fff43e3
commit d87a7a6a98
6 changed files with 112 additions and 7 deletions

View File

@ -0,0 +1,45 @@
import { Controller, Get, Query, Param } from '@nestjs/common';
// 定义商品相关的控制器,路由前缀为 /goods
@Controller('goods')
export class GoodsController {
// 构造函数,在控制器实例化时执行
constructor() {
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'
};
}
// 示例2路径参数
// GET /goods/detail/123
@Get('detail/:id')
getDetail(@Param('id') id: string) {
return {
id: id, // 返回路径参数中的id
detail: `Detail for product ${id}` // 返回包含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
) {
return {
keyword, // 返回搜索关键词
page, // 返回当前页码
limit, // 返回每页数量
results: [] // 返回搜索结果(示例中为空数组)
};
}
}

View File

@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { GoodsService } from './goods.service';
import { GoodsController } from './goods.controller';
@Module({
providers: [GoodsService],
controllers: [GoodsController]
})
export class GoodsModule {}

View File

@ -0,0 +1,6 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class GoodsService {
}

View File

@ -16,6 +16,7 @@ import { RoleMapModule } from '@server/models/rbac/rbac.module';
import { TransformModule } from '@server/models/transform/transform.module';
import { ResourceModule } from '@server/models/resource/resource.module';
import { GoodsModule } from '@server/models/goods/goods.module';
@Module({
imports: [
@ -33,6 +34,7 @@ import { ResourceModule } from '@server/models/resource/resource.module';
VisitModule,
WebSocketModule,
ResourceModule,
GoodsModule
],
controllers: [],
providers: [TrpcService, TrpcRouter, Logger],

View File

@ -2,20 +2,22 @@ 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';
// 主页入口组件,负责渲染首页内容
function HomePage() {
// 使用trpc查询接口获取员工列表取前10条记录
// 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', {
// useEffect(() => {
// console.log(data);
// }, [data]);
});
useEffect(() => {
getData();
},[])
console.log(res);
return (
<div className='p-2 space-y-2'>
<div className='flex space-x-2'>
@ -27,5 +29,6 @@ function HomePage() {
</div>
);
}
}
//export {HomePage}; MainRoute也要+{}
export default HomePage;

View File

@ -329,3 +329,44 @@ model Resource {
@@index([createdAt])
@@map("resource")
}
//商品表
model Goods {
id String @id @default(cuid()) // 商品ID
name String @unique // 商品名称
description String? // 商品描述
price Float @default(0.0)// 商品价格
images String[] @default([]) // 商品图片
tags Tag[] @relation("GoodsTags") // 多对多关系
reviews Review[] // 一对多关系
createdAt DateTime @default(now()) @map("created_at") // 创建时间
updatedAt DateTime @updatedAt @map("updated_at") // 更新时间
deletedAt DateTime? @map("deleted_at") // 删除时间,可为空
@@index([name])
@@map("goods")
}
// 标签表
model Tag {
id String @id @default(cuid())
name String @unique
goods Goods[] @relation("GoodsTags")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
@@map("tag")
}
model Review {
id String @id @default(cuid())
content String
rating Int @default(0)
goodsId String @map("goods_id")
goods Goods @relation(fields: [goodsId], references: [id])
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
@@index([goodsId])
@@index([rating])
@@map("review")
}