- 增加:增加 rest api(用户地址)

This commit is contained in:
fansili 2020-05-08 10:28:38 +08:00
parent 4ec0864626
commit 6b2213d580
15 changed files with 358 additions and 79 deletions

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.user.biz.convert.user;
import cn.iocoder.mall.user.biz.bo.user.UserAddressBO;
import cn.iocoder.mall.user.biz.dataobject.user.UserAddressDO;
import cn.iocoder.mall.user.biz.dataobject.user.UsersUserAddressDO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressAddDTO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressUpdateDTO;
import org.mapstruct.Mapper;
@ -22,14 +22,14 @@ public interface UserAddressConvert {
UserAddressConvert INSTANCE = Mappers.getMapper(UserAddressConvert.class);
@Mappings({})
UserAddressDO convert(UserAddressAddDTO userAddressAddDTO);
UsersUserAddressDO convert(UserAddressAddDTO userAddressAddDTO);
@Mappings({})
UserAddressDO convert(UserAddressUpdateDTO userAddressUpdateDTO);
UsersUserAddressDO convert(UserAddressUpdateDTO userAddressUpdateDTO);
@Mappings({})
UserAddressBO convert(UserAddressDO userAddressDO);
UserAddressBO convert(UsersUserAddressDO userAddressDO);
@Mappings({})
List<UserAddressBO> convertUserAddressBOList(List<UserAddressDO> userAddressDOList);
List<UserAddressBO> convertUserAddressBOList(List<UsersUserAddressDO> userAddressDOList);
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.user.biz.dao.user;
import cn.iocoder.mall.user.biz.dataobject.user.UserAddressDO;
import cn.iocoder.mall.user.biz.dataobject.user.UsersUserAddressDO;
import cn.iocoder.mall.user.biz.enums.user.UserAddressHasDefaultEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@ -16,18 +16,18 @@ import java.util.List;
*/
@Repository
// TODO done FROM 芋艿 to 小范替换成 Mybatis Plus
public interface UserAddressMapper extends BaseMapper<UserAddressDO> {
public interface UserAddressMapper extends BaseMapper<UsersUserAddressDO> {
default List<UserAddressDO> selectByUserId(Integer userId) {
LambdaQueryWrapper<UserAddressDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserAddressDO::getUserId, userId);
default List<UsersUserAddressDO> selectByUserId(Integer userId) {
LambdaQueryWrapper<UsersUserAddressDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UsersUserAddressDO::getUserId, userId);
return selectList(wrapper);
}
default UserAddressDO selectHasDefault(Integer userId) {
LambdaQueryWrapper<UserAddressDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserAddressDO::getUserId, userId);
wrapper.eq(UserAddressDO::getHasDefault, UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue());
default UsersUserAddressDO selectHasDefault(Integer userId) {
LambdaQueryWrapper<UsersUserAddressDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UsersUserAddressDO::getUserId, userId);
wrapper.eq(UsersUserAddressDO::getHasDefault, UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue());
return selectOne(wrapper);
}
}

View File

@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
*/
@Data
@Accessors(chain = true)
public class UserAddressDO extends DeletableDO {
public class UsersUserAddressDO extends DeletableDO {
/**
* 编号

View File

@ -14,15 +14,49 @@ import java.util.List;
*/
public interface UserAddressService {
/**
* 添加地址
*
* @param userAddressAddDTO
*/
void addAddress(UserAddressAddDTO userAddressAddDTO);
/**
* 更新 - 根据id 更新
*
* @param userAddressAddDTO
*/
void updateAddress(UserAddressUpdateDTO userAddressAddDTO);
/**
* 删除 - 更新id 删除
*
* @param userId
* @param addressId
*/
void removeAddress(Integer userId, Integer addressId);
List<UserAddressBO> addressList(Integer userId);
/**
* 获取 - 用户所有地址
*
* @param userId
* @return
*/
List<UserAddressBO> listAddress(Integer userId);
UserAddressBO getAddress(Integer userId, Integer id);
/**
* 获取 - 根据id 获取地址
*
* @param id
* @return
*/
UserAddressBO getAddress(Integer id);
/**
* 获取 - 获取用户 default 地址
*
* @param userId
* @return
*/
UserAddressBO getDefaultAddress(Integer userId);
}

View File

@ -6,7 +6,7 @@ import cn.iocoder.mall.system.biz.enums.SystemErrorCodeEnum;
import cn.iocoder.mall.user.biz.bo.user.UserAddressBO;
import cn.iocoder.mall.user.biz.convert.user.UserAddressConvert;
import cn.iocoder.mall.user.biz.dao.user.UserAddressMapper;
import cn.iocoder.mall.user.biz.dataobject.user.UserAddressDO;
import cn.iocoder.mall.user.biz.dataobject.user.UsersUserAddressDO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressAddDTO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressUpdateDTO;
import cn.iocoder.mall.user.biz.enums.user.UserAddressHasDefaultEnum;
@ -32,112 +32,101 @@ public class UserAddressServiceImpl implements UserAddressService {
@Override
@Transactional
public void addAddress(UserAddressAddDTO userAddressAddDTO) {
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
// 转换do设置默认数据
UsersUserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setCreateTime(new Date());
userAddressDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
// 检查是否设置为默认地址
if (UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue() == userAddressAddDTO.getHasDefault()) {
UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userAddressAddDTO.getUserId());
UsersUserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userAddressAddDTO.getUserId());
if (defaultUserAddress != null) {
userAddressMapper.updateById(
new UserAddressDO()
new UsersUserAddressDO()
.setId(defaultUserAddress.getId())
.setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue())
);
}
}
// 保存地址
userAddressMapper.insert(userAddressDO);
}
@Override
@Transactional
public void updateAddress(UserAddressUpdateDTO userAddressAddDTO) {
UserAddressDO userAddress = userAddressMapper.selectById(userAddressAddDTO.getId());
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_IS_DELETED.getCode());
}
// 检查地址
UsersUserAddressDO userAddress = userAddressMapper.selectById(userAddressAddDTO.getId());
if (userAddress == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
// 删除的地址不能更新
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_IS_DELETED.getCode());
}
// 检查是否设置为默认地址
// 将数据库 default address 设置为 no
if (UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue() == userAddressAddDTO.getHasDefault()) {
UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userAddressAddDTO.getUserId());
UsersUserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userAddressAddDTO.getUserId());
if (defaultUserAddress != null && !userAddressAddDTO.getId().equals(defaultUserAddress.getId())) {
userAddressMapper.updateById(
new UserAddressDO()
new UsersUserAddressDO()
.setId(defaultUserAddress.getId())
.setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue())
);
}
}
UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userAddressAddDTO.getUserId());
if (defaultUserAddress != null && !userAddressAddDTO.getId().equals(defaultUserAddress.getId())) {
userAddressMapper.updateById(
new UserAddressDO()
.setId(defaultUserAddress.getId())
.setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue())
);
}
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
// 转换 vo, 并保存数据
UsersUserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setUpdateTime(new Date());
userAddressMapper.updateById(userAddressDO);
}
@Override
public void removeAddress(Integer userId, Integer addressId) {
UserAddressDO userAddress = userAddressMapper.selectById(addressId);
// checked address is exists.
UsersUserAddressDO userAddress = userAddressMapper.selectById(addressId);
if (userAddress == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
// skip
return;
}
if (userAddress == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
// 更新状态为 remove
userAddressMapper.updateById(
(UserAddressDO) new UserAddressDO()
(UsersUserAddressDO) new UsersUserAddressDO()
.setId(addressId)
.setDeleted(DeletedStatusEnum.DELETED_YES.getValue())
);
}
@Override
public List<UserAddressBO> addressList(Integer userId) {
List<UserAddressDO> userAddressDOList = userAddressMapper.selectByUserId(userId);
List<UserAddressBO> userAddressBOList = UserAddressConvert
.INSTANCE.convertUserAddressBOList(userAddressDOList);
public List<UserAddressBO> listAddress(Integer userId) {
List<UsersUserAddressDO> userAddressDOList = userAddressMapper.selectByUserId(userId);
List<UserAddressBO> userAddressBOList = UserAddressConvert.INSTANCE.convertUserAddressBOList(userAddressDOList);
return userAddressBOList;
}
@Override
public UserAddressBO getAddress(Integer userId, Integer id) {
UserAddressDO userAddress = userAddressMapper.selectById(id);
if (userAddress == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_GET_ADDRESS_NOT_EXISTS.getCode());
}
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.USER_ADDRESS_IS_DELETED.getCode());
}
UserAddressBO userAddressBO = UserAddressConvert.INSTANCE.convert(userAddress);
return userAddressBO;
public UserAddressBO getAddress(Integer id) {
UsersUserAddressDO userAddress = userAddressMapper.selectById(id);
return UserAddressConvert.INSTANCE.convert(userAddress);
}
@Override
public UserAddressBO getDefaultAddress(Integer userId) {
UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userId);
UsersUserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault(userId);
return UserAddressConvert.INSTANCE.convert(defaultUserAddress);
}
}

View File

@ -13,17 +13,11 @@
<dependencies>
<!-- Mall 相关 -->
<!-- <dependency>-->
<!-- <groupId>cn.iocoder.mall</groupId>-->
<!-- <artifactId>user-biz</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- Mall 相关 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>common-framework</artifactId>
<artifactId>user-biz</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- Web 相关 -->
@ -32,8 +26,16 @@
<artifactId>mall-spring-boot-starter-web</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- TODO 云服务 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>mall-spring-boot-starter-security</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>mall-spring-boot-starter-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,80 @@
package cn.iocoder.mall.user.rest.controller.user;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.core.context.UserSecurityContextHolder;
import cn.iocoder.mall.user.biz.dto.user.UserAddressAddDTO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressUpdateDTO;
import cn.iocoder.mall.user.biz.service.user.UserAddressService;
import cn.iocoder.mall.user.rest.convert.UserAddressConvert;
import cn.iocoder.mall.user.rest.request.UserAddressAddRequest;
import cn.iocoder.mall.user.rest.request.UserAddressUpdateRequest;
import cn.iocoder.mall.user.rest.response.UserAddressResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户地址(user API)
*
* author: sin
* time: 2020/5/8 9:50
*/
@RestController
@Api(tags = "用户地址(user API)")
@RequestMapping("/users/user-address")
public class UsersUserAddressController {
@Autowired
private UserAddressService userAddressService;
@GetMapping("list-address")
@ApiOperation("获取 - 地址列表(all)")
public CommonResult<List<UserAddressResponse>> listAddress() {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
return CommonResult.success(UserAddressConvert.INSTANCE.convert(userAddressService.listAddress(userId)));
}
@GetMapping("{addressId}")
@ApiOperation("获取 - 根据id获取")
public CommonResult<UserAddressResponse> getAddress(@PathVariable("addressId") Integer addressId) {
return CommonResult.success(UserAddressConvert.INSTANCE.convert(userAddressService.getAddress(addressId)));
}
@GetMapping("default")
@ApiOperation("获取 - 获取默认地址")
public CommonResult<UserAddressResponse> getDefaultAddress() {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
return CommonResult.success(UserAddressConvert.INSTANCE.convert(userAddressService.getDefaultAddress(userId)));
}
@DeleteMapping("/{addressId}/remove")
@ApiOperation("删除 - 根据id删除")
public CommonResult<UserAddressResponse> getDefaultAddress(@PathVariable("addressId") Integer addressId) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
userAddressService.removeAddress(userId, addressId);
return CommonResult.success(null);
}
@PostMapping("/add-address")
@ApiOperation("添加地址")
public CommonResult<UserAddressResponse> addAddress(@RequestBody UserAddressAddRequest userAddressAddRequest) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
UserAddressAddDTO userAddressAddDTO = UserAddressConvert.INSTANCE.convert(userAddressAddRequest);
userAddressAddDTO.setUserId(userId);
userAddressService.addAddress(userAddressAddDTO);
return CommonResult.success(null);
}
@PutMapping("/update-address")
@ApiOperation("更新地址")
public CommonResult<UserAddressResponse> updateAddress(@RequestBody UserAddressUpdateRequest userAddressAddRequest) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
UserAddressUpdateDTO userAddressAddDTO = UserAddressConvert.INSTANCE.convert(userAddressAddRequest);
userAddressAddDTO.setUserId(userId);
userAddressService.updateAddress(userAddressAddDTO);
return CommonResult.success(null);
}
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.mall.user.rest.convert;
import cn.iocoder.mall.user.biz.bo.user.UserAddressBO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressAddDTO;
import cn.iocoder.mall.user.biz.dto.user.UserAddressUpdateDTO;
import cn.iocoder.mall.user.rest.request.UserAddressAddRequest;
import cn.iocoder.mall.user.rest.request.UserAddressUpdateRequest;
import cn.iocoder.mall.user.rest.response.UserAddressResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 用户地址 convert
*
* author: sin
* time: 2020/5/8 10:01
*/
@Mapper
public interface UserAddressConvert {
UserAddressConvert INSTANCE = Mappers.getMapper(UserAddressConvert.class);
@Mappings({})
List<UserAddressResponse> convert(List<UserAddressBO> userAddressBOList);
@Mappings({})
UserAddressResponse convert(UserAddressBO userAddressBO);
@Mappings({})
UserAddressAddDTO convert(UserAddressAddRequest userAddressAddRequest);
@Mappings({})
UserAddressUpdateDTO convert(UserAddressUpdateRequest userAddressUpdateRequest);
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.user.rest.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 用户地址 add
*
* @author Sin
* @time 2019-04-06 13:25
*/
@Data
@Accessors(chain = true)
@ApiModel("用户地址(添加)")
public class UserAddressAddRequest implements Serializable {
@NotNull
@ApiModelProperty("收件区域编号")
private String areaNo;
@NotNull
@ApiModelProperty("收件人名称")
private String name;
@NotNull
@ApiModelProperty("收件手机号")
private String mobile;
@NotNull
@ApiModelProperty("收件详细地址")
private String address;
@NotNull
@ApiModelProperty("是否默认 1 不是 2 是")
private Integer hasDefault;
}

View File

@ -0,0 +1,45 @@
package cn.iocoder.mall.user.rest.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 用户地址 更新
*
* @author Sin
* @time 2019-04-06 13:28
*/
@Data
@Accessors(chain = true)
@ApiModel("用户地址(更新)")
public class UserAddressUpdateRequest implements Serializable {
@NotNull
@ApiModelProperty("编号")
private Integer id;
@NotNull
@ApiModelProperty("收件区域编号")
private String areaNo;
@NotNull
@ApiModelProperty("收件人名称")
private String name;
@NotNull
@ApiModelProperty("收件手机号")
private String mobile;
@NotNull
@ApiModelProperty("收件详细地址")
private String address;
@NotNull
@ApiModelProperty("是否默认地址")
private Integer hasDefault;
}

View File

@ -0,0 +1,5 @@
/**
* author: sin
* time: 2020/5/8 9:57
*/
package cn.iocoder.mall.user.rest.request;

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.user.rest.response;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 用户地址
*
* @author Sin
* @time 2019-04-06 13:28
*/
@Data
@Accessors(chain = true)
@ApiModel(value = "用户地址")
public class UserAddressResponse implements Serializable {
@ApiModelProperty("编号")
private Integer id;
@ApiModelProperty("用户编号")
private Integer userId;
@ApiModelProperty("收件区域编号")
private String areaNo;
@ApiModelProperty("收件人名称")
private String name;
@ApiModelProperty("收件手机号")
private String mobile;
@ApiModelProperty("收件详细地址")
private String address;
@ApiModelProperty("是否默认")
private Integer hasDefault;
}

View File

@ -0,0 +1,5 @@
/**
* author: sin
* time: 2020/5/8 9:57
*/
package cn.iocoder.mall.user.rest.response;

View File

@ -22,7 +22,7 @@ public interface UserAddressRPC {
List<UserAddressResponse> addressList(Integer userId);
UserAddressResponse getAddress(Integer userId, Integer id);
UserAddressResponse getAddress(Integer id);
UserAddressResponse getDefaultAddress(Integer userId);
}

View File

@ -34,12 +34,12 @@ public class UserAddressRPCImpl implements UserAddressRPC {
@Override
public List<UserAddressResponse> addressList(Integer userId) {
return UserAddressRPCConvert.INSTANCE.convert(userAddressService.addressList(userId));
return UserAddressRPCConvert.INSTANCE.convert(userAddressService.listAddress(userId));
}
@Override
public UserAddressResponse getAddress(Integer userId, Integer id) {
return UserAddressRPCConvert.INSTANCE.convert(userAddressService.getAddress(userId, id));
public UserAddressResponse getAddress(Integer id) {
return UserAddressRPCConvert.INSTANCE.convert(userAddressService.getAddress(id));
}
@Override