目录
一、资讯详情页
//NewController
@RequestMapping(path = {"/news/{newsId}"}, method = {RequestMethod.GET})
public String newsDetail(@PathVariable("newsId") int newsId, Model model) {
News news = newsService.getById(newsId);
if (news != null) {
int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
if (localUserId != 0) {
model.addAttribute("like", likeService.getLikeStatus(localUserId, EntityType.ENTITY_NEWS, news.getId()));
} else {
model.addAttribute("like", 0);
}
// 评论
List<Comment> comments = commentService.getCommentsByEntity(news.getId(), EntityType.ENTITY_NEWS);
List<ViewObject> commentVOs = new ArrayList<ViewObject>();
for (Comment comment : comments) {
ViewObject vo = new ViewObject();
vo.set("comment", comment);
vo.set("user", userService.getUser(comment.getUserId()));
commentVOs.add(vo);
}
model.addAttribute("comments", commentVOs);
}
model.addAttribute("news", news);
model.addAttribute("owner", userService.getUser(news.getUserId()));
return "detail";
}
二、评论中心
//CommentService
@Service
public class CommentService {
@Autowired
private CommentDAO commentDAO;
public List<Comment> getCommentsByEntity(int entityId, int entityType) {
return commentDAO.selectByEntity(entityId, entityType);
}
public int addComment(Comment comment) {
return commentDAO.addComment(comment);
}
public int getCommentCount(int entityId, int entityType) {
return commentDAO.getCommentCount(entityId, entityType);
}
public void deleteComment(int entityId, int entityType) {
commentDAO.updateStatus(entityId, entityType, 1);
}
}
//NewsController
@RequestMapping(path = {"/news/{newsId}"}, method = {RequestMethod.GET})
public String newsDetail(@PathVariable("newsId") int newsId, Model model) {
News news = newsService.getById(newsId);
if (news != null) {
int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
if (localUserId != 0) {
model.addAttribute("like", likeService.getLikeStatus(localUserId, EntityType.ENTITY_NEWS, news.getId()));
} else {
model.addAttribute("like", 0);
}
// 评论
List<Comment> comments = commentService.getCommentsByEntity(news.getId(), EntityType.ENTITY_NEWS);
List<ViewObject> commentVOs = new ArrayList<ViewObject>();
for (Comment comment : comments) {
ViewObject vo = new ViewObject();
vo.set("comment", comment);
vo.set("user", userService.getUser(comment.getUserId()));
commentVOs.add(vo);
}
model.addAttribute("comments", commentVOs);
}
model.addAttribute("news", news);
model.addAttribute("owner", userService.getUser(news.getUserId()));
return "detail";
}
@RequestMapping(path = {"/addComment"}, method = {RequestMethod.POST})
public String addComment(@RequestParam("newsId") int newsId,
@RequestParam("content") String content) {
try {
content = HtmlUtils.htmlEscape(content);
// 过滤content
Comment comment = new Comment();
comment.setUserId(hostHolder.getUser().getId());
comment.setContent(content);
comment.setEntityId(newsId);
comment.setEntityType(EntityType.ENTITY_NEWS);
comment.setCreatedDate(new Date());
comment.setStatus(0);
commentService.addComment(comment);
// 更新news里的评论数量
int count = commentService.getCommentCount(comment.getEntityId(), comment.getEntityType());
newsService.updateCommentCount(comment.getEntityId(), count);
// 怎么异步化
} catch (Exception e) {
logger.error("增加评论失败" + e.getMessage());
}
return "redirect:/news/" + String.valueOf(newsId);
}
三、消息中心
//MessageController
@RequestMapping(path = {"/msg/addMessage"}, method = {RequestMethod.POST})
@ResponseBody
public String addMessage(@RequestParam("fromId") int fromId,
@RequestParam("toId") int toId,
@RequestParam("content") String content) {
try {
Message msg = new Message();
msg.setContent(content);
msg.setFromId(fromId);
msg.setToId(toId);
msg.setCreatedDate(new Date());
msg.setConversationId(fromId < toId ? String.format("%d_%d", fromId, toId) : String.format("%d_%d", toId, fromId));
messageService.addMessage(msg);
return ToutiaoUtil.getJSONString(msg.getId());
} catch (Exception e) {
logger.error("增加评论失败" + e.getMessage());
return ToutiaoUtil.getJSONString(1, "插入评论失败");
}
}
}
//获取两个用户对话的详情
@RequestMapping(path = {"/msg/detail"}, method = {RequestMethod.GET})
public String conversationDetail(Model model, @Param("conversationId") String conversationId) {
try {
List<Message> conversationList = messageService.getConversationDetail(conversationId, 0, 10);
List<ViewObject> messages = new ArrayList<>();
for (Message msg : conversationList) {
ViewObject vo = new ViewObject();
vo.set("message", msg);
User user = userService.getUser(msg.getFromId());
if (user == null) {
continue;
}
vo.set("headUrl", user.getHeadUrl());
vo.set("userId", user.getId());
messages.add(vo);
}
model.addAttribute("messages", messages);
} catch (Exception e) {
logger.error("获取详情消息失败" + e.getMessage());
}
return "letterDetail";
}
//获取当前用户所有的私信列表
@RequestMapping(path = {"/msg/list"}, method = {RequestMethod.GET})
public String conversationDetail(Model model) {
try {
int localUserId = hostHolder.getUser().getId();
List<ViewObject> conversations = new ArrayList<ViewObject>();
List<Message> conversationList = messageService.getConversationList(localUserId, 0, 10);
for (Message msg : conversationList) {
ViewObject vo = new ViewObject();
vo.set("conversation", msg);
int targetId = msg.getFromId() == localUserId ? msg.getToId() : msg.getFromId();
User user = userService.getUser(targetId);
vo.set("user", user);
vo.set("unread", messageService.getConvesationUnreadCount(localUserId, msg.getConversationId()));
conversations.add(vo);
}
model.addAttribute("conversations", conversations);
} catch (Exception e) {
logger.error("获取站内信列表失败" + e.getMessage());
}
return "letter";
}
//MessageDAO
String TABLE_NAME = " message ";
String INSERT_FIELDS = " from_id, to_id, content, has_read, conversation_id, created_date ";
String SELECT_FIELDS = " id, " + INSERT_FIELDS;
@Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,
") values (#{fromId},#{toId},#{content},#{hasRead},#{conversationId},#{createdDate})"})
int addMessage(Message message);
@Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where conversation_id=#{conversationId} order by id desc limit #{offset}, #{limit}"})
List<Message> getConversationDetail(@Param("conversationId") String conversationId,
@Param("offset") int offset, @Param("limit") int limit);
@Select({"select count(id) from ", TABLE_NAME, " where has_read=0 and to_id=#{userId} and conversation_id=#{conversationId}"})
int getConvesationUnreadCount(@Param("userId") int userId, @Param("conversationId") String conversationId);
@Select({"select ", INSERT_FIELDS, " ,count(id) as id from ( select * from ", TABLE_NAME, " where from_id=#{userId} or to_id=#{userId} order by id desc) tt group by conversation_id order by created_date desc limit #{offset}, #{limit}"})
List<Message> getConversationList(@Param("userId") int userId,
@Param("offset") int offset, @Param("limit") int limit);
项目视频中并没有提及怎么实现对已读信息数量的更新(has_read字段)。