对应的前端页面如下,游记模块的评论即可对游记评论,也能回复其他评论。
文档类
找到模块: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; private Long travelId; private String travelTitle; private Long userId; 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
public interface TravelCommentRepository extends MongoRepository<TravelComment, String> { }
|
创建包:com.swx.comment.service
,创建接口:TravelCommentService
TravelCommentServicepublic 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; } }
|
查询评论
接口信息
Service
找到:TravelCommentService,定义查询方法
TravelCommentService
List<TravelComment> findList(Long travelId);
|
找到:TravelCommentServiceImpl,实现上述方法
TravelCommentServiceImpl
@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)); }
|
保存评论
接口信息
Service
找到:TravelCommentService,定义保存方法
TravelCommentService
void save(TravelComment comment);
|
找到:TravelCommentServiceImpl,实现上述方法
TravelCommentServiceImpl
@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(); }
|