dev diary day1

今天继续昨天的工作,主要内容为:1、编写更多的dao,repository,service以及controller层代码;2、加入DTO,用于对象传递;3、加入enums包,用于处理一些状态;4、加入异常处理机制;5、编写工具类;6、编写VO;7、进行相应的测试

  • BuyerProductController:编写了一个用于返回所有已上架商品的接口。逻辑:1、首先使用productService进行查询,将上架的商品的productInfo存在一个List中。2、然后使用lambda表达式,将这些商品的类目存在一个LIst中,使用categorySerivce的方法来查询这个List。3、进行数据拼装,这里使用到了ProductVO类,这个类用于数据传递,他拥有其他类的部分变量或者更多变量,以达到,前端只传递部分变量时,也能新建对象。
package com.imooc.weixin.controller;

import com.imooc.weixin.VO.ProductInfoVO;
import com.imooc.weixin.VO.ProductVO;
import com.imooc.weixin.VO.ResultVO;
import com.imooc.weixin.dataobject.ProductCategory;
import com.imooc.weixin.dataobject.ProductInfo;
import com.imooc.weixin.service.CategoryService;
import com.imooc.weixin.service.ProductService;
import com.imooc.weixin.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController
{
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list()
    {
        //1.查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2.查询类目(一次性查询)
        //传统方法
//        for(ProductInfo productInfo : productInfoList)
//        {
//            categoryTypeList.add(productInfo.getCategoryType());
//        }

        //精简做法(java8特性,lambda表达式)
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //3.数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory : productCategoryList)
        {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo : productInfoList)
            {
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType()))
                {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }

            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);
    }
}
  • 进行几个daoobject的编写,其中有的变量存在默认值,使用了enum类来实现功能。
    package com.imooc.weixin.dataobject;
    
    import com.imooc.weixin.enums.OrderStatusEnum;
    import com.imooc.weixin.enums.PayStatusEnum;
    import lombok.Data;
    import org.hibernate.annotations.DynamicUpdate;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Transient;
    import java.math.BigDecimal;
    import java.util.Date;
    import java.util.List;
    
    @Entity
    @Data
    @DynamicUpdate
    public class OrderMaster {
        @Id
        private String orderId;
    
        private String buyerName;
    
        private String buyerPhone;
    
        private String buyerAddress;
    
        private String buyerOpenid;
    
        private BigDecimal orderAmount;
    
        private Integer orderStatus = OrderStatusEnum.NEW.getCode();
    
        private Integer payStatus = PayStatusEnum.WAIT.getCode();
    
        private Date createTime;
    
        private Date updateTime;
    }
    package com.imooc.weixin.enums;
    
    import lombok.Getter;
    
    @Getter
    public enum OrderStatusEnum {
    
        NEW(0,"新订单"),
        FINISHED(1,"完结"),
        CANCELED(2,"已取消"),
        ;
    
        private Integer code;
    
        private String msg;
    
        OrderStatusEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }

    使用这些enum类使得代码更加清晰,避免了后期发现状态为0,1而不清楚到底是什么状态的错误

  • 编写了一些工具类,包括生成orderID的KeyUti类以及ResultVO需要的一些静态函数的工具类。
  • 项目代码地址:https://github.com/CDC-Melo/weixin_sell

猜你喜欢

转载自www.cnblogs.com/qumasha/p/13210653.html
Dev