MongoDB achieve comment standings

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

public class Problem implements Serializable {
@Id
private String _id;// key
private String nickname; // 用户名
private String avator; // 用户头像

private String userId; // 户名id
private String title ; // 标题
private String content ;  // 内容
private boolean answered ; // 是否已经回答
private Date createTime ; // 创建时间
private boolean flag =false; // 标记是否是本人,默认是非本人
private List<Answer> answerList; // 问题的回答列表
}

comment

public class Answer {

    private String id;// 当前回答的唯一标识
    private String nickname; // 用户名
    private String avator; // 用户头像

    private  Integer userId; // 回答的用户的id
    private  String Content ; // 回答的内容
    private  Date time;
    private  boolean flag = false;// 默认false.不是本人
    private  Integer group; // 分组的标记
}

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 later it happened to

When a user issues a query details of 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, grouping according to our manner specified in accordance with a set of time-ordered, then the time to sort

What grouped by it?

Was according to different user groups, all with a user's comments, the landlord has its reply, reply to it and others are put together, so they need a field, group (I chose the user id), specialized storage group the flag instances within the group and then in chronological order, so the overall level of good will divide

  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);
}

Locate the current user reviews

If you want the front to show their reviews and comments of others in the two parts of the sub-page or so, you need a mark, since the above are already traversed, one more judge it anyway, holding up the front of the user id and submit the Answer userId comparison, if they are equal, put the flag comments marked as true, according to the mark to distinguish the front end, giving users more privileges, such as delete your comment

limitation

If no problem is like Netease music that, thousands of comments, this is the case, estimated to waste, although the use of stream will be faster, but also could not carry the amount ah, but a small number, it is acceptable, in fact, the ideal the state can get out of page commentary in the form of feeling it was authentic.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160305.htm