Mongodb very suitable to do this, call the api is simply to use entry-level CRUD, straightened ideas, coding will be so smooth, so you will find that I say in this blog has more than coding. |
Comments standings intended function
StackOverFlow like it, users can send their questions to other users to answer, but the landlord can reply to other people's comments, others can still respond to landlord
data structure
mongodb can store documents ah, in fact, we have to do it is to build a proper class, to help it review the success of more than half of the
Questions / Comments entity follows
problem
Problem class the implements Serializable {public the @Id Private String _id; // Key Private String the Nickname; // user name private String avator; // User Avatar private String userId; // username the above mentioned id Private String title; // title private String content ; // content private boolean answered; // already answered whether private Date createTime; // created private boolean flag = false; // mark is whether I, a non-default I private list <answer> answerList; // to answer a list of questions }
comment
Answer class {public Private String the above mentioned id; // uniquely identifies the current answer private String nickname; // user name private String avator; // User Avatar private Integer userId; id // answer the user's private String Content; // answer SUMMARY Private a Date Time; Private Boolean in flag = false; I // default is not false. Private Group Integer; // tag packet }
Thinking
Answer entity, does not add a collection used to store Answer types of entities, if you add on this collection then, does the idea of good, there are other people reply to their reply, a natural tree, but considering the front end increase the difficulty of rendering, abandoned this scheme
Entity class of problems in the maintenance of a set of entity classes to answer, answer all instances the landlord for all problems in this set, including the landlord responses to questions respondents, respondents also included responses to questions
So this way there is only two, a question maintains all of the responses to this question, the difficulty is greatly reduced front-end rendering, but then something has come up
when a user queries the details of the problem, how back-end processing
When a user queries the details of a problem, the problem of holding back end id, the database instance to the problem will be taken out, followed by treatment Answer collection, we will be grouped according to the manner specified in accordance with a set of time-ordered, then the time to sort
What grouped by it?
当时是按照不同的用户分组, 同一个用户的全部评论,已经楼主对它的回复,以及别人对它的回复都放在一起, 所以需要一个字段,group(我选的用户id), 专门存储分组的标志. 组内的实例再按照时间排序,这样整体的层次就划分好了
public JsonResult problemDetail(@PathVariable String problemId){ Optional<Problem> byId = problemRepository.findById(problemId); if (!byId.isPresent()){ return JsonResult.fail("您没有获取到详情页,请联系管理员"); } Problem problem = byId.get(); if (problem.getAnswerList().size()>0){ Map<Integer, List<Answer>> collect = problem.getAnswerList() .stream().collect(Collectors.groupingBy(Answer::getGroup)); ArrayList<Answer> list = new ArrayList<>(); collect.forEach((k,v)->{ list.addAll(v); }); problem.setAnswerList(list); } return JsonResult.ok("返回详情页"+problem); }
定位出当前用户的评论
如果前端想在页面的分左右两部分展示自己的评论和别人的评论,就需要一个标记,既然上面都已经在遍历了,多加一个判断也无妨, 拿着前端提交过来的用户id和Answer中的userId比对, 如果相等,就把这个评论的flag标记为true, 前端根据这个标记区分, 从而给用户更多的权限,比如删除自己的评论
局限性
如果没个问题都像网易音乐那种,上万条评论,这样的话,估计就废了,虽然使用stream会快,但是也扛不住量啊, 但是数量小的话,还是可以接受的, 其实理想的状态是评论可以以分页的形式获取出来, 感觉才正宗。
郑州不孕不育医院:http://yyk.39.net/zz3/zonghe/1d427.html