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

This commit is contained in:
shuaidawang 2022-04-14 15:47:37 +08:00
parent 2e3bd00839
commit 4a4097cfb9
11 changed files with 179 additions and 44 deletions

View File

@ -23,5 +23,6 @@ public interface ErrorCodeConstants {
// ========== 用户收件地址 1004004000 ========== // ========== 用户收件地址 1004004000 ==========
ErrorCode ADDRESS_NOT_EXISTS = new ErrorCode(1004004000, "用户收件地址不存在"); ErrorCode ADDRESS_NOT_EXISTS = new ErrorCode(1004004000, "用户收件地址不存在");
ErrorCode ADDRESS_FORBIDDEN = new ErrorCode(1004004001, "没有该操作权限");
} }

View File

@ -0,0 +1,54 @@
### 请求 /create 接口 => 成功
POST {{appApi}}//member/address/create
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6
{
"userId": "245",
"name": "yunai",
"mobile": "15601691300",
"areaCode": "610632",
"detailAddress": "芋道源码 233 号 666 室",
"type": "1"
}
### 请求 /update 接口 => 成功
PUT {{appApi}}//member/address/update
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6
{
"id": "1",
"userId": "245",
"name": "yunai888",
"mobile": "15601691300",
"areaCode": "610632",
"detailAddress": "芋道源码 233 号 666 室",
"type": "1"
}
### 请求 /delete 接口 => 成功
DELETE {{appApi}}//member/address/delete?id=2
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
### 请求 /get 接口 => 成功
GET {{appApi}}//member/address/get?id=1
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
### 请求 /get-default 接口 => 成功
GET {{appApi}}//member/address/get-default
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8
### 请求 /list 接口 => 成功
GET {{appApi}}//member/address/list
Content-Type: application/json
tenant-id: {{appTenentId}}
Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8

View File

@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Api(tags = "用户 APP - 用户收件地址") @Api(tags = "用户 APP - 用户收件地址")
@RestController @RestController
@ -35,14 +36,14 @@ public class AppAddressController {
@ApiOperation("创建用户收件地址") @ApiOperation("创建用户收件地址")
public CommonResult<Long> createAddress(@Valid @RequestBody AppAddressCreateReqVO createReqVO) { public CommonResult<Long> createAddress(@Valid @RequestBody AppAddressCreateReqVO createReqVO) {
return success(addressService.createAddress(createReqVO)); return success(addressService.createAddress(getLoginUserId(), createReqVO));
} }
@PutMapping("/update") @PutMapping("/update")
@ApiOperation("更新用户收件地址") @ApiOperation("更新用户收件地址")
public CommonResult<Boolean> updateAddress(@Valid @RequestBody AppAddressUpdateReqVO updateReqVO) { public CommonResult<Boolean> updateAddress(@Valid @RequestBody AppAddressUpdateReqVO updateReqVO) {
addressService.updateAddress(updateReqVO); addressService.updateAddress(getLoginUserId(), updateReqVO);
return success(true); return success(true);
} }
@ -51,7 +52,7 @@ public class AppAddressController {
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) { public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) {
addressService.deleteAddress(id); addressService.deleteAddress(getLoginUserId(), id);
return success(true); return success(true);
} }
@ -60,25 +61,24 @@ public class AppAddressController {
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
public CommonResult<AppAddressRespVO> getAddress(@RequestParam("id") Long id) { public CommonResult<AppAddressRespVO> getAddress(@RequestParam("id") Long id) {
AddressDO address = addressService.getAddress(id); AddressDO address = addressService.getAddress(getLoginUserId(), id);
return success(AddressConvert.INSTANCE.convert(address));
}
@GetMapping("/get-default")
@ApiOperation("获得默认的用户收件地址")
public CommonResult<AppAddressRespVO> getDefaultUserAddress() {
AddressDO address = addressService.getDefaultUserAddress(getLoginUserId());
return success(AddressConvert.INSTANCE.convert(address)); return success(AddressConvert.INSTANCE.convert(address));
} }
@GetMapping("/list") @GetMapping("/list")
@ApiOperation("获得用户收件地址列表") @ApiOperation("获得用户收件地址列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
public CommonResult<List<AppAddressRespVO>> getAddressList(@RequestParam("ids") Collection<Long> ids) { public CommonResult<List<AppAddressRespVO>> getAddressList() {
List<AddressDO> list = addressService.getAddressList(ids); List<AddressDO> list = addressService.getAddressList(getLoginUserId());
return success(AddressConvert.INSTANCE.convertList(list)); return success(AddressConvert.INSTANCE.convertList(list));
} }
@GetMapping("/page")
@ApiOperation("获得用户收件地址分页")
public CommonResult<PageResult<AppAddressRespVO>> getAddressPage(@Valid AppAddressPageReqVO pageVO) {
PageResult<AddressDO> pageResult = addressService.getAddressPage(pageVO);
return success(AddressConvert.INSTANCE.convertPage(pageResult));
}
} }

View File

@ -12,10 +12,6 @@ import javax.validation.constraints.*;
@Data @Data
public class AppAddressBaseVO { public class AppAddressBaseVO {
@ApiModelProperty(value = "用户编号", required = true)
@NotNull(message = "用户编号不能为空")
private Integer userId;
@ApiModelProperty(value = "收件人名称", required = true) @ApiModelProperty(value = "收件人名称", required = true)
@NotNull(message = "收件人名称不能为空") @NotNull(message = "收件人名称不能为空")
private String name; private String name;

View File

@ -13,7 +13,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
public class AppAddressExportReqVO { public class AppAddressExportReqVO {
@ApiModelProperty(value = "用户编号") @ApiModelProperty(value = "用户编号")
private Integer userId; private Long userId;
@ApiModelProperty(value = "收件人名称") @ApiModelProperty(value = "收件人名称")
private String name; private String name;

View File

@ -15,7 +15,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
public class AppAddressPageReqVO extends PageParam { public class AppAddressPageReqVO extends PageParam {
@ApiModelProperty(value = "用户编号") @ApiModelProperty(value = "用户编号")
private Integer userId; private Long userId;
@ApiModelProperty(value = "收件人名称") @ApiModelProperty(value = "收件人名称")
private String name; private String name;

View File

@ -28,7 +28,7 @@ public class AddressDO extends BaseDO {
/** /**
* 用户编号 * 用户编号
*/ */
private Integer userId; private Long userId;
/** /**
* 收件人名称 * 收件人名称
*/ */

View File

@ -16,24 +16,28 @@ public interface AddressService {
/** /**
* 创建用户收件地址 * 创建用户收件地址
* *
*
* @param userId 用户编号
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
*/ */
Long createAddress(@Valid AppAddressCreateReqVO createReqVO); Long createAddress(Long userId, @Valid AppAddressCreateReqVO createReqVO);
/** /**
* 更新用户收件地址 * 更新用户收件地址
* *
* @param userId 用户编号
* @param updateReqVO 更新信息 * @param updateReqVO 更新信息
*/ */
void updateAddress(@Valid AppAddressUpdateReqVO updateReqVO); void updateAddress(Long userId, @Valid AppAddressUpdateReqVO updateReqVO);
/** /**
* 删除用户收件地址 * 删除用户收件地址
* *
* @param userId 用户编号
* @param id 编号 * @param id 编号
*/ */
void deleteAddress(Long id); void deleteAddress(Long userId, Long id);
/** /**
* 获得用户收件地址 * 获得用户收件地址
@ -46,10 +50,10 @@ public interface AddressService {
/** /**
* 获得用户收件地址列表 * 获得用户收件地址列表
* *
* @param ids 编号 * @param userId 用户编号
* @return 用户收件地址列表 * @return 用户收件地址列表
*/ */
List<AddressDO> getAddressList(Collection<Long> ids); List<AddressDO> getAddressList(Long userId);
/** /**
* 获得用户收件地址分页 * 获得用户收件地址分页
@ -67,4 +71,14 @@ public interface AddressService {
*/ */
List<AddressDO> getAddressList(AppAddressExportReqVO exportReqVO); List<AddressDO> getAddressList(AppAddressExportReqVO exportReqVO);
/**
* 获得用户收件地址
*
* @param userId 用户编号
* @param id 编号
* @return 用户收件地址
*/
AddressDO getAddress(Long userId, Long id);
AddressDO getDefaultUserAddress(Long userId);
} }

View File

@ -1,6 +1,13 @@
package cn.iocoder.yudao.module.member.service.address; package cn.iocoder.yudao.module.member.service.address;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.member.enums.AddressTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.checkerframework.checker.nullness.Opt;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Nullable;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -28,35 +35,65 @@ public class AddressServiceImpl implements AddressService {
private AddressMapper addressMapper; private AddressMapper addressMapper;
@Override @Override
public Long createAddress(AppAddressCreateReqVO createReqVO) { public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) {
// 如果添加的是默认收件地址则将原默认地址修改为非默认
if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) {
List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
if (!CollectionUtils.isEmpty(addressDOs)) {
addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO()
.setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType())));
}
}
// 插入 // 插入
AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); AddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
address.setUserId(userId);
addressMapper.insert(address); addressMapper.insert(address);
// 返回 // 返回
return address.getId(); return address.getId();
} }
@Override @Override
public void updateAddress(AppAddressUpdateReqVO updateReqVO) { public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) {
// 校验存在 // 校验存在,校验是否能够操作
this.validateAddressExists(updateReqVO.getId()); check(userId, updateReqVO.getId());
// 如果修改的是默认收件地址则将原默认地址修改为非默认
if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) {
List<AddressDO> addressDOs = selectListByUserIdAndType(
userId, AddressTypeEnum.DEFAULT.getType());
if (!CollectionUtils.isEmpty(addressDOs)) {
addressDOs.stream().filter(userAddressDO -> !userAddressDO.getId().equals(updateReqVO.getId())) // 过滤掉更新的收件地址
.forEach(userAddressDO -> addressMapper.updateById(new AddressDO()
.setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType())));
}
}
// 更新 // 更新
AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
updateObj.setUserId(userId);
addressMapper.updateById(updateObj); addressMapper.updateById(updateObj);
} }
@Override @Override
public void deleteAddress(Long id) { public void deleteAddress(Long userId, Long id) {
// 校验存在 // 校验存在,校验是否能够操作
this.validateAddressExists(id); check(userId, id);
// 删除 // 删除
addressMapper.deleteById(id); addressMapper.deleteById(id);
} }
private void validateAddressExists(Long id) { /**
if (addressMapper.selectById(id) == null) { * 校验用户收件地址是不是属于该用户
*
* @param userId 用户编号
* @param userAddressId 用户收件地址
*/
private void check(Long userId, Long userAddressId) {
AddressDO addressDO = getAddress(userAddressId);
if(null == addressDO){
throw exception(ADDRESS_NOT_EXISTS); throw exception(ADDRESS_NOT_EXISTS);
} }
if (!addressDO.getUserId().equals(userId)) {
throw exception(ADDRESS_FORBIDDEN);
}
} }
@Override @Override
@ -65,8 +102,8 @@ public class AddressServiceImpl implements AddressService {
} }
@Override @Override
public List<AddressDO> getAddressList(Collection<Long> ids) { public List<AddressDO> getAddressList(Long userId) {
return addressMapper.selectBatchIds(ids); return selectListByUserIdAndType(userId, null);
} }
@Override @Override
@ -79,4 +116,35 @@ public class AddressServiceImpl implements AddressService {
return addressMapper.selectList(exportReqVO); return addressMapper.selectList(exportReqVO);
} }
@Override
public AddressDO getAddress(Long userId, Long id) {
AddressDO address = getAddress(id);
check(userId, id);
return address;
}
/**
* 获取默认地址
* @param userId
* @return
*/
@Override
public AddressDO getDefaultUserAddress(Long userId) {
List<AddressDO> addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
return addressDOList.stream().findFirst().orElse(null);
}
/**
* 根据类型获取地址列表
* @param userId
* @param type null则查询全部
* @return
*/
public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) {
QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId)
.eqIfPresent("type", type);
return addressMapper.selectList(queryWrapperX);
}
} }

View File

@ -16,6 +16,7 @@ import javax.annotation.Resource;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
@ -43,7 +44,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
AppAddressCreateReqVO reqVO = randomPojo(AppAddressCreateReqVO.class); AppAddressCreateReqVO reqVO = randomPojo(AppAddressCreateReqVO.class);
// 调用 // 调用
Long addressId = addressService.createAddress(reqVO); Long addressId = addressService.createAddress(getLoginUserId(), reqVO);
// 断言 // 断言
assertNotNull(addressId); assertNotNull(addressId);
// 校验记录的属性是否正确 // 校验记录的属性是否正确
@ -62,7 +63,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
}); });
// 调用 // 调用
addressService.updateAddress(reqVO); addressService.updateAddress(getLoginUserId(), reqVO);
// 校验是否更新正确 // 校验是否更新正确
AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的 AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, address); assertPojoEquals(reqVO, address);
@ -74,7 +75,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class); AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class);
// 调用, 并断言异常 // 调用, 并断言异常
assertServiceException(() -> addressService.updateAddress(reqVO), ADDRESS_NOT_EXISTS); assertServiceException(() -> addressService.updateAddress(getLoginUserId(), reqVO), ADDRESS_NOT_EXISTS);
} }
@Test @Test
@ -86,7 +87,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
Long id = dbAddress.getId(); Long id = dbAddress.getId();
// 调用 // 调用
addressService.deleteAddress(id); addressService.deleteAddress(getLoginUserId(), id);
// 校验数据不存在了 // 校验数据不存在了
assertNull(addressMapper.selectById(id)); assertNull(addressMapper.selectById(id));
} }
@ -97,7 +98,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
Long id = randomLongId(); Long id = randomLongId();
// 调用, 并断言异常 // 调用, 并断言异常
assertServiceException(() -> addressService.deleteAddress(id), ADDRESS_NOT_EXISTS); assertServiceException(() -> addressService.deleteAddress(getLoginUserId(), id), ADDRESS_NOT_EXISTS);
} }
@Test @Test

View File

@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS "inf_file" (
CREATE TABLE IF NOT EXISTS "member_address" ( CREATE TABLE IF NOT EXISTS "member_address" (
"id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"user_id" int(11) NOT NULL, "user_id" bigint(20) NOT NULL,
"name" varchar(10) NOT NULL, "name" varchar(10) NOT NULL,
"mobile" varchar(20) NOT NULL, "mobile" varchar(20) NOT NULL,
"area_code" int(11) NOT NULL, "area_code" int(11) NOT NULL,
@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS "member_address" (
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE, "deleted" bit NOT NULL DEFAULT FALSE,
"updater" varchar(64) DEFAULT '', "updater" varchar(64) DEFAULT '',
"tenant_id" bigint(20) NOT NULL,
PRIMARY KEY ("id") PRIMARY KEY ("id")
) COMMENT '用户收件地址'; ) COMMENT '用户收件地址';