feat(kv): 新增批量查询评论内容功能

- 新增 BatchFindCommentContentReqDTO 用于批量查询请求参数校验
- 新增 FindCommentContentReqDTO 和 FindCommentContentRspDTO 用于查询参数与响应封装
- 在 CommentContentController 中添加 /comment/content/batchFind 接口
- 实现 CommentContentRepository 的批量查询方法
- 在 CommentContentServiceImpl 中完成批量查询逻辑,包括参数解析与数据转换
- 更新 gateApi.http 添加批量查询接口测试用例
This commit is contained in:
2025-11-08 09:54:34 +08:00
parent 29cf889dd7
commit 2b06ca0300
8 changed files with 182 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
package com.hanserwei.hannote.kv.dto.req;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class BatchFindCommentContentReqDTO {
/**
* 笔记 ID
*/
@NotNull(message = "评论 ID 不能为空")
private Long noteId;
@NotEmpty(message = "评论内容 Key 集合")
@Valid // 指定集合中的 DTO 也需要进行参数校验
private List<FindCommentContentReqDTO> commentContentKeys;
}

View File

@@ -0,0 +1,21 @@
package com.hanserwei.hannote.kv.dto.req;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class FindCommentContentReqDTO {
@NotBlank(message = "发布年月不能为空")
private String yearMonth;
@NotBlank(message = "评论正文 ID 不能为空")
private String contentId;
}

View File

@@ -0,0 +1,24 @@
package com.hanserwei.hannote.kv.dto.resp;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class FindCommentContentRspDTO {
/**
* 评论内容 UUID
*/
private String contentId;
/**
* 评论内容
*/
private String content;
}

View File

@@ -4,6 +4,7 @@ import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.kv.biz.service.CommentContentService;
import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
@@ -26,4 +27,10 @@ public class CommentContentController {
return commentContentService.batchAddCommentContent(batchAddCommentContentReqDTO);
}
@PostMapping(value = "/comment/content/batchFind")
@ApiOperationLog(description = "批量查询评论内容")
public Response<?> batchFindCommentContent(@Validated @RequestBody BatchFindCommentContentReqDTO batchFindCommentContentReqDTO) {
return commentContentService.batchFindCommentContent(batchFindCommentContentReqDTO);
}
}

View File

@@ -0,0 +1,23 @@
package com.hanserwei.hannote.kv.biz.domain.repository;
import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentDO;
import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentPrimaryKey;
import org.springframework.data.cassandra.repository.CassandraRepository;
import java.util.List;
import java.util.UUID;
public interface CommentContentRepository extends CassandraRepository<CommentContentDO, CommentContentPrimaryKey> {
/**
* 批量查询评论内容
*
* @param noteId 笔记ID
* @param yearMonths 年月
* @param contentIds 评论 ID列表
* @return 评论内容
*/
List<CommentContentDO> findByPrimaryKeyNoteIdAndPrimaryKeyYearMonthInAndPrimaryKeyContentIdIn(
Long noteId, List<String> yearMonths, List<UUID> contentIds
);
}

View File

@@ -2,8 +2,23 @@ package com.hanserwei.hannote.kv.biz.service;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO;
public interface CommentContentService {
/**
* 批量添加评论内容
*
* @param batchAddCommentContentReqDTO 批量添加评论内容请求参数
* @return 批量添加结果
*/
Response<?> batchAddCommentContent(BatchAddCommentContentReqDTO batchAddCommentContentReqDTO);
/**
* 批量查询评论内容
*
* @param batchFindCommentContentReqDTO 批量查询评论内容请求参数
* @return 批量查询结果
*/
Response<?> batchFindCommentContent(BatchFindCommentContentReqDTO batchFindCommentContentReqDTO);
}

View File

@@ -1,12 +1,19 @@
package com.hanserwei.hannote.kv.biz.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentDO;
import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentPrimaryKey;
import com.hanserwei.hannote.kv.biz.domain.repository.CommentContentRepository;
import com.hanserwei.hannote.kv.biz.service.CommentContentService;
import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.CommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.FindCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.resp.FindCommentContentRspDTO;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Service;
@@ -18,6 +25,8 @@ public class CommentContentServiceImpl implements CommentContentService {
@Resource
private CassandraTemplate cassandraTemplate;
@Resource
private CommentContentRepository commentContentRepository;
@Override
public Response<?> batchAddCommentContent(BatchAddCommentContentReqDTO batchAddCommentContentReqDTO) {
@@ -47,4 +56,39 @@ public class CommentContentServiceImpl implements CommentContentService {
.execute();
return Response.success();
}
@Override
public Response<?> batchFindCommentContent(BatchFindCommentContentReqDTO batchFindCommentContentReqDTO) {
// 归属的笔记ID
Long noteId = batchFindCommentContentReqDTO.getNoteId();
// 查询评论的发布年月、内容 UUID
List<FindCommentContentReqDTO> commentContentKeys = batchFindCommentContentReqDTO.getCommentContentKeys();
// 过滤出年月
List<@NotBlank(message = "发布年月不能为空") String> yearMonths = commentContentKeys.stream()
.map(FindCommentContentReqDTO::getYearMonth)
.distinct()
.toList();
// 过滤出内容 UUID
List<UUID> contentIds = commentContentKeys.stream()
.map(r -> UUID.fromString(r.getContentId()))
.distinct()
.toList();
// 批量查询 Cassandra
List<CommentContentDO> commentContentDOS = commentContentRepository
.findByPrimaryKeyNoteIdAndPrimaryKeyYearMonthInAndPrimaryKeyContentIdIn(noteId, yearMonths, contentIds);
// DO 转 DTO
List<FindCommentContentRspDTO> findCommentContentRspDTOS = Lists.newArrayList();
if (CollUtil.isNotEmpty(commentContentDOS)) {
findCommentContentRspDTOS = commentContentDOS.stream()
.map(commentContentDO -> FindCommentContentRspDTO.builder()
.contentId(String.valueOf(commentContentDO.getPrimaryKey().getContentId()))
.content(commentContentDO.getContent())
.build())
.toList();
}
return Response.success(findCommentContentRspDTOS);
}
}

View File

@@ -329,3 +329,21 @@ Content-Type: application/json
}
]
}
### 批量查询评论内容
POST http://localhost:8084/kv/comment/content/batchFind
Content-Type: application/json
{
"noteId": 1862481582414102549,
"commentContentKeys": [
{
"yearMonth": "2025-11",
"contentId": "a2537301-dc80-4fd6-ab2f-a9a908baebba"
},
{
"yearMonth": "2025-11",
"contentId": "a66b06ce-bf45-4a21-a5f2-31e5fb5fb5eb"
}
]
}