删除短信渠道时,增加校验是否有模板

This commit is contained in:
YunaiV 2021-04-12 01:07:31 +08:00
parent 2f75a716c6
commit 1740128dc3
7 changed files with 48 additions and 3 deletions

View File

@ -35,5 +35,4 @@ public class SysSmsChannelBaseVO {
@URL(message = "回调 URL 格式不正确") @URL(message = "回调 URL 格式不正确")
private String callbackUrl; private String callbackUrl;
} }

View File

@ -41,4 +41,8 @@ public interface SysSmsTemplateMapper extends BaseMapperX<SysSmsTemplateDO> {
.orderByDesc("id")); .orderByDesc("id"));
} }
default Integer selectCountByChannelId(Long channelId) {
return selectCount("channel_id", channelId);
}
} }

View File

@ -81,6 +81,7 @@ public interface SysErrorCodeConstants {
// ========== 短信渠道 1002011000 ========== // ========== 短信渠道 1002011000 ==========
ErrorCode SMS_CHANNEL_NOT_EXISTS = new ErrorCode(1002011000, "短信渠道不存在"); ErrorCode SMS_CHANNEL_NOT_EXISTS = new ErrorCode(1002011000, "短信渠道不存在");
ErrorCode SMS_CHANNEL_DISABLE = new ErrorCode(1002011001, "短信渠道不处于开启状态,不允许选择"); ErrorCode SMS_CHANNEL_DISABLE = new ErrorCode(1002011001, "短信渠道不处于开启状态,不允许选择");
ErrorCode SMS_CHANNEL_HAS_CHILDREN = new ErrorCode(1002011002, "无法删除,该短信渠道还有短信模板");
// ========== 短信模板 1002011000 ========== // ========== 短信模板 1002011000 ==========
ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002011000, "短信模板不存在"); ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002011000, "短信模板不存在");

View File

@ -91,4 +91,12 @@ public interface SysSmsTemplateService {
*/ */
List<SysSmsTemplateDO> getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO); List<SysSmsTemplateDO> getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO);
/**
* 获得指定短信渠道下的短信模板数量
*
* @param channelId 短信渠道编号
* @return 数量
*/
Integer countByChannelId(Long channelId);
} }

View File

@ -11,6 +11,7 @@ import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsChannelConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsChannelMapper; import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsChannelMapper;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -19,6 +20,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS;
/** /**
@ -36,6 +38,9 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
@Resource @Resource
private SysSmsChannelMapper smsChannelMapper; private SysSmsChannelMapper smsChannelMapper;
@Resource
private SysSmsTemplateService smsTemplateService;
@Override @Override
@PostConstruct @PostConstruct
public void initSmsClients() { public void initSmsClients() {
@ -70,6 +75,10 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
public void deleteSmsChannel(Long id) { public void deleteSmsChannel(Long id) {
// 校验存在 // 校验存在
this.validateSmsChannelExists(id); this.validateSmsChannelExists(id);
// 校验是否有字典数据
if (smsTemplateService.countByChannelId(id) > 0) {
throw exception(SMS_CHANNEL_HAS_CHILDREN);
}
// 更新 // 更新
smsChannelMapper.deleteById(id); smsChannelMapper.deleteById(id);
} }

View File

@ -21,7 +21,6 @@ import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collection; import java.util.Collection;
@ -40,7 +39,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
* @date 2021/1/25 9:25 * @date 2021/1/25 9:25
*/ */
@Service @Service
@Validated
public class SysSmsTemplateServiceImpl implements SysSmsTemplateService { public class SysSmsTemplateServiceImpl implements SysSmsTemplateService {
/** /**
@ -142,6 +140,11 @@ public class SysSmsTemplateServiceImpl implements SysSmsTemplateService {
return smsTemplateMapper.selectList(exportReqVO); return smsTemplateMapper.selectList(exportReqVO);
} }
@Override
public Integer countByChannelId(Long channelId) {
return smsTemplateMapper.selectCountByChannelId(channelId);
}
@VisibleForTesting @VisibleForTesting
public SysSmsChannelDO checkSmsChannel(Long channelId) { public SysSmsChannelDO checkSmsChannel(Long channelId) {
SysSmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); SysSmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId);

View File

@ -20,12 +20,15 @@ import javax.annotation.Resource;
import java.util.function.Consumer; import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
import static cn.iocoder.dashboard.util.RandomUtils.*; import static cn.iocoder.dashboard.util.RandomUtils.*;
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
/** /**
* {@link SysSmsChannelServiceImpl} 的单元测试类 * {@link SysSmsChannelServiceImpl} 的单元测试类
@ -44,6 +47,9 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
@Resource @Resource
private SysSmsChannelMapper smsChannelMapper; private SysSmsChannelMapper smsChannelMapper;
@MockBean
private SysSmsTemplateService smsTemplateService;
@Test @Test
public void testCreateSmsChannel_success() { public void testCreateSmsChannel_success() {
// 准备参数 // 准备参数
@ -67,6 +73,7 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
SysSmsChannelUpdateReqVO reqVO = randomPojo(SysSmsChannelUpdateReqVO.class, o -> { SysSmsChannelUpdateReqVO reqVO = randomPojo(SysSmsChannelUpdateReqVO.class, o -> {
o.setId(dbSmsChannel.getId()); // 设置更新的 ID o.setId(dbSmsChannel.getId()); // 设置更新的 ID
o.setStatus(randomCommonStatus()); o.setStatus(randomCommonStatus());
o.setCallbackUrl(randomString());
}); });
// 调用 // 调用
@ -108,6 +115,20 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_NOT_EXISTS); assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_NOT_EXISTS);
} }
@Test
public void testDeleteSmsChannel_hasChildren() {
// mock 数据
SysSmsChannelDO dbSmsChannel = randomSmsChannelDO();
smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsChannel.getId();
// mock 方法
when(smsTemplateService.countByChannelId(eq(id))).thenReturn(10);
// 调用, 并断言异常
assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_HAS_CHILDREN);
}
@Test @Test
public void testGetSmsChannelPage() { public void testGetSmsChannelPage() {
// mock 数据 // mock 数据