ChatViewer
  1. 评论
ChatViewer
  • 用户
    • 用户接口
    • 发送验证码
      GET
    • 用户登录
      POST
    • 用户注册
      POST
    • 用户修改信息
      PUT
    • 用户信息
      GET
  • 分类
    • 分类目录接口
    • 查询分类目录
      GET
    • 删除分类
      DELETE
    • 插入分类路径
      POST
    • 插入子分类
      POST
  • ChatGPT
    • GPT会话列表
      GET
    • 新建会话
      POST
    • 删除会话
      DELETE
    • 消息记录
      GET
    • 发送消息
      POST
  • 文章
    • 文章详情查询
      GET
    • 文章分页查询
      GET
    • 上传文件
      POST
    • 上传文章
      POST
  • 点赞
    • 点赞/取消点赞
      POST
    • 点赞状态查询
      GET
  • 评论
    • 评论查询
      GET
    • 添加评论
      POST
  • 聊天
    • 会话查询
    • 聊天记录
    • 发送消息
    • 设置已读
  • 通知
    • 通知
    • 通知摘要
    • 通知查询
    • 设为已读
  • 问题
    • 问题分页查询
    • 添加问题
    • 问题查询
    • 回答查询
    • 保存回答
    • 求助GPT
  • 购买token
    • 添加商品
    • 秒杀免费商品
  1. 评论

评论查询

GET
/comment
值得看看实现
查询文章下所有评论。
image.png
评论采用二级结构,分为“对文章的评论”和“对评论的评论”。盖楼通过target_id实现,target_id标识了该条评论的回复对象id。
image.png
查询文章下所有评论,分为两个任务,两个任务并行执行以加速。
1.
组织评论结构
2.
组织点赞列表

1 组织评论结构#

image.png
在MyBatis-Plus中使用ResultMap,以实现正确地将UserDtp类作为CommetDto类的一个属性。
	<!-- 配置 CommentDto中的映射关系 -->
    <resultMap id="commentResultMap" type="com.chatwithinterview.blog.dto.CommentDto">
        <id property="id" column="comment_id" />
        <result property="parentId" column="parent_id" />
        <result property="uid" column="user_id" />
        <result property="content" column="comment_content" />
        <result property="likes" column="like_counts" />
        <result property="createTime" column="create_time" />
        <!-- 配置 user 的映射关系 -->
        <association property="user" javaType="com.chatwithinterview.blog.dto.UserDtoForComment">
            <result property="username" column="user_name" />
            <result property="avatar" column="user_avatar" />
        </association>
    </resultMap>

    <select id="queryReplyWithAuthor" resultMap="commentResultMap">
        select blog_comment.*,
               blog_user.user_name,
               blog_user.user_avatar
        FROM blog_comment JOIN blog_user ON blog_comment.user_id = blog_user.user_id
        WHERE blog_comment.parent_id = #{parentId} and blog_comment.comment_type = #{commentType}
    </select>

2 组织点赞列表#

image.png
一开始的设计:在文章详情页,将该文章所有的评论点赞情况读入Redis,将评论的点赞用户集合和点赞数读入Redis。不合理的地方:有些评论可能访问量非常低,甚至为0,这样的评论没有必要浪费时间查询其点赞情况。
更改后:首先从MySQL中查找该文章下用户点赞过的所有评论,然后检查Redis中是否有与MySQL不一致的数据,如果有,以Redis为准。
image.png

请求参数

Query 参数
articleId
string 
必需
示例值:
1703374330079252482
Header 参数
Token
string 
必需
默认值:
{{token}}

示例代码

Shell
JavaScript
Java
Swift
Go
PHP
Python
HTTP
C
C#
Objective-C
Ruby
OCaml
Dart
R
请求示例请求示例
Shell
JavaScript
Java
Swift
curl --location --request GET '/comment?articleId=1703374330079252482' \
--header 'Token;'

返回响应

🟢200成功
application/json
Body
code
integer 
必需
msg
string 
必需
data
object 
必需
likeList
array[string]
点赞列表
必需
已登录用户在该文章下点赞评论的id
comments
array [object {8}] 
评论列表
必需
该文章下的评论列表
示例
{
    "code": 200,
    "msg": "success",
    "data": {
        "likeList": [
            "1703381796103950337",
            "1707724217031217154"
        ],
        "comments": [
            {
                "id": "1703381796103950337",
                "parentId": "1703376641350471682",
                "uid": "1703381665786925057",
                "content": "不错不错嘛![狗头]",
                "likes": 1,
                "createTime": "2023-09-17 20:14:02",
                "user": {
                    "username": "桃桃",
                    "avatar": "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                },
                "reply": {
                    "total": 2,
                    "list": [
                        {
                            "id": "1707724019802460161",
                            "parentId": "1703381796103950337",
                            "uid": "1703365214623535105",
                            "content": "回复 <span style=\"color: var(--u-color-success-dark-2);\">@桃桃:</span> 谢谢你[打call]",
                            "likes": 0,
                            "createTime": "2023-09-29 19:48:29",
                            "user": {
                                "username": "木槿",
                                "avatar": "https://blog-picture-upload-bucket.oss-cn-beijing.aliyuncs.com/article_picture/19d51b93-e3a4-40b3-8e08-3d99d2a2ace7.jpg"
                            },
                            "reply": null
                        },
                        {
                            "id": "1707724217031217154",
                            "parentId": "1703381796103950337",
                            "uid": "1703365214623535105",
                            "content": "回复 <span style=\"color: var(--u-color-success-dark-2);\">@木槿:</span> 愿意的话可以提些建议呐!",
                            "likes": 1,
                            "createTime": "2023-09-29 19:49:16",
                            "user": {
                                "username": "木槿",
                                "avatar": "https://blog-picture-upload-bucket.oss-cn-beijing.aliyuncs.com/article_picture/19d51b93-e3a4-40b3-8e08-3d99d2a2ace7.jpg"
                            },
                            "reply": null
                        }
                    ]
                }
            }
        ]
    }
}
修改于 2023-10-04 13:34:53
上一页
点赞状态查询
下一页
添加评论
Built with