保存陌生人评论问题信息 功能 参考代码     DATE: 2024-04-27 22:42:05

保存陌生人评论问题信息 功能 参考代码


controller



/

**

* 设置陌生人问题

*

* @return

*/

@PostMapping("questions")

public ResponseEntity<Void> saveQuestions(@RequestBody Map<String,保存 String> param) {

try {

String content = param.get("content");

this.myCenterService.saveQuestions(content);

return ResponseEntity.ok(null);

} catch (Exception e) {

e.printStackTrace();

}

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

}





=========================================================





service 获取controller传递请求线程 获取其中用户ID




public void saveQuestions(String content) {

User user = UserThreadLocal.get();

this.questionService.save(user.getId(), content);

}





==================================================



service 核心逻辑处理 问题service





public void save(Long userId, String content) {

Question question = this.queryQuestion(userId);

if(null != question){

question.setTxt(content);

this.questionMapper.updateById(question);

}else {

question = new Question();

question.setUserId(userId);

question.setTxt(content);

question.setCreated(new Date());

question.setUpdated(question.getCreated());

this.questionMapper.insert(question);

}

}