Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#2556 from wangyu096/issue_2554
Browse files Browse the repository at this point in the history
fix: 编辑文件源报错报错 404 TencentBlueKing#2554
  • Loading branch information
jsonwan authored Oct 26, 2023
2 parents 715b2f7 + ca07906 commit fada442
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Response<FileSourceVO> saveFileSource(
);

@ApiOperation(value = "更新文件源", produces = "application/json")
@PutMapping("")
@PutMapping("/{id}")
Response<FileSourceVO> updateFileSource(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -112,6 +112,9 @@ Response<FileSourceVO> updateFileSource(
@ApiParam(value = "资源范围ID", required = true)
@PathVariable(value = "scopeId")
String scopeId,
@ApiParam(value = "文件源 ID", required = true)
@PathVariable("id")
Integer id,
@ApiParam(value = "更新文件源请求")
@RequestBody
FileSourceCreateUpdateReq fileSourceCreateUpdateReq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
@ApiModel("文件源创建、更新请求")
public class FileSourceCreateUpdateReq {

@Deprecated
@ApiModelProperty(value = "文件源 ID", hidden = true)
private Integer id;
/**
* 文件源Code
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.tencent.bk.audit.annotations.AuditEntry;
import com.tencent.bk.audit.annotations.AuditRequestBody;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.exception.ServiceException;
import com.tencent.bk.job.common.iam.constant.ActionId;
Expand Down Expand Up @@ -80,34 +79,8 @@ private void checkCodeBlank(String code) {
}
}

private void confirmIdExists(Integer id) {
if (id == null || id <= 0) {
throw new InvalidParamException(ErrorCode.MISSING_OR_ILLEGAL_PARAM_WITH_PARAM_NAME, new String[]{"id"});
}
}

private void checkParam(Long appId, FileSourceCreateUpdateReq fileSourceCreateUpdateReq, boolean forCreate) {
Integer id = fileSourceCreateUpdateReq.getId();
String code = fileSourceCreateUpdateReq.getCode();
checkCodeBlank(code);
if (forCreate) {
// 创建
if (fileSourceService.existsCode(appId, code)) {
throw new FailedPreconditionException(
ErrorCode.FILE_SOURCE_CODE_ALREADY_EXISTS,
new String[]{code}
);
}
} else {
// 更新
confirmIdExists(id);
if (fileSourceService.existsCodeExceptId(appId, code, id)) {
throw new FailedPreconditionException(
ErrorCode.FILE_SOURCE_CODE_ALREADY_EXISTS,
new String[]{code}
);
}
}
private void checkParam(FileSourceCreateUpdateReq fileSourceCreateUpdateReq) {
checkCodeBlank(fileSourceCreateUpdateReq.getCode());
if (StringUtils.isBlank(fileSourceCreateUpdateReq.getCredentialId())) {
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM_WITH_PARAM_NAME, new String[]{"credentialId"});
}
Expand All @@ -134,8 +107,9 @@ public Response<FileSourceVO> saveFileSource(
@AuditRequestBody FileSourceCreateUpdateReq fileSourceCreateUpdateReq) {
try {
Long appId = appResourceScope.getAppId();
checkParam(appId, fileSourceCreateUpdateReq, true);
FileSourceDTO fileSourceDTO = buildFileSourceDTO(username, appId, fileSourceCreateUpdateReq);
checkParam(fileSourceCreateUpdateReq);
FileSourceDTO fileSourceDTO = buildFileSourceDTO(username, appId, null,
fileSourceCreateUpdateReq);
FileSourceDTO createdFileSource = fileSourceService.saveFileSource(username, appId, fileSourceDTO);
boolean registerResult = fileSourceAuthService.registerFileSource(
username, createdFileSource.getId(), fileSourceDTO.getAlias());
Expand All @@ -156,11 +130,12 @@ public Response<FileSourceVO> updateFileSource(
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
Integer id,
@AuditRequestBody FileSourceCreateUpdateReq fileSourceCreateUpdateReq) {
Long appId = appResourceScope.getAppId();
log.info("Input=({},{},{})", username, appId, fileSourceCreateUpdateReq);
FileSourceDTO fileSourceDTO = buildFileSourceDTO(username, appId, fileSourceCreateUpdateReq);
checkParam(appId, fileSourceCreateUpdateReq, false);
FileSourceDTO fileSourceDTO = buildFileSourceDTO(username, appId, id, fileSourceCreateUpdateReq);
checkParam(fileSourceCreateUpdateReq);

FileSourceDTO updateFileSource = fileSourceService.updateFileSourceById(username, appId, fileSourceDTO);
return Response.buildSuccessResp(FileSourceDTO.toVO(updateFileSource));
Expand Down Expand Up @@ -263,11 +238,11 @@ public Response<List<FileSourceStaticParam>> getFileSourceParams(String username
return Response.buildSuccessResp(fileSourceService.getFileSourceParams(appId, fileSourceTypeCode));
}

private FileSourceDTO buildFileSourceDTO(String username, Long appId,
private FileSourceDTO buildFileSourceDTO(String username, Long appId, Integer fileSourceId,
FileSourceCreateUpdateReq fileSourceCreateUpdateReq) {
FileSourceDTO fileSourceDTO = new FileSourceDTO();
fileSourceDTO.setAppId(appId);
fileSourceDTO.setId(fileSourceCreateUpdateReq.getId());
fileSourceDTO.setId(fileSourceId);
fileSourceDTO.setCode(fileSourceCreateUpdateReq.getCode());
fileSourceDTO.setAlias(fileSourceCreateUpdateReq.getAlias());
fileSourceDTO.setStatus(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.tencent.bk.job.common.audit.constants.EventContentConstants;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.AlreadyExistsException;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
import com.tencent.bk.job.common.exception.NotFoundException;
import com.tencent.bk.job.common.iam.constant.ActionId;
import com.tencent.bk.job.common.iam.constant.ResourceTypeId;
Expand Down Expand Up @@ -141,10 +142,18 @@ public List<FileSourceDTO> listWorkTableFileSource(List<Long> appIdList, List<In
public FileSourceDTO saveFileSource(String username, Long appId, FileSourceDTO fileSource) {
authCreate(username, appId);

if (existsCode(appId, fileSource.getCode())) {
throw new FailedPreconditionException(
ErrorCode.FILE_SOURCE_CODE_ALREADY_EXISTS,
new String[]{fileSource.getCode()}
);
}

if (fileSourceDAO.checkFileSourceExists(fileSource.getAppId(), fileSource.getAlias())) {
throw new AlreadyExistsException(ErrorCode.FILE_SOURCE_ALIAS_ALREADY_EXISTS,
new String[]{fileSource.getAlias()});
}

Integer id = fileSourceDAO.insertFileSource(fileSource);
fileSource.setId(id);

Expand Down Expand Up @@ -183,6 +192,13 @@ private void authManage(String username, long appId, int fileSourceId) {
public FileSourceDTO updateFileSourceById(String username, Long appId, FileSourceDTO fileSource) {
authManage(username, appId, fileSource.getId());

if (existsCodeExceptId(appId, fileSource.getCode(), fileSource.getId())) {
throw new FailedPreconditionException(
ErrorCode.FILE_SOURCE_CODE_ALREADY_EXISTS,
new String[]{fileSource.getCode()}
);
}

FileSourceDTO originFileSource = getFileSourceById(fileSource.getId());
if (originFileSource == null) {
throw new NotFoundException(ErrorCode.FILE_SOURCE_NOT_EXIST);
Expand Down

0 comments on commit fada442

Please sign in to comment.