架构师成长记_第三周_01_2020.11.08

文章目录

  • 收货地址功能实现: 查询用户的所有收货地址列表
    • Controller : AddressController
    • Service : AddressService
    • ServiceImpl : AddressServiceImpl
    • Swagger2 测试

收货地址功能实现: 查询用户的所有收货地址列表

Controller : AddressController

package com.beyond.controller;

import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;
import com.beyond.service.AddressService;
import com.beyond.utils.BEYONDJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "与地址相关",tags = {
     "地址相关的接口"})
@RestController
@RequestMapping("address")
public class AddressController {
     
    /**
     * 用户在确认订单页面,可以针对收货地址所做的操作
     * 1.查询用户的所有收货地址列表
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据用户的id查询收货地址列表",notes = "根据用户的id查询收货地址列表 ",httpMethod = "POST")
    @PostMapping("/list")
    public BEYONDJSONResult list(
            @RequestParam String userId){
     
        if (StringUtils.isBlank(userId)){
     
            return BEYONDJSONResult.errorMsg("");
        }

        List<UserAddress> list = addressService.queryAll(userId);
        return BEYONDJSONResult.ok(list);
    }
}

Service : AddressService

package com.beyond.service;

import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;

import java.util.List;

public interface AddressService {
     

    /**
     * 根据传进来的用户Id 查询购物车地址列表
     * @param userId
     * @return
     */
    public List<UserAddress> queryAll(String userId);

    /**
     * 用户新增地址
     * @param userAddressBO
     */
    public void addNewUserAddress(UserAddressBO userAddressBO);


}

ServiceImpl : AddressServiceImpl

package com.beyond.service.impl;

import com.beyond.mapper.UserAddressMapper;
import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;
import com.beyond.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@SuppressWarnings("ALL")
@Service
public class AddressServiceImpl implements AddressService {
     

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAll(String userId) {
     
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        return userAddressMapper.select(userAddress);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void addNewUserAddress(UserAddressBO userAddressBO) {
     
        // 1.需要判断,当前的用户是后存在地址,如果不存在则进行新增地址,新增地址为默认地址
        Integer isDefault = 0;
        List<UserAddress> addressList = this.queryAll(userAddressBO.getUserId());
        if (addressList == null || addressList.isEmpty() || addressList.size() == 0){
     
            isDefault = 1;
        }

        String newAddressId = sid.nextShort();

        // 2.保存数据到数据库
        UserAddress newAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressBO, newAddress);
        newAddress.setId(newAddressId);
        newAddress.setIsDefault(isDefault);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());

        userAddressMapper.insert(newAddress);
    }
}

Swagger2 测试

架构师成长记_第三周_01_2020.11.08_第1张图片

你可能感兴趣的:(Architect,SpringBoot)