fix(note):优化笔记内容更新逻辑

- 修改查询笔记内容 UUID 的注释表述
- 当笔记内容为空时,删除对应的 K-V 存储
- 若从无内容更新为有内容,重新生成内容 UUID
- 调用 K-V 服务保存或删除笔记内容
- 更新失败时抛出业务异常以回滚事务
This commit is contained in:
Hanserwei
2025-10-09 16:21:22 +08:00
parent 04196f8e3a
commit d00933caad

View File

@@ -369,24 +369,27 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
LOCAL_CACHE.invalidate(noteId); LOCAL_CACHE.invalidate(noteId);
// 笔记内容更新 // 笔记内容更新
// 查询笔记内容对应的UUID // 查询此篇笔记内容对应的 UUID
NoteDO noteDO1 = this.getById(noteId); NoteDO noteDO1 = this.getById(noteId);
String contentUuid = noteDO1.getContentUuid(); String contentUuid = noteDO1.getContentUuid();
// 笔记内容是否更新成功 // 笔记内容是否更新成功
boolean isUpdateContentSuccess = false; boolean isUpdateContentSuccess = false;
if (StringUtils.isNotBlank(contentUuid)){ if (StringUtils.isBlank(content)) {
// 若笔记内容为空则删除kv存储 // 若笔记内容为空则删除 K-V 存储
isUpdateContentSuccess = keyValueRpcService.deleteNoteContent(contentUuid); isUpdateContentSuccess = keyValueRpcService.deleteNoteContent(contentUuid);
}else { } else {
// 若将无内容的笔记,更新为了有内容的笔记,需要重新生成 UUID // 若将无内容的笔记,更新为了有内容的笔记,需要重新生成 UUID
contentUuid = StringUtils.isBlank(contentUuid) ? UUID.randomUUID().toString() : contentUuid; contentUuid = StringUtils.isBlank(contentUuid) ? UUID.randomUUID().toString() : contentUuid;
// 调用 K-V 更新短文本 // 调用 K-V 更新短文本
isUpdateContentSuccess = keyValueRpcService.saveNoteContent(contentUuid, content); isUpdateContentSuccess = keyValueRpcService.saveNoteContent(contentUuid, content);
} }
if (!isUpdateContentSuccess){
// 如果更新失败,抛出业务异常,回滚事务
if (!isUpdateContentSuccess) {
throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL); throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL);
} }
return Response.success(); return Response.success();
} }