|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.bcn.login_mybatis_demo.controller;
|
|
|
+
|
|
|
+import com.bcn.login_mybatis_demo.pojo.Goods;
|
|
|
+import com.bcn.login_mybatis_demo.service.GoodsService;
|
|
|
+import com.bcn.login_mybatis_demo.util.Result;
|
|
|
+import com.bcn.login_mybatis_demo.util.ResultCode;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/goods")
|
|
|
+public class GoodsController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GoodsService goodsService;
|
|
|
+ //获取全部商品
|
|
|
+ @GetMapping("/getAllGoods")
|
|
|
+ public Result getAllGoods() {
|
|
|
+ List<Goods> goodsList = goodsService.getAllGoods();
|
|
|
+ System.out.println("goodsList:"+goodsList);
|
|
|
+ if (goodsList != null && !goodsList.isEmpty()) {
|
|
|
+ return new Result(200, "查询成功", goodsList); // 成功返回
|
|
|
+ } else {
|
|
|
+ return new Result(404, "未找到商品", null); // 未找到商品时返回
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //获取单个商品
|
|
|
+ @RequestMapping("/{id}")
|
|
|
+ public Result getGoodsById(@PathVariable int id) {
|
|
|
+ Goods goods = goodsService.getGoodsById(id);
|
|
|
+ if (goods != null) {
|
|
|
+ return new Result(200,"获取成功",goods);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return new Result(404,"未找到商品",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加新商品
|
|
|
+ @PostMapping("/add")
|
|
|
+ public Result addGoods(@RequestBody Goods goods) {
|
|
|
+ if (goods.getGoodsname().isEmpty()||goods.getId()==null||goods.getPrice()==null||
|
|
|
+ goods.getInventory()==null) {
|
|
|
+ Result<Goods> result = new Result<>(404, "商品添加失败", goods);
|
|
|
+ System.out.println(result); // 打印成功的 Result 对象
|
|
|
+ return result;
|
|
|
+ }else {
|
|
|
+ goodsService.addGoods(goods);
|
|
|
+ Result<Goods> result = new Result<>(200, "商品添加成功", goods);
|
|
|
+ System.out.println(result); // 打印成功的 Result 对象
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //更新商品
|
|
|
+ @PutMapping("/update/{id}")
|
|
|
+ public Result updateGoods(@RequestBody Goods goods) {
|
|
|
+ if (goods.getGoodsname().isEmpty()||goods.getId()==null||goods.getPrice()==null||goods.getInventory()==null) {
|
|
|
+ Result<Goods> result = new Result<>(404, "商品更新失败", goods);
|
|
|
+ System.out.println(result); // 打印成功的 Result 对象
|
|
|
+ return result;
|
|
|
+ }else {
|
|
|
+ goodsService.updateGoods(goods);
|
|
|
+ Result<Goods> result = new Result<>(200, "商品更新成功", goods);
|
|
|
+ System.out.println(result); // 打印成功的 Result 对象
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除商品
|
|
|
+ @DeleteMapping("/delete/{id}")
|
|
|
+ public String deleteGoods(@PathVariable int id) {
|
|
|
+
|
|
|
+ goodsService.deleteGoods(id);
|
|
|
+
|
|
|
+
|
|
|
+ return "调用了删除";
|
|
|
+ }
|
|
|
+ //查询商品--精准查询
|
|
|
+ //@GetMapping("/searchbyname")
|
|
|
+ //public List<Goods> searchGoods(@RequestParam String name) {
|
|
|
+ // return goodsService.findGoodsByName(name);
|
|
|
+ //}
|
|
|
+
|
|
|
+ //查询商品--模糊查询
|
|
|
+ @GetMapping("/search")
|
|
|
+ public Result<List<Goods>> searchGoods(@RequestParam String keyword) {
|
|
|
+ List<Goods> goodsList = goodsService.searchGoodsByKeyword(keyword);
|
|
|
+ return new Result<>(200,"查询成功",goodsList);
|
|
|
+ }
|
|
|
+}
|