parent
1df66044dd
commit
a243cd7e6a
@ -0,0 +1,86 @@
|
||||
package com.example.api.controller;
|
||||
|
||||
import com.example.api.annotation.Log;
|
||||
import com.example.api.model.entity.Commodity;
|
||||
import com.example.api.model.enums.BusinessType;
|
||||
import com.example.api.service.CommodityService;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CommodityController 类是商品管理的 RESTful API 控制器。
|
||||
*/
|
||||
@RestController // 表明这是一个 REST 控制器,其方法的返回值会自动作为 HTTP 响应体
|
||||
@RequestMapping("/api/commodity") // 定义类级别的路由:/api/commodity
|
||||
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN','ROLE_COMMODITY','ROLE_SALE')") // 权限注解,只有具有指定角色的用户才能访问
|
||||
public class CommodityController {
|
||||
|
||||
@Resource // 自动注入 CommodityService
|
||||
private CommodityService commodityService;
|
||||
|
||||
/**
|
||||
* 保存商品信息。
|
||||
* @param commodity 商品实体
|
||||
* @return 保存后的商品实体
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.INSERT) // 记录日志,标记为插入操作
|
||||
@PostMapping("") // 定义 POST 请求的路由
|
||||
public Commodity save(@RequestBody Commodity commodity) {
|
||||
return commodityService.save(commodity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品信息。
|
||||
* @param id 商品的 ID
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.DELETE) // 记录日志,标记为删除操作
|
||||
@DeleteMapping("") // 定义 DELETE 请求的路由
|
||||
public void delete(String id) {
|
||||
commodityService.delete(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品信息。
|
||||
* @param commodity 商品实体
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.UPDATE) // 记录日志,标记为更新操作
|
||||
@PutMapping("") // 定义 PUT 请求的路由
|
||||
public void update(@RequestBody Commodity commodity) {
|
||||
commodityService.update(commodity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有商品信息。
|
||||
* @return 商品列表
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.QUERY) // 记录日志,标记为查询操作
|
||||
@GetMapping("") // 定义 GET 请求的路由
|
||||
public List<Commodity> findAll() {
|
||||
return commodityService.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名称模糊查询商品信息。
|
||||
* @param name 名称关键字
|
||||
* @return 匹配的商品列表
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.QUERY) // 记录日志,标记为查询操作
|
||||
@GetMapping("/search/{name}") // 定义 GET 请求的路由,包含路径变量
|
||||
public List<Commodity> findByLikeName(@PathVariable String name) {
|
||||
return commodityService.findAllByLikeName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询商品信息。
|
||||
* @param id 商品的 ID
|
||||
* @return 商品实体
|
||||
*/
|
||||
@Log(module = "商品管理", type = BusinessType.QUERY) // 记录日志,标记为查询操作
|
||||
@GetMapping("/{id}") // 定义 GET 请求的路由,包含路径变量
|
||||
public Commodity findById(@PathVariable String id) {
|
||||
return commodityService.findById(id);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue