一、商品信息CRUD 接口

image.png
image.png
image.png

  1. package com.yhh.goods.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.yhh.common.base.ret.ApiResult;
  6. import com.yhh.common.entity.goods.dto.ProductDto;
  7. import com.yhh.common.entity.goods.model.Product;
  8. import com.yhh.common.utils.FieldsFilter;
  9. import com.yhh.goods.service.IProductService;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.validation.annotation.Validated;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.stereotype.Controller;
  17. import java.util.Arrays;
  18. import java.util.Date;
  19. import java.util.List;
  20. /**
  21. * <p>
  22. * 商品信息服务API
  23. * </p>
  24. *
  25. * @author yhh
  26. * @since 2021-04-21
  27. */
  28. @RestController
  29. @RequestMapping("goods/product/api/v1")
  30. @Api(tags = "商品信息服务API")
  31. @Slf4j
  32. public class ProductController {
  33. @Autowired
  34. private IProductService productService;
  35. @ApiOperation(value = "查询商品信息")
  36. @GetMapping("getProducts")
  37. public ApiResult getProducts(ProductDto dto){
  38. if(dto == null)
  39. return ApiResult.error("没有查询到您需要的信息");
  40. if(dto.getPageNum() == null || dto.getPageSize() == null){
  41. return ApiResult.error("请传入分页参数");
  42. }
  43. IPage page = new Page(dto.getPageNum(), dto.getPageSize());
  44. QueryWrapper wrapper = new QueryWrapper();
  45. if(dto.getName() != null){
  46. wrapper.like("name",dto.getName());
  47. }
  48. if(dto.getMinPrice() !=null){
  49. wrapper.ge("price",dto.getMinPrice());
  50. }
  51. if(dto.getMaxPrice() !=null){
  52. wrapper.le("price",dto.getMaxPrice());
  53. }
  54. wrapper.eq("status",dto.getStatus());
  55. page = productService.page(page,wrapper);
  56. List records = page.getRecords();
  57. return ApiResult.success().put("products",page.setRecords(FieldsFilter.filter(records,
  58. Arrays.asList("id","name","price","mainImg"))));
  59. }
  60. @ApiOperation(value = "根据ID查询商品信息")
  61. @GetMapping("getProductById")
  62. public ApiResult getProductById(Integer id){
  63. return ApiResult.success().put("product",productService.getById(id));
  64. }
  65. @ApiOperation(value = "添加商品信息")
  66. @PostMapping("addProduct")
  67. public ApiResult addProduct(@Validated({Product.Save.class}) @RequestBody Product product){
  68. product.setCreateTime(new Date());
  69. product.setUpdateTime(new Date());
  70. product.setStatus(1);
  71. if(productService.save(product)){
  72. return ApiResult.success();
  73. }
  74. return ApiResult.error("添加商品信息失败");
  75. }
  76. @ApiOperation(value = "修改商品信息")
  77. @PostMapping("updateProduct")
  78. public ApiResult updateProduct(@Validated({Product.Update.class}) @RequestBody Product product){
  79. product.setUpdateTime(new Date());
  80. if(productService.updateById(product)){
  81. return ApiResult.success();
  82. }
  83. return ApiResult.error("添加商品信息失败");
  84. }
  85. @ApiOperation(value = "删除商品信息")
  86. @PostMapping("delProduct")
  87. public ApiResult delProduct(Integer id){
  88. if(productService.removeById(id)){
  89. return ApiResult.success();
  90. }
  91. return ApiResult.error("删除商品信息失败");
  92. }
  93. }

二、添加网关

image.png