11package com .drawbluecup .web .controller ;
22
3+ import com .drawbluecup .dto .product .ProductAddDTO ;
4+ import com .drawbluecup .dto .product .ProductRespDTO ;
5+ import com .drawbluecup .dto .product .ProductUpdateDTO ;
36import com .drawbluecup .entity .Product ;
47import com .drawbluecup .result .Result ;
58import com .drawbluecup .service .ProductService ;
811import org .springframework .beans .factory .annotation .Autowired ;
912import org .springframework .web .bind .annotation .*;
1013
14+ import java .util .ArrayList ;
1115import java .util .List ;
1216
1317
2125//http://localhost:8080
2226
2327@ Tag (name = "产品管理" , description = "产品增删改查接口" )
24-
28+ //Swagger3.x 注解:生成 API 文档时,@Tag是接口分组名,@Operation是单个接口的描述
2529public class ProductController {
2630
2731
@@ -38,8 +42,23 @@ public class ProductController {
3842 */
3943 @ GetMapping ("/findAll" )
4044 @ Operation (summary = "查询所有商品" )
41- public Result <List <Product >> findAll (){
42- return Result .success (200 ,"查询成功" , productService .findAll ());
45+
46+ public Result <List <ProductRespDTO >> findAll (){//实体转DTO
47+
48+ // 1. 调用Service获取所有Product实体类
49+ List <Product > productList = productService .findAll ();
50+
51+ // 2. 遍历转换:每个Product → ProductRespDTO
52+ List <ProductRespDTO > respDTOList = new ArrayList <>();
53+ for (Product product : productList ) {
54+ ProductRespDTO respDTO = new ProductRespDTO ();
55+ respDTO .setId (product .getId ());
56+ respDTO .setName (product .getName ());
57+ respDTOList .add (respDTO );
58+ }
59+
60+ // 3. 返回DTO列表
61+ return Result .success (200 ,"查询成功" , respDTOList );
4362 }
4463
4564 /*
@@ -50,8 +69,19 @@ public Result<List<Product>> findAll(){
5069 */
5170 @ GetMapping ("/findById/{id}" )
5271 @ Operation (summary = "根据id查询商品" , description = "根据商品ID查询商品信息" )
53- public Result <Product > findById (@ PathVariable Integer id ){
54- return Result .success (200 ,"查询成功" , productService .findById (id ));
72+
73+ public Result <ProductRespDTO > findById (@ PathVariable Integer id ){
74+
75+ // 1. 调用Service获取实体类(还是原来的逻辑,Service返回Product)
76+ Product product = productService .findById (id );
77+
78+ // 2. 实体类转DTO(只赋值前端需要的id和name)
79+ ProductRespDTO respDTO = new ProductRespDTO ();
80+ respDTO .setId (product .getId ());
81+ respDTO .setName (product .getName ());
82+
83+ // 3. 返回DTO给前端(前端只看到id+name,看不到其他字段)
84+ return Result .success (200 ,"查询成功" ,respDTO );
5585 }
5686
5787 /*
@@ -93,6 +123,8 @@ public Result<Void> deleteById(@PathVariable Integer id){
93123 return Result .success (200 ,"删除成功" ,null );
94124 }
95125
126+
127+
96128 /*
97129 * 2.1添加商品
98130 * 接口路径:/api/product/add
@@ -101,20 +133,35 @@ public Result<Void> deleteById(@PathVariable Integer id){
101133 */
102134 @ PostMapping ("/add" )
103135 @ Operation (summary = "新增商品" , description = "不需要传输自增id" )
104- public Result <Void > add (@ RequestBody Product product ){
136+ public Result <Void > add (@ RequestBody ProductAddDTO addDTO ){ // 接收DTO,不再接收Product
137+
138+ // 关键:DTO转实体类(只赋值name字段)
139+ Product product = new Product ();
140+ product .setName (addDTO .getName ());// 手动赋值(你的场景字段少,不用BeanUtils)
141+
142+ //给服务层实体类
105143 productService .add (product );
106144 return Result .success (201 ,"添加成功" ,null );
107145 }
108146
147+
148+
109149 /*
110150 * 3.1修改商品
111151 * 接口路径:/api/product/update
112152 * 请求方式:Put
113153 * 参数对象(其中包含id和其他字段,id用来定位,其他是修改),无返回
114154 */
115155 @ PutMapping ("/update" )
116- @ Operation (summary = "基于id查询修改单个商品" )
117- public Result <Void > update (@ RequestBody Product product ){
156+ @ Operation (summary = "基于id查询修改单个商品" )//因为要用到id查询,增加DTO不适用了(本来也不应该混用😒)
157+
158+ public Result <Void > update (@ RequestBody ProductUpdateDTO updateDTO ){
159+
160+ //将前端DTO转换为实体类
161+ Product product = new Product ();
162+ product .setId (updateDTO .getId ());//一一赋值
163+ product .setName (updateDTO .getName ());
164+
118165 productService .update (product );
119166 return Result .success (200 ,"修改成功" ,null );
120167 }
0 commit comments