对应的前端页面如下,攻略模块的评论只能对攻略评论,不能回复其他评论。

文档类

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

StrategyComment
/**
* 攻略评论
*/
@Setter
@Getter
@Document("strategy_comment")
@ToString
public class StrategyComment implements Serializable {
@Id
private String id;
private Long strategyId; //攻略(明细)id
private String strategyTitle; //攻略标题
private Long userId; //用户id
private String nickname; //用户名
private String city;
private int level;
private String headImgUrl; //头像
private Date createTime; //创建时间
private String content; //评论内容
private int thumbupnum; //点赞数
private List<Long> thumbuplist = new ArrayList<>();
}

初始化

找到模块:trip-comment-server

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

StrategyCommentRepository
/**
* 攻略评论 Repository
*/
public interface StrategyCommentRepository extends MongoRepository<StrategyComment, String> {
}

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

StrategyCommentService
public interface StrategyCommentService {

}

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

StrategyCommentServiceImpl
@Slf4j
@Service
public class StrategyCommentServiceImpl implements StrategyCommentService {

private final StrategyCommentRepository strategyCommentRepository;
private final MongoTemplate mongoTemplate;

public StrategyCommentServiceImpl(StrategyCommentRepository strategyCommentRepository, MongoTemplate mongoTemplate) {
this.strategyCommentRepository = strategyCommentRepository;
this.mongoTemplate = mongoTemplate;
}
}

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

StrategyCommentController
@RestController
@RequestMapping("/strategies/comments")
public class StrategyCommentController {

private final StrategyCommentService strategyCommentService;

public StrategyCommentController(StrategyCommentService strategyCommentService) {
this.strategyCommentService = strategyCommentService;
}
}

分页查询评论

接口信息

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

查询条件

找到模块:trip-comment-api,创建包:com.swx.comment.qo,包下创建查询类:CommentQuery

CommentQuery
@Getter
@Setter
public class CommentQuery extends QueryObject {
private Long articleId;
}

Service

找到:StrategyCommentService,定义分组查询方法

StrategyCommentService
/**
* 分页查询
* @param qo 分页参数
* @return 评论
*/
Page<StrategyComment> page(CommentQuery qo);

找到:StrategyCommentServiceImpl,实现上述方法

StrategyCommentServiceImpl
/**
* 分页查询
*
* @param qo 分页参数
* @return 评论
*/
@Override
public Page<StrategyComment> page(CommentQuery qo) {
// 拼接查询条件
Criteria criteria = Criteria.where("strategyId").is(qo.getArticleId());
// 创建查询对象,关联条件
Query query = new Query();
query.addCriteria(criteria);
// 统计总数
long total = mongoTemplate.count(query, StrategyComment.class);
if (total == 0) {
return Page.empty();
}
// 设置分页参数
PageRequest request = PageRequest.of(qo.getCurrent() - 1, qo.getSize());
query.skip(request.getOffset()).limit(request.getPageSize());
// 按照时间排序
query.with(Sort.by(Sort.Direction.DESC, "createTime"));
// 查询数据
List<StrategyComment> records = mongoTemplate.find(query, StrategyComment.class);
return new PageImpl<>(records, request, total);
}

Controller

找到:StrategyCommentController,定义分页查询接口

StrategyCommentController
@GetMapping("/query")
public R<Page<StrategyComment>> saveComment(CommentQuery query) {
return R.ok(strategyCommentService.page(query));
}

保存评论

接口信息

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

Service

找到:StrategyCommentService,定义保存方法

StrategyCommentService
/**
* 保存评论
*
* @param comment 攻略评论
*/
void save(StrategyComment comment);

找到:StrategyCommentServiceImpl,实现上述方法

StrategyCommentServiceImpl
/**
* 保存评论
*
* @param comment 攻略评论
*/
@Override
public void save(StrategyComment 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());
// 保存到 mongodb
strategyCommentRepository.save(comment);
}

Controller

找到:StrategyCommentController,定义保存接口

该接口需要登陆才能访问

StrategyCommentController
@RequireLogin
@PostMapping("/save")
public R<?> saveComment(StrategyComment comment) {
strategyCommentService.save(comment);
return R.ok();
}

点赞评论

取消点赞也是该方法

接口信息

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

Service

找到:StrategyCommentService,定义点赞方法

StrategyCommentService
/**
* 点赞和取消点赞
* @param cid 评论ID
*/
void doLike(String cid);

找到:StrategyCommentServiceImpl,实现上述方法

StrategyCommentServiceImpl
/**
* 点赞和取消点赞
*
* @param cid 评论ID
*/
@Override
public void doLike(String cid) {
// 基于 cid 查询评论对象
Optional<StrategyComment> optional = strategyCommentRepository.findById(cid);
if (optional.isPresent()) {
StrategyComment strategyComment = optional.get();
// 获取当前登陆用户对象
LoginUser loginUser = AuthenticationUtil.getLoginUser();
// 判断当前用户是否点赞
if (strategyComment.getThumbuplist().contains(loginUser.getId())) {
// 如果点赞:点赞数-1,将用户 id 从集合中删除
strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
strategyComment.getThumbuplist().remove(loginUser.getId());
} else {
// 如果没点赞:点赞数+1, 将用户 id 添加到集合中
strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
strategyComment.getThumbuplist().add(loginUser.getId());
}
// 重新将对象保存到 mongodb
strategyCommentRepository.save(strategyComment);
}
}

Controller

找到:StrategyCommentController,定义点赞接口

该接口需要登陆才能访问

StrategyCommentController
@RequireLogin
@PostMapping("/likes")
public R<?> likes(String cid) {
strategyCommentService.doLike(cid);
return R.ok();
}