Commit 4a4097cfb9e122124b7b7ec85556b1d981c74f2c

Authored by shuaidawang
1 parent 2e3bd008

用户收件地址接口开发调试

yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java
... ... @@ -23,5 +23,6 @@ public interface ErrorCodeConstants {
23 23  
24 24 // ========== 用户收件地址 1004004000 ==========
25 25 ErrorCode ADDRESS_NOT_EXISTS = new ErrorCode(1004004000, "用户收件地址不存在");
  26 + ErrorCode ADDRESS_FORBIDDEN = new ErrorCode(1004004001, "没有该操作权限");
26 27  
27 28 }
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.http 0 → 100644
  1 +### 请求 /create 接口 => 成功
  2 +POST {{appApi}}//member/address/create
  3 +Content-Type: application/json
  4 +tenant-id: {{appTenentId}}
  5 +Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6
  6 +
  7 +{
  8 + "userId": "245",
  9 + "name": "yunai",
  10 + "mobile": "15601691300",
  11 + "areaCode": "610632",
  12 + "detailAddress": "芋道源码 233 号 666 室",
  13 + "type": "1"
  14 +}
  15 +
  16 +### 请求 /update 接口 => 成功
  17 +PUT {{appApi}}//member/address/update
  18 +Content-Type: application/json
  19 +tenant-id: {{appTenentId}}
  20 +Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6
  21 +
  22 +{
  23 + "id": "1",
  24 + "userId": "245",
  25 + "name": "yunai888",
  26 + "mobile": "15601691300",
  27 + "areaCode": "610632",
  28 + "detailAddress": "芋道源码 233 号 666 室",
  29 + "type": "1"
  30 +}
  31 +
  32 +### 请求 /delete 接口 => 成功
  33 +DELETE {{appApi}}//member/address/delete?id=2
  34 +Content-Type: application/json
  35 +tenant-id: {{appTenentId}}
  36 +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
  37 +
  38 +### 请求 /get 接口 => 成功
  39 +GET {{appApi}}//member/address/get?id=1
  40 +Content-Type: application/json
  41 +tenant-id: {{appTenentId}}
  42 +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
  43 +
  44 +### 请求 /get-default 接口 => 成功
  45 +GET {{appApi}}//member/address/get-default
  46 +Content-Type: application/json
  47 +tenant-id: {{appTenentId}}
  48 +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
  49 +
  50 +### 请求 /list 接口 => 成功
  51 +GET {{appApi}}//member/address/list
  52 +Content-Type: application/json
  53 +tenant-id: {{appTenentId}}
  54 +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
0 55 \ No newline at end of file
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java
... ... @@ -21,6 +21,7 @@ import java.util.Collection;
21 21 import java.util.List;
22 22  
23 23 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  24 +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
24 25  
25 26 @Api(tags = "用户 APP - 用户收件地址")
26 27 @RestController
... ... @@ -35,14 +36,14 @@ public class AppAddressController {
35 36 @ApiOperation("创建用户收件地址")
36 37  
37 38 public CommonResult<Long> createAddress(@Valid @RequestBody AppAddressCreateReqVO createReqVO) {
38   - return success(addressService.createAddress(createReqVO));
  39 + return success(addressService.createAddress(getLoginUserId(), createReqVO));
39 40 }
40 41  
41 42 @PutMapping("/update")
42 43 @ApiOperation("更新用户收件地址")
43 44  
44 45 public CommonResult<Boolean> updateAddress(@Valid @RequestBody AppAddressUpdateReqVO updateReqVO) {
45   - addressService.updateAddress(updateReqVO);
  46 + addressService.updateAddress(getLoginUserId(), updateReqVO);
46 47 return success(true);
47 48 }
48 49  
... ... @@ -51,7 +52,7 @@ public class AppAddressController {
51 52 @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
52 53  
53 54 public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) {
54   - addressService.deleteAddress(id);
  55 + addressService.deleteAddress(getLoginUserId(), id);
55 56 return success(true);
56 57 }
57 58  
... ... @@ -60,25 +61,24 @@ public class AppAddressController {
60 61 @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
61 62  
62 63 public CommonResult<AppAddressRespVO> getAddress(@RequestParam("id") Long id) {
63   - AddressDO address = addressService.getAddress(id);
  64 + AddressDO address = addressService.getAddress(getLoginUserId(), id);
64 65 return success(AddressConvert.INSTANCE.convert(address));
65 66 }
66 67  
67   - @GetMapping("/list")
68   - @ApiOperation("获得用户收件地址列表")
69   - @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
  68 + @GetMapping("/get-default")
  69 + @ApiOperation("获得默认的用户收件地址")
70 70  
71   - public CommonResult<List<AppAddressRespVO>> getAddressList(@RequestParam("ids") Collection<Long> ids) {
72   - List<AddressDO> list = addressService.getAddressList(ids);
73   - return success(AddressConvert.INSTANCE.convertList(list));
  71 + public CommonResult<AppAddressRespVO> getDefaultUserAddress() {
  72 + AddressDO address = addressService.getDefaultUserAddress(getLoginUserId());
  73 + return success(AddressConvert.INSTANCE.convert(address));
74 74 }
75 75  
76   - @GetMapping("/page")
77   - @ApiOperation("获得用户收件地址分页")
  76 + @GetMapping("/list")
  77 + @ApiOperation("获得用户收件地址列表")
78 78  
79   - public CommonResult<PageResult<AppAddressRespVO>> getAddressPage(@Valid AppAddressPageReqVO pageVO) {
80   - PageResult<AddressDO> pageResult = addressService.getAddressPage(pageVO);
81   - return success(AddressConvert.INSTANCE.convertPage(pageResult));
  79 + public CommonResult<List<AppAddressRespVO>> getAddressList() {
  80 + List<AddressDO> list = addressService.getAddressList(getLoginUserId());
  81 + return success(AddressConvert.INSTANCE.convertList(list));
82 82 }
83 83  
84 84 }
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java
... ... @@ -12,10 +12,6 @@ import javax.validation.constraints.*;
12 12 @Data
13 13 public class AppAddressBaseVO {
14 14  
15   - @ApiModelProperty(value = "用户编号", required = true)
16   - @NotNull(message = "用户编号不能为空")
17   - private Integer userId;
18   -
19 15 @ApiModelProperty(value = "收件人名称", required = true)
20 16 @NotNull(message = "收件人名称不能为空")
21 17 private String name;
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java
... ... @@ -13,7 +13,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
13 13 public class AppAddressExportReqVO {
14 14  
15 15 @ApiModelProperty(value = "用户编号")
16   - private Integer userId;
  16 + private Long userId;
17 17  
18 18 @ApiModelProperty(value = "收件人名称")
19 19 private String name;
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java
... ... @@ -15,7 +15,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
15 15 public class AppAddressPageReqVO extends PageParam {
16 16  
17 17 @ApiModelProperty(value = "用户编号")
18   - private Integer userId;
  18 + private Long userId;
19 19  
20 20 @ApiModelProperty(value = "收件人名称")
21 21 private String name;
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java
... ... @@ -28,7 +28,7 @@ public class AddressDO extends BaseDO {
28 28 /**
29 29 * 用户编号
30 30 */
31   - private Integer userId;
  31 + private Long userId;
32 32 /**
33 33 * 收件人名称
34 34 */
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java
... ... @@ -16,24 +16,28 @@ public interface AddressService {
16 16 /**
17 17 * 创建用户收件地址
18 18 *
  19 + *
  20 + * @param userId 用户编号
19 21 * @param createReqVO 创建信息
20 22 * @return 编号
21 23 */
22   - Long createAddress(@Valid AppAddressCreateReqVO createReqVO);
  24 + Long createAddress(Long userId, @Valid AppAddressCreateReqVO createReqVO);
23 25  
24 26 /**
25 27 * 更新用户收件地址
26 28 *
  29 + * @param userId 用户编号
27 30 * @param updateReqVO 更新信息
28 31 */
29   - void updateAddress(@Valid AppAddressUpdateReqVO updateReqVO);
  32 + void updateAddress(Long userId, @Valid AppAddressUpdateReqVO updateReqVO);
30 33  
31 34 /**
32 35 * 删除用户收件地址
33 36 *
  37 + * @param userId 用户编号
34 38 * @param id 编号
35 39 */
36   - void deleteAddress(Long id);
  40 + void deleteAddress(Long userId, Long id);
37 41  
38 42 /**
39 43 * 获得用户收件地址
... ... @@ -46,10 +50,10 @@ public interface AddressService {
46 50 /**
47 51 * 获得用户收件地址列表
48 52 *
49   - * @param ids 编号
  53 + * @param userId 用户编号
50 54 * @return 用户收件地址列表
51 55 */
52   - List<AddressDO> getAddressList(Collection<Long> ids);
  56 + List<AddressDO> getAddressList(Long userId);
53 57  
54 58 /**
55 59 * 获得用户收件地址分页
... ... @@ -67,4 +71,14 @@ public interface AddressService {
67 71 */
68 72 List<AddressDO> getAddressList(AppAddressExportReqVO exportReqVO);
69 73  
  74 + /**
  75 + * 获得用户收件地址
  76 + *
  77 + * @param userId 用户编号
  78 + * @param id 编号
  79 + * @return 用户收件地址
  80 + */
  81 + AddressDO getAddress(Long userId, Long id);
  82 +
  83 + AddressDO getDefaultUserAddress(Long userId);
70 84 }
... ...
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java
1 1 package cn.iocoder.yudao.module.member.service.address;
2 2  
  3 +import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
  4 +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum;
  5 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6 +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  7 +import org.checkerframework.checker.nullness.Opt;
3 8 import org.springframework.stereotype.Service;
  9 +
  10 +import javax.annotation.Nullable;
4 11 import javax.annotation.Resource;
5 12 import org.springframework.validation.annotation.Validated;
6 13  
... ... @@ -28,35 +35,65 @@ public class AddressServiceImpl implements AddressService {
28 35 private AddressMapper addressMapper;
29 36  
30 37 @Override
31   - public Long createAddress(AppAddressCreateReqVO createReqVO) {
  38 + public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) {
  39 + // 如果添加的是默认收件地址,则将原默认地址修改为非默认
  40 + if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) {
  41 + List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
  42 + if (!CollectionUtils.isEmpty(addressDOs)) {
  43 + addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO()
  44 + .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType())));
  45 + }
  46 + }
32 47 // 插入
33 48 AddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
  49 + address.setUserId(userId);
34 50 addressMapper.insert(address);
35 51 // 返回
36 52 return address.getId();
37 53 }
38 54  
39 55 @Override
40   - public void updateAddress(AppAddressUpdateReqVO updateReqVO) {
41   - // 校验存在
42   - this.validateAddressExists(updateReqVO.getId());
  56 + public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) {
  57 + // 校验存在,校验是否能够操作
  58 + check(userId, updateReqVO.getId());
  59 + // 如果修改的是默认收件地址,则将原默认地址修改为非默认
  60 + if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) {
  61 + List<AddressDO> addressDOs = selectListByUserIdAndType(
  62 + userId, AddressTypeEnum.DEFAULT.getType());
  63 + if (!CollectionUtils.isEmpty(addressDOs)) {
  64 + addressDOs.stream().filter(userAddressDO -> !userAddressDO.getId().equals(updateReqVO.getId())) // 过滤掉更新的收件地址
  65 + .forEach(userAddressDO -> addressMapper.updateById(new AddressDO()
  66 + .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType())));
  67 + }
  68 + }
43 69 // 更新
44 70 AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
  71 + updateObj.setUserId(userId);
45 72 addressMapper.updateById(updateObj);
46 73 }
47 74  
48 75 @Override
49   - public void deleteAddress(Long id) {
50   - // 校验存在
51   - this.validateAddressExists(id);
  76 + public void deleteAddress(Long userId, Long id) {
  77 + // 校验存在,校验是否能够操作
  78 + check(userId, id);
52 79 // 删除
53 80 addressMapper.deleteById(id);
54 81 }
55 82  
56   - private void validateAddressExists(Long id) {
57   - if (addressMapper.selectById(id) == null) {
  83 + /**
  84 + * 校验用户收件地址是不是属于该用户
  85 + *
  86 + * @param userId 用户编号
  87 + * @param userAddressId 用户收件地址
  88 + */
  89 + private void check(Long userId, Long userAddressId) {
  90 + AddressDO addressDO = getAddress(userAddressId);
  91 + if(null == addressDO){
58 92 throw exception(ADDRESS_NOT_EXISTS);
59 93 }
  94 + if (!addressDO.getUserId().equals(userId)) {
  95 + throw exception(ADDRESS_FORBIDDEN);
  96 + }
60 97 }
61 98  
62 99 @Override
... ... @@ -65,8 +102,8 @@ public class AddressServiceImpl implements AddressService {
65 102 }
66 103  
67 104 @Override
68   - public List<AddressDO> getAddressList(Collection<Long> ids) {
69   - return addressMapper.selectBatchIds(ids);
  105 + public List<AddressDO> getAddressList(Long userId) {
  106 + return selectListByUserIdAndType(userId, null);
70 107 }
71 108  
72 109 @Override
... ... @@ -79,4 +116,35 @@ public class AddressServiceImpl implements AddressService {
79 116 return addressMapper.selectList(exportReqVO);
80 117 }
81 118  
  119 + @Override
  120 + public AddressDO getAddress(Long userId, Long id) {
  121 + AddressDO address = getAddress(id);
  122 + check(userId, id);
  123 + return address;
  124 + }
  125 +
  126 + /**
  127 + * 获取默认地址
  128 + * @param userId
  129 + * @return
  130 + */
  131 + @Override
  132 + public AddressDO getDefaultUserAddress(Long userId) {
  133 + List<AddressDO> addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
  134 + return addressDOList.stream().findFirst().orElse(null);
  135 + }
  136 +
  137 + /**
  138 + * 根据类型获取地址列表
  139 + * @param userId
  140 + * @param type null则查询全部
  141 + * @return
  142 + */
  143 + public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) {
  144 + QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId)
  145 + .eqIfPresent("type", type);
  146 + return addressMapper.selectList(queryWrapperX);
  147 + }
  148 +
  149 +
82 150 }
... ...
yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java
... ... @@ -16,6 +16,7 @@ import javax.annotation.Resource;
16 16 import java.util.List;
17 17  
18 18 import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
  19 +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
19 20 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
20 21 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
21 22 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
... ... @@ -43,7 +44,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
43 44 AppAddressCreateReqVO reqVO = randomPojo(AppAddressCreateReqVO.class);
44 45  
45 46 // 调用
46   - Long addressId = addressService.createAddress(reqVO);
  47 + Long addressId = addressService.createAddress(getLoginUserId(), reqVO);
47 48 // 断言
48 49 assertNotNull(addressId);
49 50 // 校验记录的属性是否正确
... ... @@ -62,7 +63,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
62 63 });
63 64  
64 65 // 调用
65   - addressService.updateAddress(reqVO);
  66 + addressService.updateAddress(getLoginUserId(), reqVO);
66 67 // 校验是否更新正确
67 68 AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的
68 69 assertPojoEquals(reqVO, address);
... ... @@ -74,7 +75,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
74 75 AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class);
75 76  
76 77 // 调用, 并断言异常
77   - assertServiceException(() -> addressService.updateAddress(reqVO), ADDRESS_NOT_EXISTS);
  78 + assertServiceException(() -> addressService.updateAddress(getLoginUserId(), reqVO), ADDRESS_NOT_EXISTS);
78 79 }
79 80  
80 81 @Test
... ... @@ -86,7 +87,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
86 87 Long id = dbAddress.getId();
87 88  
88 89 // 调用
89   - addressService.deleteAddress(id);
  90 + addressService.deleteAddress(getLoginUserId(), id);
90 91 // 校验数据不存在了
91 92 assertNull(addressMapper.selectById(id));
92 93 }
... ... @@ -97,7 +98,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
97 98 Long id = randomLongId();
98 99  
99 100 // 调用, 并断言异常
100   - assertServiceException(() -> addressService.deleteAddress(id), ADDRESS_NOT_EXISTS);
  101 + assertServiceException(() -> addressService.deleteAddress(getLoginUserId(), id), ADDRESS_NOT_EXISTS);
101 102 }
102 103  
103 104 @Test
... ...
yudao-module-member/yudao-module-member-impl/src/test/resources/sql/create_tables.sql
... ... @@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS &quot;inf_file&quot; (
32 32  
33 33 CREATE TABLE IF NOT EXISTS "member_address" (
34 34 "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
35   - "user_id" int(11) NOT NULL,
  35 + "user_id" bigint(20) NOT NULL,
36 36 "name" varchar(10) NOT NULL,
37 37 "mobile" varchar(20) NOT NULL,
38 38 "area_code" int(11) NOT NULL,
... ... @@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS &quot;member_address&quot; (
43 43 "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
44 44 "deleted" bit NOT NULL DEFAULT FALSE,
45 45 "updater" varchar(64) DEFAULT '',
  46 + "tenant_id" bigint(20) NOT NULL,
46 47 PRIMARY KEY ("id")
47 48 ) COMMENT '用户收件地址';
48 49  
... ...