Skip to content

Commit

Permalink
增加留言功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Dec 20, 2019
1 parent 7362ceb commit 938a82f
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.swpu.uchain.blog.controller;

import com.swpu.uchain.blog.form.CreatLeaveMsgForm;
import com.swpu.uchain.blog.service.LeaveMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @author hobo
* @description
*/
@RestController
@RequestMapping("/leaveMsg")
@Api(tags = "留言接口")
@CrossOrigin
public class LeaveMsgController {

@Autowired
private LeaveMessageService messageService;

@ApiOperation("添加新留言")
@PostMapping(name = "添加新留言",value = "/insert")
public Object insertLeaveMsg(CreatLeaveMsgForm form) {
return messageService.insertLeaveMsg(form);
}

@ApiOperation("查看所有留言")
@GetMapping(name = "查看所有留言",value = "/all")
public Object selectAllLeaveMsg(){
return messageService.selectAllLeaveMsg();
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/LeaveMessageMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.swpu.uchain.blog.dao;

import com.swpu.uchain.blog.entity.LeaveMessage;
import com.swpu.uchain.blog.vo.LeaveMsgVO;
import com.swpu.uchain.blog.vo.ReplyVO;

import java.util.List;

public interface LeaveMessageMapper {
int deleteByPrimaryKey(Long leaveMessageId);

int insert(LeaveMessage record);

LeaveMessage selectByPrimaryKey(Long leaveMessageId);

List<LeaveMessage> selectAll();

int updateByPrimaryKey(LeaveMessage record);

List<Long> getAllParentLeaveMsg();

LeaveMsgVO selectByLeaveMsgId(Long leaveMsgId);

List<ReplyVO> selectByPid(Long leaveMessageId);
}
84 changes: 84 additions & 0 deletions src/main/java/com/swpu/uchain/blog/entity/LeaveMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.swpu.uchain.blog.entity;

import java.io.Serializable;

public class LeaveMessage implements Serializable {
private Long leaveMessageId;

private Long userId;

private Long replyUserId;

private Long pid;

private String leaveMsg;

private String creatTime;

private static final long serialVersionUID = 1L;

public Long getLeaveMessageId() {
return leaveMessageId;
}

public void setLeaveMessageId(Long leaveMessageId) {
this.leaveMessageId = leaveMessageId;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public Long getReplyUserId() {
return replyUserId;
}

public void setReplyUserId(Long replyUserId) {
this.replyUserId = replyUserId;
}

public Long getPid() {
return pid;
}

public void setPid(Long pid) {
this.pid = pid;
}

public String getLeaveMsg() {
return leaveMsg;
}

public void setLeaveMsg(String leaveMsg) {
this.leaveMsg = leaveMsg == null ? null : leaveMsg.trim();
}

public String getCreatTime() {
return creatTime;
}

public void setCreatTime(String creatTime) {
this.creatTime = creatTime == null ? null : creatTime.trim();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", leaveMessageId=").append(leaveMessageId);
sb.append(", userId=").append(userId);
sb.append(", replyUserId=").append(replyUserId);
sb.append(", pid=").append(pid);
sb.append(", leaveMsg=").append(leaveMsg);
sb.append(", creatTime=").append(creatTime);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/swpu/uchain/blog/form/CreatLeaveMsgForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.swpu.uchain.blog.form;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;

/**
* @author hobo
* @description
*/
@Data
public class CreatLeaveMsgForm {

@ApiModelProperty("留言内容")
@NotNull(message = "留言内容不能为空")
private String commentMsg;

@ApiModelProperty("被回复人 ")
private Long replyUserId;

@ApiModelProperty("父级留言Id 若为首级评论则为0")
@NotNull(message = "父级留言Id不能为空")
private Long pid;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.swpu.uchain.blog.service;

import com.swpu.uchain.blog.form.CreatLeaveMsgForm;
import com.swpu.uchain.blog.vo.ResultVO;
import lombok.Data;

/**
* @author hobo
* @description
*/
public interface LeaveMessageService {

/**
* 添加留言
* @return
*/
ResultVO insertLeaveMsg(CreatLeaveMsgForm form);

/**
* 展示所有留言
* @return
*/
ResultVO selectAllLeaveMsg();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.swpu.uchain.blog.service.impl;

import com.swpu.uchain.blog.dao.LeaveMessageMapper;
import com.swpu.uchain.blog.entity.LeaveMessage;
import com.swpu.uchain.blog.entity.User;
import com.swpu.uchain.blog.enums.ResultEnum;
import com.swpu.uchain.blog.form.CreatLeaveMsgForm;
import com.swpu.uchain.blog.service.LeaveMessageService;
import com.swpu.uchain.blog.service.UserService;
import com.swpu.uchain.blog.util.ResultVOUtil;
import com.swpu.uchain.blog.util.TimeUtil;
import com.swpu.uchain.blog.vo.LeaveMsgVO;
import com.swpu.uchain.blog.vo.ReplyVO;
import com.swpu.uchain.blog.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author hobo
* @description
*/
@Service
public class LeaveMessageServiceImpl implements LeaveMessageService {

@Autowired
private LeaveMessageMapper messageMapper;

@Autowired
private UserService userService;


@Override
public ResultVO insertLeaveMsg(CreatLeaveMsgForm form) {
LeaveMessage leaveMessage = new LeaveMessage();
BeanUtils.copyProperties(form,leaveMessage);
if (leaveMessage.getReplyUserId()==null){
leaveMessage.setReplyUserId(0L);
}
User user = userService.getCurrentUser();
if (user == null) {
return ResultVOUtil.error(ResultEnum.USER_NOT_LOGIN);
}
leaveMessage.setUserId(user.getUserId());
leaveMessage.setCreatTime(TimeUtil.getNowTime());
if (messageMapper.insert(leaveMessage)==1){
return ResultVOUtil.success();
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO selectAllLeaveMsg() {
List<LeaveMsgVO> result = new ArrayList<>();
List<Long> leaveMsgIdList = messageMapper.getAllParentLeaveMsg();
for (Long leaveMsgId : leaveMsgIdList){
LeaveMsgVO vo = messageMapper.selectByLeaveMsgId(leaveMsgId);
List<ReplyVO> vos = messageMapper.selectByPid(vo.getLeaveMessageId());
List<ReplyVO> reply = new ArrayList<>();
for (ReplyVO replyVO : vos) {
User user = userService.selectByUserId(replyVO.getReplyUserId());
replyVO.setReplyUserName(user.getUsername());
reply.add(replyVO);
}
vo.setReplyVO(reply);
result.add(vo);
}
return ResultVOUtil.success(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public ResultVO login(LoginForm loginForm, HttpServletResponse response) {
final String realToken = jwtTokenUtil.generateToken(userDetails);
response.addHeader(jwtProperties.getTokenName(), realToken);
Map map = new HashMap();
map.put("username",user.getUsername());
map.put("headPortrait",user.getHeadPortrait());
map.put("role", user.getRole());
map.put("token", realToken);
return ResultVOUtil.success(map);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/swpu/uchain/blog/vo/LeaveMsgVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.swpu.uchain.blog.vo;

import lombok.Data;

import java.util.List;

/**
* @author hobo
* @description
*/
@Data
public class LeaveMsgVO {

private Long leaveMessageId;

private Long userId;

private String userName;

private String headPortrait;

private String creatTime;

private String leaveMsg;

private List<ReplyVO> replyVO;
}
62 changes: 62 additions & 0 deletions src/main/resources/mappers/LeaveMessageMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.swpu.uchain.blog.dao.LeaveMessageMapper" >
<resultMap id="BaseResultMap" type="com.swpu.uchain.blog.entity.LeaveMessage" >
<id column="leave_message_id" property="leaveMessageId" jdbcType="BIGINT" />
<result column="user_id" property="userId" jdbcType="BIGINT" />
<result column="reply_user_id" property="replyUserId" jdbcType="BIGINT" />
<result column="pid" property="pid" jdbcType="BIGINT" />
<result column="leave_msg" property="leaveMsg" jdbcType="VARCHAR" />
<result column="creat_time" property="creatTime" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from leave_message
where leave_message_id = #{leaveMessageId,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.swpu.uchain.blog.entity.LeaveMessage" >
insert into leave_message (leave_message_id, user_id, reply_user_id,
pid, leave_msg, creat_time
)
values (#{leaveMessageId,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, #{replyUserId,jdbcType=BIGINT},
#{pid,jdbcType=BIGINT}, #{leaveMsg,jdbcType=VARCHAR}, #{creatTime,jdbcType=VARCHAR}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.swpu.uchain.blog.entity.LeaveMessage" >
update leave_message
set user_id = #{userId,jdbcType=BIGINT},
reply_user_id = #{replyUserId,jdbcType=BIGINT},
pid = #{pid,jdbcType=BIGINT},
leave_msg = #{leaveMsg,jdbcType=VARCHAR},
creat_time = #{creatTime,jdbcType=VARCHAR}
where leave_message_id = #{leaveMessageId,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select leave_message_id, user_id, reply_user_id, pid, leave_msg, creat_time
from leave_message
where leave_message_id = #{leaveMessageId,jdbcType=BIGINT}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select leave_message_id, user_id, reply_user_id, pid, leave_msg, creat_time
from leave_message
</select>
<select id="getAllParentLeaveMsg" resultType="java.lang.Long">
select leave_message_id
from leave_message
where pid = 0
</select>
<select id="selectByLeaveMsgId" resultType="com.swpu.uchain.blog.vo.LeaveMsgVO">
SELECT u.user_id,u.username,u.head_portrait ,lm.leave_message_id,lm.leave_msg,lm.creat_time
FROM leave_message lm
LEFT JOIN user u on c.user_id = u.user_id
where c.comment_id = #{id}
</select>
<select id="selectByPid" resultType="com.swpu.uchain.blog.vo.ReplyVO">
SELECT u.user_id,u.username,u.head_portrait ,lm.leave_message_id as comment_id,
lm.leave_msg as comment_msg,lm.creat_time,lm.reply_user_id
FROM
user u,
leave_message lm
WHERE u.user_id = c.user_id
and c.pid = #{pid}
</select>
</mapper>

0 comments on commit 938a82f

Please sign in to comment.