码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-01-02 10:52:30      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:int   poj   autowire   用户id   use   收货   clear   img   lan   

此文转载自:https://blog.csdn.net/Beyond_Nothing/article/details/111715258

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

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

标签:int   poj   autowire   用户id   use   收货   clear   img   lan   

原文地址:https://www.cnblogs.com/phyger/p/14205104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!