对应的前端页面如下,攻略模块的评论只能对攻略评论,不能回复其他评论。
文档类
找到模块: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; private String strategyTitle; private Long userId; 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
public interface StrategyCommentRepository extends MongoRepository<StrategyComment, String> { }
|
创建包:com.swx.comment.service
,创建接口:StrategyCommentService
StrategyCommentServicepublic 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; } }
|
分页查询评论
接口信息
查询条件
找到模块:trip-comment-api,创建包:com.swx.comment.qo
,包下创建查询类:CommentQuery
CommentQuery@Getter @Setter public class CommentQuery extends QueryObject { private Long articleId; }
|
Service
找到:StrategyCommentService,定义分组查询方法
StrategyCommentService
Page<StrategyComment> page(CommentQuery qo);
|
找到:StrategyCommentServiceImpl,实现上述方法
StrategyCommentServiceImpl
@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)); }
|
保存评论
接口信息
Service
找到:StrategyCommentService,定义保存方法
StrategyCommentService
void save(StrategyComment comment);
|
找到:StrategyCommentServiceImpl,实现上述方法
StrategyCommentServiceImpl
@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()); strategyCommentRepository.save(comment); }
|
Controller
找到:StrategyCommentController,定义保存接口
该接口需要登陆才能访问
StrategyCommentController@RequireLogin @PostMapping("/save") public R<?> saveComment(StrategyComment comment) { strategyCommentService.save(comment); return R.ok(); }
|
点赞评论
取消点赞也是该方法
接口信息
Service
找到:StrategyCommentService,定义点赞方法
找到:StrategyCommentServiceImpl,实现上述方法
StrategyCommentServiceImpl
@Override public void doLike(String cid) { Optional<StrategyComment> optional = strategyCommentRepository.findById(cid); if (optional.isPresent()) { StrategyComment strategyComment = optional.get(); LoginUser loginUser = AuthenticationUtil.getLoginUser(); if (strategyComment.getThumbuplist().contains(loginUser.getId())) { strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1); strategyComment.getThumbuplist().remove(loginUser.getId()); } else { strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1); strategyComment.getThumbuplist().add(loginUser.getId()); } strategyCommentRepository.save(strategyComment); } }
|
Controller
找到:StrategyCommentController,定义点赞接口
该接口需要登陆才能访问
StrategyCommentController@RequireLogin @PostMapping("/likes") public R<?> likes(String cid) { strategyCommentService.doLike(cid); return R.ok(); }
|