对应的前端页面如下,游记模块的评论即可对游记评论,也能回复其他评论。

文档类

找到模块:trip-comment-api,找到包com.swx.comment.domain,创建游记评论文档实体类

TravelComment
/**
* 游记评论
*/
@Setter
@Getter
@Document("travel_comment")
public class TravelComment implements Serializable {
public static final int TRAVLE_COMMENT_TYPE_COMMENT = 0; //普通评论
public static final int TRAVLE_COMMENT_TYPE = 1; //评论的评论
@Id
private String id; //id
private Long travelId; //游记id
private String travelTitle; //游记标题
private Long userId; //用户id
private String nickname; //用户名
private String city;
private Integer level;
private String headImgUrl; // 用户头像
private Integer type; //评论类别
private Date createTime; //创建时间
private String content; //评论内容
private TravelComment refComment; //关联的评论
}

初始化

找到模块:trip-comment-server

创建包:com.swx.comment.repository,创建接口:TravelCommentRepository

TravelCommentRepository
/**
* 游记评论 Repository
*/
public interface TravelCommentRepository extends MongoRepository<TravelComment, String> {
}

创建包:com.swx.comment.service,创建接口:TravelCommentService

TravelCommentService
public interface TravelCommentService {

}

创建包:com.swx.comment.service.impl,创建类:TravelCommentServiceImpl

TravelCommentServiceImpl
@Slf4j
@Service
public class TravelCommentServiceImpl implements TravelCommentService {

private final TravelCommentRepository travelCommentRepository;
private final MongoTemplate mongoTemplate;

public TravelCommentServiceImpl(TravelCommentRepository travelCommentRepository, MongoTemplate mongoTemplate) {
this.travelCommentRepository = travelCommentRepository;
this.mongoTemplate = mongoTemplate;
}
}

创建包:com.swx.comment.controller,创建类:TravelCommentController

TravelCommentController
@RestController
@RequestMapping("/travels/comments")
public class TravelCommentController {

private final TravelCommentService travelCommentService;

public TravelCommentController(TravelCommentService travelCommentService) {
this.travelCommentService = travelCommentService;
}
}

查询评论

接口信息

路径地址 http://localhost:9000/comment/travels/comments/query
请求方式 GET
请求参数 travelId
返回结果 R { code: “”, msg: “”, data:List }

Service

找到:TravelCommentService,定义查询方法

TravelCommentService
/**
* 获取游记的评论
* @param travelId 游记ID
* @return 所有评论
*/
List<TravelComment> findList(Long travelId);

找到:TravelCommentServiceImpl,实现上述方法

TravelCommentServiceImpl
/**
* 获取游记的评论
*
* @param travelId 游记ID
* @return 所有评论
*/
@Override
public List<TravelComment> findList(Long travelId) {
Query query = new Query()
.with(Sort.by(Sort.Direction.DESC, "createTime"))
.addCriteria(Criteria.where("travelId").is(travelId));
List<TravelComment> travelComments = mongoTemplate.find(query, TravelComment.class);
for (TravelComment travelComment : travelComments) {
TravelComment refComment = travelComment.getRefComment();
if (refComment != null && refComment.getId() != null) {
Optional<TravelComment> refCommentOptional = travelCommentRepository.findById(refComment.getId());
travelComment.setRefComment(refCommentOptional.orElse(null));
}
}
return travelComments;
}

Controller

找到:TravelCommentController,定义查询接口

TravelCommentController
@GetMapping("/query")
public R<List<TravelComment>> findList(Long travelId) {
return R.ok(travelCommentService.findList(travelId));
}

保存评论

接口信息

路径地址 http://localhost:9000/comment/travels/comments/save
请求方式 GET
请求参数 TravelComment
返回结果 R { code: “”, msg: “”, data: }
Service

找到:TravelCommentService,定义保存方法

TravelCommentService
/**
* 保存游记评论
* @param comment 评论信息
*/
void save(TravelComment comment);

找到:TravelCommentServiceImpl,实现上述方法

TravelCommentServiceImpl
/**
* 保存游记评论
*
* @param comment 评论信息
*/
@Override
public void save(TravelComment comment) {
// 获取当前登陆用户
LoginUser loginUser = AuthenticationUtil.getLoginUser();
if (loginUser == null) {
log.error("[游记评论模块] 获取登陆用户信息错误");
throw new BizException("获取登陆用户信息错误");
}
comment.setUserId(loginUser.getId());
comment.setNickname(loginUser.getNickname());
comment.setCity(loginUser.getCity());
comment.setLevel(loginUser.getLevel());
comment.setHeadImgUrl(loginUser.getHeadImgUrl());
comment.setCreateTime(new Date());
if (comment.getRefComment() != null && StringUtils.hasLength(comment.getRefComment().getId())) {
// 评论的评论
comment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
} else {
// 普通评论
comment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
}
travelCommentRepository.save(comment);
}

Controller

找到:TravelCommentController,定义保存接口

该接口需要登陆才能访问

TravelCommentController
@RequireLogin
@PostMapping("/save")
public R<?> queryComment(TravelComment comment) {
travelCommentService.save(comment);
return R.ok();
}