• notice
  • Congratulations on the launch of the Sought Tech site

MongoDB implementation review list

Mongodb is very suitable for this.The api call only uses the entry-level CRUD, and the idea is clarified, and the coding will be smooth, so you will find that I said more than coding in this blog.

Expected features of the review list

Just like StackOverFlow, users can send their own questions and other users can answer them.At the same time, the host can reply to other people's comments, and others can still reply to the host

data structure

Mongodb can store documents, in fact, what we have to do is to build a suitable class, and the comment help will be more than half of the success.

The question/comment entity is as 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; // 分组的标记
}

Ideas

In the Answer entity, a collection is not added to store Answer type entities.If you add this collection, the idea is really good.In the reply, there are other people's replies to yourself, a natural tree structure, but considering the front-end The rendering is more difficult, and this solution is abandoned

The entity class of the question maintains a collection of answer entity classes.All the answer instances for the host’s question are placed in this collection, including the host’s replies to the answerer of the question, and the answer from the answerer to the question.

So there are only two layers in this way, and all the answers to this question are maintained in one question.The difficulty of front-end rendering is greatly reduced, but then something happened.

When a user queries the details of a problem, how does the backend handle it?

When the user queries the details of a question, the backend takes the id of the question, takes the instance of the question out of the database, and then processes the Answer collection, and the time-sorted collection is grouped according to the way we specify, and then sorted by time

What is the grouping?

At that time, it was grouped by different users.All comments of the same user have been put together by the host's replies to it and others' replies to it, so a field, group (user id I chose), is required to store the grouping.The logo.The instances in the group are sorted by time, so that the overall level is divided.

  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’s comment

If the front end wants to display your own comments and other people's comments on the left and right parts of the page, you need a mark.Since the above is already traversed, it doesn't hurt to add one more judgment.Take the user id and the answer submitted by the front end.UserId comparison, if they are equal, the flag of this comment is marked as true, and the front-end distinguishes according to this flag, so as to give users more permissions, such as deleting their own comments

limitation

If there is no problem like Netease Music, tens of thousands of comments, then it is estimated that it will be useless.Although the use of stream will be fast, but it can't hold the amount, but if the amount is small, it is still acceptable.In fact, it is ideal.The status is that the comments can be obtained in the form of pagination, and it feels authentic.

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy generic lipitor & lt;a href="https://lipiws.top/"& gt;lipitor 40mg sale& lt;/a& gt; lipitor price

Lyaosd

2024-03-08

Leave a Reply

+