Skip to content

Commit 7c3536d

Browse files
committed
fix message board display error bug
1 parent ac5bfc5 commit 7c3536d

File tree

15 files changed

+328
-208
lines changed

15 files changed

+328
-208
lines changed

dimple-modules/dimple-blog-front/dimple-blog-front-service/src/main/java/com/dimple/blog/front/service/entity/BlogComment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class BlogComment extends BaseEntity {
3030
*/
3131
private Long parentId;
3232

33+
private Long replyId;
34+
35+
private Long likeCount;
36+
3337
/**
3438
* user head image
3539
*/

dimple-modules/dimple-blog-front/dimple-blog-front-service/src/main/java/com/dimple/blog/front/service/mapper/BlogCommentMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ public interface BlogCommentMapper {
3636
*/
3737
List<BlogComment> selectBlogCommentList(BlogComment blogComment);
3838

39+
List<BlogComment> selectBlogCommentByParentIds(List<Long> ids);
40+
41+
int addCommentLikeCount(Long id);
3942
}

dimple-modules/dimple-blog-front/dimple-blog-front-service/src/main/java/com/dimple/blog/front/service/service/BlogCommentService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public interface BlogCommentService {
2727
*/
2828
List<BlogCommentBO> selectBlogCommentList(BlogCommentBO blogComment);
2929

30+
31+
List<BlogCommentBO> selectBlogCommentListWithSub(BlogCommentBO blogComment);
32+
33+
3034
/**
3135
* 新增
3236
*
@@ -35,4 +39,5 @@ public interface BlogCommentService {
3539
*/
3640
int insertBlogComment(BlogCommentBO blogComment);
3741

42+
int addBlogCommentLikeCount(Long id);
3843
}

dimple-modules/dimple-blog-front/dimple-blog-front-service/src/main/java/com/dimple/blog/front/service/service/bo/BlogCommentBO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.dimple.common.core.web.entity.BaseEntity;
55
import lombok.Data;
66

7+
import java.util.List;
8+
79

810
/**
911
* 对象 blog_comment
@@ -53,4 +55,12 @@ public class BlogCommentBO extends BaseEntity {
5355
*/
5456
@Excel(name = "邮件")
5557
private String email;
58+
59+
private Long replyId;
60+
61+
private String replyUsername;
62+
63+
private Long likeCount;
64+
65+
private List<BlogCommentBO> subComments;
5666
}

dimple-modules/dimple-blog-front/dimple-blog-front-service/src/main/java/com/dimple/blog/front/service/service/impl/BlogCommentServiceImpl.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dimple.blog.front.service.service.impl;
22

3+
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
34
import com.dimple.blog.front.service.entity.BlogComment;
45
import com.dimple.blog.front.service.mapper.BlogCommentMapper;
56
import com.dimple.blog.front.service.service.BlogArticleService;
@@ -8,12 +9,14 @@
89
import com.dimple.blog.front.service.service.bo.BlogCommentBO;
910
import com.dimple.common.core.utils.DateUtils;
1011
import com.dimple.common.core.utils.bean.BeanMapper;
12+
import org.apache.commons.collections4.CollectionUtils;
1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.stereotype.Service;
1315

1416
import java.util.Arrays;
1517
import java.util.List;
1618
import java.util.Map;
19+
import java.util.Objects;
1720
import java.util.stream.Collectors;
1821

1922
/**
@@ -45,6 +48,33 @@ public List<BlogCommentBO> selectBlogCommentList(BlogCommentBO blogCommentBO) {
4548
return blogCommentBOS;
4649
}
4750

51+
@Override
52+
public List<BlogCommentBO> selectBlogCommentListWithSub(BlogCommentBO blogCommentBO) {
53+
List<BlogComment> blogCommentEntityList = blogCommentMapper.selectBlogCommentList(BeanMapper.convert(blogCommentBO, BlogComment.class));
54+
if (CollectionUtils.isEmpty(blogCommentEntityList)) {
55+
return Lists.newArrayList();
56+
}
57+
List<Long> ids = blogCommentEntityList.stream().map(e -> e.getId()).collect(Collectors.toList());
58+
List<BlogComment> blogCommentByParentIds = blogCommentMapper.selectBlogCommentByParentIds(ids);
59+
List<BlogCommentBO> resultList = BeanMapper.convertList(blogCommentEntityList, BlogCommentBO.class);
60+
Map<Long, String> idUsernameMap = resultList.stream().collect(Collectors.toMap(BlogCommentBO::getId, BlogCommentBO::getUsername));
61+
idUsernameMap.putAll(blogCommentByParentIds.stream().collect(Collectors.toMap(BlogComment::getId, BlogComment::getUsername)));
62+
for (BlogCommentBO resultItem : resultList) {
63+
List<BlogComment> subCommentList = blogCommentByParentIds.stream()
64+
.filter(e -> Objects.equals(e.getParentId(), resultItem.getId()))
65+
.collect(Collectors.toList());
66+
if (CollectionUtils.isEmpty(subCommentList)) {
67+
continue;
68+
}
69+
List<BlogCommentBO> subCommentBOList = BeanMapper.convertList(subCommentList, BlogCommentBO.class);
70+
for (BlogCommentBO commentBO : subCommentBOList) {
71+
commentBO.setReplyUsername(idUsernameMap.getOrDefault(commentBO.getReplyId(), "-"));
72+
}
73+
resultItem.setSubComments(subCommentBOList);
74+
}
75+
return resultList;
76+
}
77+
4878
private void fillCommentInfo(List<BlogCommentBO> blogCommentBOList) {
4979
List<Long> articleIds = blogCommentBOList.stream().map(BlogCommentBO::getArticleId).collect(Collectors.toList());
5080
Map<Long, String> articleIdAndTitleMap = blogArticleService.selectBlogArticleByIds(articleIds).stream().collect(Collectors.toMap(BlogArticleBO::getId, BlogArticleBO::getTitle));
@@ -60,4 +90,9 @@ public int insertBlogComment(BlogCommentBO blogCommentBO) {
6090
return blogCommentMapper.insertBlogComment(blogComment);
6191
}
6292

93+
@Override
94+
public int addBlogCommentLikeCount(Long id) {
95+
return blogCommentMapper.addCommentLikeCount(id);
96+
}
97+
6398
}

dimple-modules/dimple-blog-front/dimple-blog-front-web/src/main/java/com/dimple/blog/front/web/controller/BlogCommentController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class BlogCommentController extends BaseController {
3434
public TableDataInfo list(BlogCommentVOParams blogComment) {
3535
startPage();
3636
BlogCommentBO blogCommentBO = BeanMapper.convert(blogComment, BlogCommentBO.class);
37-
List<BlogCommentBO> list = blogCommentService.selectBlogCommentList(blogCommentBO);
37+
List<BlogCommentBO> list = blogCommentService.selectBlogCommentListWithSub(blogCommentBO);
3838
return getDataTable(BeanMapper.convertList(list, BlogCommentVO.class));
3939
}
4040

@@ -50,4 +50,9 @@ public AjaxResult add(@RequestBody BlogCommentVOParams blogComment) {
5050
return toAjax(blogCommentService.insertBlogComment(blogCommentBO));
5151
}
5252

53+
@PostMapping("likeCount/{id}")
54+
public AjaxResult like(@PathVariable Long id) {
55+
return toAjax(blogCommentService.addBlogCommentLikeCount(id));
56+
}
57+
5358
}

dimple-modules/dimple-blog-front/dimple-blog-front-web/src/main/java/com/dimple/blog/front/web/controller/vo/BlogCommentVO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.dimple.common.core.web.vo.params.BaseVOParams;
44
import lombok.Data;
55

6+
import java.util.List;
7+
68
/**
79
* BlogCommentVO
810
*
@@ -42,4 +44,12 @@ public class BlogCommentVO extends BaseVOParams {
4244
* user email, if email is not null will reply when the comment has been replied
4345
*/
4446
private String email;
47+
48+
private Long replyId;
49+
50+
private String replyUsername;
51+
52+
private Long likeCount;
53+
54+
private List<BlogCommentVO> subComments;
4555
}

dimple-modules/dimple-blog-front/dimple-blog-front-web/src/main/java/com/dimple/blog/front/web/controller/vo/params/BlogCommentVOParams.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class BlogCommentVOParams extends BaseVOParams {
2727
*/
2828
private Long parentId;
2929

30+
private Long replyId;
31+
3032
/**
3133
* user head image
3234
*/

dimple-modules/dimple-blog-front/dimple-blog-front-web/src/main/resources/mapper/blog/front/BlogCommentMapper.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<result property="headImage" column="head_image"/>
1313
<result property="content" column="content"/>
1414
<result property="email" column="email"/>
15+
<result property="replyId" column="reply_id"/>
16+
<result property="likeCount" column="like_count"/>
1517
<result property="createBy" column="create_by"/>
1618
<result property="createTime" column="create_time"/>
1719
<result property="updateBy" column="update_by"/>
@@ -26,6 +28,8 @@
2628
head_image,
2729
content,
2830
email,
31+
reply_id,
32+
like_count,
2933
create_by,
3034
create_time,
3135
update_by,
@@ -37,11 +41,13 @@
3741
<include refid="selectBlogCommentVo"/>
3842
<where>
3943
<if test="articleId != null ">and article_id = #{articleId}</if>
44+
<if test="articleId == null ">and article_id &gt; 0</if>
4045
<if test="username != null and username != ''">and username like concat('%', #{username}, '%')</if>
4146
<if test="parentId != null ">and parent_id = #{parentId}</if>
4247
<if test="headImage != null and headImage != ''">and head_image = #{headImage}</if>
4348
<if test="content != null and content != ''">and content = #{content}</if>
4449
<if test="email != null and email != ''">and email = #{email}</if>
50+
and reply_id &lt; 0
4551
</where>
4652
</select>
4753

@@ -51,6 +57,16 @@
5157
</select>
5258

5359

60+
<select id="selectBlogCommentByParentIds" resultMap="BlogCommentResult">
61+
<include refid="selectBlogCommentVo"/>
62+
where parent_id in
63+
<foreach item="id" collection="collection" open="(" separator="," close=")">
64+
#{id}
65+
</foreach>
66+
order by create_time desc
67+
</select>
68+
69+
5470
<insert id="insertBlogComment" parameterType="BlogComment" useGeneratedKeys="true" keyProperty="id">
5571
insert into blog_comment
5672
<trim prefix="(" suffix=")" suffixOverrides=",">
@@ -60,6 +76,7 @@
6076
<if test="headImage != null">head_image,</if>
6177
<if test="content != null">content,</if>
6278
<if test="email != null">email,</if>
79+
<if test="replyId != null">reply_id,</if>
6380
<if test="createBy != null">create_by,</if>
6481
<if test="createTime != null">create_time,</if>
6582
<if test="updateBy != null">update_by,</if>
@@ -72,11 +89,15 @@
7289
<if test="headImage != null">#{headImage},</if>
7390
<if test="content != null">#{content},</if>
7491
<if test="email != null">#{email},</if>
92+
<if test="replyId != null">#{replyId},</if>
7593
<if test="createBy != null">#{createBy},</if>
7694
<if test="createTime != null">#{createTime},</if>
7795
<if test="updateBy != null">#{updateBy},</if>
7896
<if test="updateTime != null">#{updateTime},</if>
7997
</trim>
8098
</insert>
99+
<insert id="addCommentLikeCount">
100+
update blog_comment set like_count=like_count+1 where id =#{id}
101+
</insert>
81102

82103
</mapper>

dimple-ui-blog/src/api/messageBoard.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ import request from "@/utils/request";
44
export default {
55
getMessageBoard(query) {
66
return request({
7-
url: "/blog-front/category/list",
7+
url: "/blog-front/comment/list",
88
method: "get",
99
params: query
1010
});
1111
},
1212

13-
likeMessageBoard(query) {
13+
saveMessageBoard(data) {
1414
return request({
15-
url: "/blog-front/category/list",
16-
method: "get",
17-
params: query
15+
url: "/blog-front/comment",
16+
method: "post",
17+
data: data
18+
});
19+
},
20+
21+
likeMessageBoard(id) {
22+
return request({
23+
url: "/blog-front/comment/likeCount/"+id,
24+
method: "post",
1825
});
1926
}
2027
};

0 commit comments

Comments
 (0)