The project is continuously updated:
Table of contents
Save system messages to MongoDB
System messages stored in storage-follow
System messages are saved in the database - like the short video
System messages stored in storage - comments and replies
Save system messages to MongoDB
After we integrate mongoDB into Springboot, we need to do a good job at the mapping level.
First create a new object level in the model, which is a new package:
Here we abbreviate as mo
Then create a class that is consistent with our current business object:
package com.imooc.mo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.Date;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document("message")
public class MessageMO {
@Id
private String id;//消息主键id
@Field("fromUserId")
private String fromUserId;//消息来自的用户id
@Field("fromNickId")
private String fromNickId;//消息来自的用户昵称
@Field("fromFace")
private String fromFace;//消息来自的用户头像
@Field("toUserId")
private String toUserId;//消息来自的用户id
@Field("msgType")
private Integer msgType;//消息类型 枚举
@Field("msgContent")
private Map msgContent;//消息内容
@Field("createTime")
private Date createTime;//消息创建时间
}
After we have the object, we must strive to operate on the object, which is our business layer:
Then it will be realized:
The general mapper we used before, here we need to interact with mongoDB, the repository we use here also needs an interface to build, we create a new package in the data layer, and add another:
We can think of MessageRepository as a general Mapper because it integrates many methods
import com.imooc.pojo.Users;
import com.imooc.repository.MessageRepository;
import com.imooc.service.MsgService;
import com.imooc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
@Service
public class MsgServiceImpl implements MsgService {
@Autowired
private MessageRepository messageRepository;
@Autowired
private UserService userService;
@Override
public void createMsg(String fromUserId,
String toUserId,
Integer type,
Map msgContent){
Users fromUser = userService.getUser(fromUserId);
MessageMO messageMO = new MessageMO();
messageMO.setFromUserId(fromUserId);
messageMO.setFromNickId(fromUser.getNickname());
messageMO.setFromFace(fromUser.getFace());
messageMO.setToUserId(toUserId);
messageMO.setMsgType(type);
if(msgContent != null){
messageMO.setMsgContent(msgContent);
}
messageMO.setCreateTime(new Date());
messageRepository.save(messageMO);
}
}
This completes the writing of our business layer.
System messages stored in storage-follow
Next, we need to improve the place of calling. Based on the following five types, we will do a message storage
After our doFollow attention is over, we are going to remind the other user that someone has followed you
//System message: Follow msgService.createMsg(myId,vlogerId, MessageEnum.FOLLOW_YOU.type, null );
The content we focus on here does not need to be displayed, so we only need to pass in a null here
Then we restart it and make a concern,
Refresh and open mongoDB
This is our data
When mongoDB field is null, the data will not exist
System messages are saved in the database - like the short video
When we insert the data, the system also needs to send a message to like the short video
Like the short video, we need to get the cover of the short video
So we need to pass in vlogCover when setting
//系统消息:点赞短视频
Vlog vlog = this.getVlog(vlogId);
Map msgContent = new HashMap();
msgContent.put("vlogId",vlogId);
msgContent.put("vlogCover",vlog.getCover());
msgService.createMsg(userId,
vlog.getVlogerId(),
MessageEnum.LIKE_VLOG.type,
msgContent);
}
private Vlog getVlog(String id){
return vlogMapper.selectByPrimaryKey(id);
}
Reboot, test:
like this video
Then we look at the database
Mainly look at the rear, at this time there is an extra data
System messages stored in storage - comments and replies
Here we can process comments and reply comments together
Here we need to re-extend the public Vlog getVlog(String id) and write to the interface
You can inquire here
//系统消息:评论/回复
Vlog vlog = vlogService.getVlog(commentBO.getVlogId());
Map msgContent = new HashMap();
msgContent.put("vlogId",vlog.getId());
msgContent.put("vlogCover",vlog.getCover());
msgContent.put("commentId",commentId);
msgContent.put("commentContent",commentBO.getContent());
Integer type= MessageEnum.COMMENT_VLOG.type;
if(StringUtils.isNotBlank(commentBO.getFatherCommentId()) && !commentBO.getFatherCommentId().equalsIgnoreCase("0")){
type = MessageEnum.REPLY_YOU.type;
}
msgService.createMsg(commentBO.getCommentUserId(),
commentBO.getVlogerId(),
type,
msgContent);
return commentVO;
Then we restart the test:
Then go to our mongoDB to view:
At this point we can find that the content here is empty for the data of the previous two days
Then we reply to the test:
Refresh again:
This completes the saving of our system messages for comments and replies.