修复测试名称重复bug

This commit is contained in:
q4speed 2020-06-03 11:43:29 +08:00
parent 8ba58d1a70
commit a64685abdf
1 changed files with 11 additions and 30 deletions

View File

@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
@ -122,7 +123,16 @@ public class APITestService {
apiTestMapper.updateByPrimaryKeySelective(apiTest);
}
private void checkNameExist(SaveAPITestRequest request) {
ApiTestExample example = new ApiTestExample();
example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()).andIdNotEqualTo(request.getId());
if (apiTestMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("load_test_already_exists"));
}
}
private ApiTestWithBLOBs updateTest(SaveAPITestRequest request) {
checkNameExist(request);
final ApiTestWithBLOBs test = new ApiTestWithBLOBs();
test.setId(request.getId());
test.setName(request.getName());
@ -130,29 +140,12 @@ public class APITestService {
test.setScenarioDefinition(request.getScenarioDefinition());
test.setUpdateTime(System.currentTimeMillis());
test.setStatus(APITestStatus.Saved.name());
checkApiTestExist(test);
apiTestMapper.updateByPrimaryKeySelective(test);
return test;
}
private void checkApiTestExist(ApiTest apiTest) {
if (apiTest.getName() != null) {
ApiTestExample example = new ApiTestExample();
example.createCriteria()
.andNameEqualTo(apiTest.getName());
if (apiTestMapper.selectByExample(example).size() > 0) {
MSException.throwException(Translator.get("api_test_name_already_exists"));
}
}
}
private ApiTestWithBLOBs createTest(SaveAPITestRequest request) {
ApiTestExample example = new ApiTestExample();
example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId());
if (apiTestMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("load_test_already_exists"));
}
checkNameExist(request);
final ApiTestWithBLOBs test = new ApiTestWithBLOBs();
test.setId(request.getId());
test.setName(request.getName());
@ -162,22 +155,10 @@ public class APITestService {
test.setUpdateTime(System.currentTimeMillis());
test.setStatus(APITestStatus.Saved.name());
test.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
checkApiTestPlanExist(test);
apiTestMapper.insert(test);
return test;
}
private void checkApiTestPlanExist(ApiTest apiTest) {
if (apiTest.getName() != null) {
ApiTestExample example = new ApiTestExample();
example.createCriteria()
.andNameEqualTo(apiTest.getName());
if (apiTestMapper.selectByExample(example).size() > 0) {
MSException.throwException(Translator.get("api_test_name_already_exists"));
}
}
}
private void saveFile(String testId, List<MultipartFile> files) {
files.forEach(file -> {
final FileMetadata fileMetadata = fileService.saveFile(file);