diff --git a/xdclass-product-service/src/main/java/net/jieyuu/config/InterceptorConfig.java b/xdclass-product-service/src/main/java/net/jieyuu/config/InterceptorConfig.java new file mode 100644 index 0000000..0e5ffbe --- /dev/null +++ b/xdclass-product-service/src/main/java/net/jieyuu/config/InterceptorConfig.java @@ -0,0 +1,25 @@ +package net.jieyuu.config; + + +import lombok.extern.slf4j.Slf4j; +import net.jieyuu.interceptor.LoginInterceptor; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@Slf4j +public class InterceptorConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry + .addInterceptor(new LoginInterceptor()) + //拦截的路径 + .addPathPatterns("") + //放行的路径 + .excludePathPatterns("", ""); + + WebMvcConfigurer.super.addInterceptors(registry); + } +} diff --git a/xdclass-product-service/src/main/java/net/jieyuu/controller/ProductController.java b/xdclass-product-service/src/main/java/net/jieyuu/controller/ProductController.java index 7e8a342..d3d5253 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/controller/ProductController.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/controller/ProductController.java @@ -1,21 +1,50 @@ package net.jieyuu.controller; -import org.springframework.web.bind.annotation.RequestMapping; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import net.jieyuu.service.ProductService; +import net.jieyuu.utils.JsonData; +import net.jieyuu.vo.ProductVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; -import org.springframework.web.bind.annotation.RestController; +import java.util.Map; /** *

- * 前端控制器 + * 前端控制器 *

* * @author jieyuu * @since 2024-08-07 */ +@Api("商品模块") @RestController -@RequestMapping("/productDO") +@RequestMapping("/api/product/v1") public class ProductController { + @Autowired + private ProductService productService; + + + @ApiOperation(value = "分页查询商品列表") + @GetMapping("page") + public JsonData page(@ApiParam("当前页") + @RequestParam(value = "page", defaultValue = "1") int page, + @ApiParam("显示多少条") + @RequestParam(value = "size", defaultValue = "20") int size) { + Map pageResult = productService.page(page, size); + return JsonData.buildSuccess(pageResult); + } + + @ApiOperation("") + @GetMapping("detail/{product_id}") + public JsonData detail(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") long productId) { + ProductVO productVO = productService.findDetailById(productId); + + return JsonData.buildSuccess(productVO); + } } diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/BannerService.java b/xdclass-product-service/src/main/java/net/jieyuu/service/BannerService.java index 9f67b3b..f490141 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/BannerService.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/BannerService.java @@ -1,7 +1,6 @@ package net.jieyuu.service; -import net.jieyuu.model.BannerDO; -import com.baomidou.mybatisplus.extension.service.IService; + import net.jieyuu.vo.BannerVO; import java.util.List; diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/ProductService.java b/xdclass-product-service/src/main/java/net/jieyuu/service/ProductService.java index 5719ad5..69b63ba 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/ProductService.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/ProductService.java @@ -2,6 +2,9 @@ package net.jieyuu.service; import net.jieyuu.model.ProductDO; import com.baomidou.mybatisplus.extension.service.IService; +import net.jieyuu.vo.ProductVO; + +import java.util.Map; /** *

@@ -11,6 +14,20 @@ import com.baomidou.mybatisplus.extension.service.IService; * @author jieyuu * @since 2024-08-07 */ -public interface ProductService extends IService { +public interface ProductService { + /** + * 分页查询商品列表 + * @param page + * @param size + * @return + */ + Map page(int page,int size); + + /** + * 根据id查找商品详情 + * @param productId + * @return + */ + ProductVO findDetailById(long productId); } diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/BannerServiceImpl.java b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/BannerServiceImpl.java index 89f0408..990d322 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/BannerServiceImpl.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/BannerServiceImpl.java @@ -1,10 +1,10 @@ package net.jieyuu.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import lombok.extern.slf4j.Slf4j; import net.jieyuu.model.BannerDO; import net.jieyuu.mapper.BannerMapper; import net.jieyuu.service.BannerService; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.jieyuu.vo.BannerVO; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -22,6 +22,7 @@ import java.util.stream.Collectors; * @since 2024-08-07 */ @Service +@Slf4j public class BannerServiceImpl implements BannerService { @Autowired diff --git a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/ProductServiceImpl.java b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/ProductServiceImpl.java index ba8a14b..9458d45 100644 --- a/xdclass-product-service/src/main/java/net/jieyuu/service/impl/ProductServiceImpl.java +++ b/xdclass-product-service/src/main/java/net/jieyuu/service/impl/ProductServiceImpl.java @@ -1,20 +1,62 @@ package net.jieyuu.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import lombok.extern.slf4j.Slf4j; import net.jieyuu.model.ProductDO; import net.jieyuu.mapper.ProductMapper; import net.jieyuu.service.ProductService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import net.jieyuu.vo.ProductVO; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + /** *

- * 服务实现类 + * 服务实现类 *

* * @author jieyuu * @since 2024-08-07 */ @Service +@Slf4j public class ProductServiceImpl extends ServiceImpl implements ProductService { + @Autowired + private ProductMapper productMapper; + + @Override + public Map page(int page, int size) { + Page pageInfo = new Page<>(page, size); + IPage productDOIPage = productMapper.selectPage(pageInfo, null); + + Map pageMap = new HashMap<>(3); + + pageMap.put("total_record", productDOIPage.getTotal()); + pageMap.put("total_page", productDOIPage.getPages()); + pageMap.put("current_data", productDOIPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList())); + + return pageMap; + } + + @Override + public ProductVO findDetailById(long productId) { + ProductDO productDO = productMapper.selectById(productId); + + return beanProcess(productDO); + } + + private ProductVO beanProcess(ProductDO obj) { + ProductVO productVO = new ProductVO(); + BeanUtils.copyProperties(obj, productVO); + + return productVO; + } } diff --git a/xdclass-product-service/src/main/java/net/jieyuu/vo/ProductVO.java b/xdclass-product-service/src/main/java/net/jieyuu/vo/ProductVO.java new file mode 100644 index 0000000..17d9364 --- /dev/null +++ b/xdclass-product-service/src/main/java/net/jieyuu/vo/ProductVO.java @@ -0,0 +1,57 @@ +package net.jieyuu.vo; + + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.math.BigDecimal; + + +/** + *

+ * + *

+ * + * @author jieyuu + * @since 2024-08-07 + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class ProductVO { + + private Long id; + + /** + * 标题 + */ + private String title; + + /** + * 封面图 + */ + @JsonProperty("cover_img") + private String coverImg; + + /** + * 详情 + */ + private String detail; + + /** + * 老价格 + */ + @JsonProperty("old_price") + private BigDecimal oldPrice; + + /** + * 新价格 + */ + private BigDecimal price; + + /** + * 库存 + */ + private Integer stock; + +}