- 添加用户地址api

This commit is contained in:
sin 2019-04-06 14:32:41 +08:00
parent 2bcaaf2027
commit 434ed6f2f3
15 changed files with 884 additions and 1 deletions

View File

@ -0,0 +1,62 @@
package cn.iocoder.mall.user.application.controller.users;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.application.convert.UserAddressConvert;
import cn.iocoder.mall.user.application.po.UserAddressAddPO;
import cn.iocoder.mall.user.application.po.UserAddressUpdatePO;
import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder;
import cn.iocoder.mall.user.service.api.UserAddressService;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
* 用户地址
*
* @author Sin
* @time 2019-04-06 14:11
*/
@RestController
@RequestMapping("user/address")
@Api(description = "用户地址API")
public class UserAddressController {
@Autowired
private UserAddressService userAddressService;
@PostMapping("add")
@ApiOperation(value = "用户地址-添加")
public CommonResult addAddress(@RequestBody @Validated UserAddressAddPO userAddressAddPO) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
UserAddressAddDTO userAddressAddDTO = UserAddressConvert.INSTANCE.convert(userAddressAddPO);
userAddressAddDTO.setUserId(userId);
return userAddressService.addAddress(userAddressAddDTO);
}
@PostMapping("update")
@ApiOperation(value = "用户地址-更新")
public CommonResult updateAddress(@RequestBody @Validated UserAddressUpdatePO userAddressUpdatePO) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
UserAddressUpdateDTO userAddressUpdateDTO = UserAddressConvert.INSTANCE.convert(userAddressUpdatePO);
userAddressUpdateDTO.setUserId(userId);
return userAddressService.updateAddress(userAddressUpdateDTO);
}
@DeleteMapping("remove")
@ApiOperation(value = "用户地址-删除")
public CommonResult removeAddress(@RequestParam("id") Integer id) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
return userAddressService.removeAddress(userId, id);
}
@GetMapping("list")
@ApiOperation(value = "用户地址列表")
public CommonResult addressList() {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
return userAddressService.addressList(userId);
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.mall.user.application.convert;
import cn.iocoder.mall.user.application.po.UserAddressAddPO;
import cn.iocoder.mall.user.application.po.UserAddressUpdatePO;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @author Sin
* @time 2019-04-06 14:19
*/
@Mapper
public interface UserAddressConvert {
UserAddressConvert INSTANCE = Mappers.getMapper(UserAddressConvert.class);
@Mappings({})
UserAddressAddDTO convert(UserAddressAddPO userAddressAddPO);
@Mappings({})
UserAddressUpdateDTO convert(UserAddressUpdatePO userAddressUpdatePO);
}

View File

@ -0,0 +1,91 @@
package cn.iocoder.mall.user.application.po;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 用户地址 add
*
* @author Sin
* @time 2019-04-06 14:13
*/
@ApiModel("用户地址")
public class UserAddressAddPO implements Serializable {
/**
* 收件区域编号
*/
@ApiModelProperty("区域编号")
@NotNull(message = "区域编号不能为空!")
private String areaNo;
/**
* 收件人名称
*/
@ApiModelProperty("收件人名称")
@NotNull(message = "请填写收人信息!")
private String name;
/**
* 收件手机号
*/
@ApiModelProperty("收件手机号")
@NotNull(message = "手机号为不能为空!")
@Size(min = 11, max = 11, message = "手机号为 11 位!")
private String mobile;
/**
* 收件详细地址
*/
@ApiModelProperty("收件详细地址")
@NotNull(message = "详细地址不能为空")
@Size(min = 10, max = 100, message = "地址在 10 ~ 100 字之间!")
private String address;
@Override
public String toString() {
return "UserAddressAddPO{" +
"areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public String getAreaNo() {
return areaNo;
}
public UserAddressAddPO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressAddPO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressAddPO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressAddPO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@ -0,0 +1,108 @@
package cn.iocoder.mall.user.application.po;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 用户地址 add
*
* @author Sin
* @time 2019-04-06 14:13
*/
@ApiModel("用户地址-更新")
public class UserAddressUpdatePO implements Serializable {
/**
* 收件区域编号
*/
@ApiModelProperty("地址编号")
@NotNull(message = "地址编号为空!")
private Integer id;
/**
* 收件区域编号
*/
@ApiModelProperty("区域编号")
@NotNull(message = "区域编号不能为空!")
private String areaNo;
/**
* 收件人名称
*/
@ApiModelProperty("收件人名称")
@NotNull(message = "请填写收人信息!")
private String name;
/**
* 收件手机号
*/
@ApiModelProperty("收件手机号")
@NotNull(message = "手机号为不能为空!")
@Size(min = 11, max = 11, message = "手机号为 11 位!")
private String mobile;
/**
* 收件详细地址
*/
@ApiModelProperty("收件详细地址")
@NotNull(message = "详细地址不能为空")
@Size(min = 10, max = 100, message = "地址在 10 ~ 100 字之间!")
private String address;
@Override
public String toString() {
return "UserAddressUpdatePO{" +
"id=" + id +
", areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public UserAddressUpdatePO setId(Integer id) {
this.id = id;
return this;
}
public String getAreaNo() {
return areaNo;
}
public UserAddressUpdatePO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressUpdatePO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressUpdatePO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressUpdatePO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.mall.user.service.api;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.service.api.bo.UserAddressBO;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import java.util.List;
/**
* 用户地址
*
* @author Sin
* @time 2019-04-06 13:24
*/
public interface UserAddressService {
CommonResult addAddress(UserAddressAddDTO userAddressAddDTO);
CommonResult updateAddress(UserAddressUpdateDTO userAddressAddDTO);
CommonResult removeAddress(Integer userId, Integer addressId);
CommonResult<List<UserAddressBO>> addressList(Integer userId);
}

View File

@ -0,0 +1,8 @@
package cn.iocoder.mall.user.service.api.bo;
/**
* @author Sin
* @time 2019-04-06 13:28
*/
public class UserAddressBO {
}

View File

@ -0,0 +1,48 @@
package cn.iocoder.mall.user.service.api.bo;
import java.io.Serializable;
import java.util.List;
/**
* 用户地址 page
*
* @author Sin
* @time 2019-04-06 13:56
*/
public class UserAddressPageBO implements Serializable {
/**
* 总量
*/
private Integer total;
/**
* 地址
*/
private List<UserAddressBO> list;
@Override
public String toString() {
return "UserAddressPageBO{" +
"total=" + total +
", list=" + list +
'}';
}
public Integer getTotal() {
return total;
}
public UserAddressPageBO setTotal(Integer total) {
this.total = total;
return this;
}
public List<UserAddressBO> getList() {
return list;
}
public UserAddressPageBO setList(List<UserAddressBO> list) {
this.list = list;
return this;
}
}

View File

@ -30,7 +30,11 @@ public enum UserErrorCodeEnum {
MOBILE_CODE_USED(1001003002, "验证码已使用"),
MOBILE_CODE_NOT_CORRECT(1001003003, "验证码不正确"),
MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY(1001003004, "超过每日短信发送数量"),
MOBILE_CODE_SEND_TOO_FAST(1001003005, "短信发送过于频率")
MOBILE_CODE_SEND_TOO_FAST(1001003005, "短信发送过于频率"),
// ========== 用户地址 ==========
USER_ADDRESS_NOT_EXISTENT(1001004000, "用户地址不存在!"),
USER_ADDRESS_IS_DELETED(1001004000, "用户地址已被删除!"),
;
private final int code;

View File

@ -0,0 +1,89 @@
package cn.iocoder.mall.user.service.api.dto;
import java.io.Serializable;
/**
* 用户地址 add
*
* @author Sin
* @time 2019-04-06 13:25
*/
public class UserAddressAddDTO implements Serializable {
/**
* 收件区域编号
*/
private Integer userId;
/**
* 收件区域编号
*/
private String areaNo;
/**
* 收件人名称
*/
private String name;
/**
* 收件手机号
*/
private String mobile;
/**
* 收件详细地址
*/
private String address;
@Override
public String toString() {
return "UserAddressAddDTO{" +
"userId=" + userId +
", areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getUserId() {
return userId;
}
public UserAddressAddDTO setUserId(Integer userId) {
this.userId = userId;
return this;
}
public String getAreaNo() {
return areaNo;
}
public UserAddressAddDTO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressAddDTO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressAddDTO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressAddDTO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@ -0,0 +1,103 @@
package cn.iocoder.mall.user.service.api.dto;
import java.io.Serializable;
/**
* 用户地址 更新
*
* @author Sin
* @time 2019-04-06 13:28
*/
public class UserAddressUpdateDTO implements Serializable {
/**
* 编号
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 收件区域编号
*/
private String areaNo;
/**
* 收件人名称
*/
private String name;
/**
* 收件手机号
*/
private String mobile;
/**
* 收件详细地址
*/
private String address;
@Override
public String toString() {
return "UserAddressUpdateDTO{" +
"id=" + id +
", userId=" + userId +
", areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public UserAddressUpdateDTO setId(Integer id) {
this.id = id;
return this;
}
public Integer getUserId() {
return userId;
}
public UserAddressUpdateDTO setUserId(Integer userId) {
this.userId = userId;
return this;
}
public String getAreaNo() {
return areaNo;
}
public UserAddressUpdateDTO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressUpdateDTO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressUpdateDTO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressUpdateDTO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@ -0,0 +1,32 @@
package cn.iocoder.mall.user.convert;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import cn.iocoder.mall.user.service.api.bo.UserAddressBO;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 用户地址 convert
*
* @author Sin
* @time 2019-04-06 13:38
*/
@Mapper
public interface UserAddressConvert {
UserAddressConvert INSTANCE = Mappers.getMapper(UserAddressConvert.class);
@Mappings({})
UserAddressDO convert(UserAddressAddDTO userAddressAddDTO);
@Mappings({})
UserAddressDO convert(UserAddressUpdateDTO userAddressUpdateDTO);
@Mappings({})
List<UserAddressBO> convertUserAddressBOList(List<UserAddressDO> userAddressDOList);
}

View File

@ -0,0 +1,34 @@
package cn.iocoder.mall.user.dao;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 用户 地址
*
* @author Sin
* @time 2019-04-06 13:29
*/
@Repository
public interface UserAddressMapper {
int insert(UserAddressDO userAddressDO);
int updateById(
@Param("id") Integer id,
@Param("userAddressDO") UserAddressDO userAddressDO
);
List<UserAddressDO> selectByUserIdAndDeleted(
Integer deleted,
Integer userId
);
UserAddressDO selectByUserIdAndId(
Integer userId,
Integer id
);
}

View File

@ -0,0 +1,103 @@
package cn.iocoder.mall.user.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
/**
* 用户地址信息
*
* @author Sin
* @time 2019-04-06 13:22
*/
public class UserAddressDO extends DeletableDO {
/**
* 编号
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 收件区域编号
*/
private String areaNo;
/**
* 收件人名称
*/
private String name;
/**
* 收件手机号
*/
private String mobile;
/**
* 收件详细地址
*/
private String address;
@Override
public String toString() {
return "UserAddressDO{" +
"id=" + id +
", userId=" + userId +
", areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public UserAddressDO setId(Integer id) {
this.id = id;
return this;
}
public Integer getUserId() {
return userId;
}
public UserAddressDO setUserId(Integer userId) {
this.userId = userId;
return this;
}
public String getAreaNo() {
return areaNo;
}
public UserAddressDO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressDO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressDO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressDO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@ -0,0 +1,92 @@
package cn.iocoder.mall.user.service;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.convert.UserAddressConvert;
import cn.iocoder.mall.user.dao.UserAddressMapper;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import cn.iocoder.mall.user.service.api.UserAddressService;
import cn.iocoder.mall.user.service.api.bo.UserAddressBO;
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 用户地址
*
* @author Sin
* @time 2019-04-06 13:26
*/
@Service
public class UserAddressServiceImpl implements UserAddressService {
@Autowired
private UserAddressMapper userAddressMapper;
@Override
public CommonResult addAddress(UserAddressAddDTO userAddressAddDTO) {
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setCreateTime(new Date());
userAddressDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
userAddressMapper.insert(userAddressDO);
return CommonResult.success(null);
}
@Override
public CommonResult updateAddress(UserAddressUpdateDTO userAddressAddDTO) {
UserAddressDO userAddress = userAddressMapper
.selectByUserIdAndId(userAddressAddDTO.getUserId(), userAddressAddDTO.getId());
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
return CommonResult.success(UserErrorCodeEnum.USER_ADDRESS_IS_DELETED.getCode());
}
if (userAddress == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setUpdateTime(new Date());
userAddressMapper.updateById(userAddressDO.getId(), userAddressDO);
return CommonResult.success(null);
}
@Override
public CommonResult removeAddress(Integer userId, Integer addressId) {
UserAddressDO userAddress = userAddressMapper.selectByUserIdAndId(userId, addressId);
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
// skip
return CommonResult.success(null);
}
if (userAddress == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
userAddressMapper.updateById(
addressId,
(UserAddressDO) new UserAddressDO()
.setDeleted(DeletedStatusEnum.DELETED_YES.getValue())
);
return CommonResult.success(null);
}
@Override
public CommonResult<List<UserAddressBO>> addressList(Integer userId) {
List<UserAddressDO> userAddressDOList = userAddressMapper
.selectByUserIdAndDeleted(DeletedStatusEnum.DELETED_NO.getValue(), userId);
List<UserAddressBO> userAddressBOList = UserAddressConvert
.INSTANCE.convertUserAddressBOList(userAddressDOList);
return CommonResult.success(userAddressBOList);
}
}

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.user.dao.UserAddressMapper">
<sql id="FIELDS">
id, user_id, areaNo, `name`, mobile, address,
create_time, update_time, deleted
</sql>
<insert id="insert" parameterType="UserAddressDO" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_address (
user_id, areaNo, `name`, mobile, address
) VALUES (
#{userId}, #{areaNo}, #{name}, #{mobile}, #{address},
#{createTime}, #{updateTime}, #{deleted}
)
</insert>
<update id="updateById" resultType="Integer">
UPDATE user_address
<set>
<if test="area_no != null">
, area_no = #{areaNo}
</if>
<if test="name != null">
, `name` = #{name}
</if>
<if test="mobile != null">
, mobile = #{mobile}
</if>
<if test="address != null">
, address = #{address}
</if>
<if test="updateTime != null">
, update_time = #{updateTime}
</if>
<if test="deleted != null">
, deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
<select id="selectByUserIdAndId" resultType="cn.iocoder.mall.user.dataobject.UserAddressDO">
SELECT
<include refid="FIELDS" />
FROM user_address
WHERE user_id = #{userId}
AND id = #{id}
</select>
<select id="selectByUserIdAndDeleted" resultType="cn.iocoder.mall.user.dataobject.UserAddressDO">
SELECT
<include refid="FIELDS" />
FROM user_address
WHERE deleted = #{deleted}
AND `user_id` = #{userId}
</select>
</mapper>