Skip to content

Commit 37718e8

Browse files
committed
feat: add comment and show comments
1 parent da65b95 commit 37718e8

File tree

18 files changed

+275
-111
lines changed

18 files changed

+275
-111
lines changed

api/entrance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func AppRun() {
4343
user.GET("/user/logout", guest.Logout)
4444
user.GET("/user/current", guest.GetCurrentUser)
4545

46-
user.POST("/topic", guest.PostArticle)
46+
user.POST("/topics", guest.PostArticle)
4747
user.GET("/topics", guest.GetArticleList)
4848
user.GET("/topics/:id", guest.GetArticleByID)
4949
user.POST("/topics/like", guest.PostLikeArticle)

api/guest/articles.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66

77
"github.com/gin-gonic/gin"
8+
"github.com/nk-akun/NeighborBBS/logs"
89
"github.com/nk-akun/NeighborBBS/model"
910
"github.com/nk-akun/NeighborBBS/service"
1011
"github.com/nk-akun/NeighborBBS/util"
@@ -13,6 +14,7 @@ import (
1314
// PostArticle is the api used to build a article
1415
func PostArticle(c *gin.Context) {
1516
req := getReqFromContext(c).(*model.ArticleRequest)
17+
logs.Logger.Info(req)
1618
var err error
1719
if req.UserID == 0 {
1820
err = errors.New("user_id is invalid")

model/constant.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ const (
1010
CTXAPIResponseSuccess = "api_response_success"
1111

1212
TokenExpireDays = 2
13+
MAXCursorTime = 2559090472000
1314
)

model/response.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ type ArticleBriefInfo struct {
4646

4747
// ArticleResponse ...
4848
type ArticleResponse struct {
49-
Title string `json:"title"`
50-
AuthorID int64 `json:"user_id"`
51-
AuthorName string `json:"nick_name"`
52-
AvatarURL string `json:"avatar_url"`
53-
Content string `json:"content"`
54-
CommentCount int `json:"commnet_count"`
55-
LikeCount int `json:"like_count"`
56-
CreateTime int64 `json:"create_time"`
49+
ArticleID int64 `json:"article_id"`
50+
Title string `json:"title"`
51+
User *UserBriefInfo `json:"user"`
52+
Content string `json:"content"`
53+
CommentCount int `json:"commnet_count"`
54+
LikeCount int `json:"like_count"`
55+
CreateTime int64 `json:"create_time"`
5756
}
5857

5958
// Comment

repository/comment_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *commentRepository) Create(db *gorm.DB, comment *model.Comment) error {
2525

2626
func (r *commentRepository) GetCommentsByCursorTime(db *gorm.DB, articleID int64, cursorTime int64) ([]model.Comment, error) {
2727
var comments []model.Comment
28-
err := db.Where("create_time < ?", cursorTime).Limit(30).Find(&comments).Error
28+
err := db.Where("create_time < ?", cursorTime).Where("article_id = ?", articleID).Limit(30).Find(&comments).Error
2929
if err != nil {
3030
logs.Logger.Errorf("query db error:", err)
3131
return nil, err

service/articles.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func (s *articleService) GetArticleList(limit int, cursorTime int64, sortby stri
3939
fields := []string{"id", "title", "create_time", "user_id", "view_count", "comment_count", "like_count"}
4040
articles := repository.ArticleRepository.GetArticleFields(util.DB(), fields, cursorTime, limit, sortby, order)
4141

42-
briefList := BuildArticleList(articles)
42+
briefList, minCursorTime := buildArticleList(articles)
4343

44-
resp.Cursor = cursorTime
44+
resp.Cursor = minCursorTime
4545
for i := range briefList {
4646
if briefList[i].CreateTime < resp.Cursor {
4747
resp.Cursor = briefList[i].CreateTime
@@ -63,10 +63,9 @@ func (s *articleService) GetArticleByID(id int64) (*model.ArticleResponse, error
6363
}
6464

6565
resp := &model.ArticleResponse{
66+
ArticleID: articleInfo.ID,
6667
Title: articleInfo.Title,
67-
AuthorID: userInfo.ID,
68-
AuthorName: userInfo.Nickname,
69-
AvatarURL: userInfo.AvatarURL,
68+
User: BuildUserBriefInfo(userInfo),
7069
Content: articleInfo.Content,
7170
CommentCount: articleInfo.CommentCount,
7271
LikeCount: articleInfo.LikeCount,
@@ -75,10 +74,11 @@ func (s *articleService) GetArticleByID(id int64) (*model.ArticleResponse, error
7574
return resp, nil
7675
}
7776

78-
// BuildArticleList ...
79-
func BuildArticleList(articles []model.Article) []*model.ArticleBriefInfo {
77+
func buildArticleList(articles []model.Article) ([]*model.ArticleBriefInfo, int64) {
78+
var minCursorTime int64 = model.MAXCursorTime
8079
briefList := make([]*model.ArticleBriefInfo, len(articles))
8180
for i := range articles {
81+
minCursorTime = util.MinInt64(minCursorTime, articles[i].CreateTime)
8282
briefList[i] = new(model.ArticleBriefInfo)
8383
briefList[i].ArticleID = articles[i].ID
8484
briefList[i].Title = articles[i].Title
@@ -91,5 +91,5 @@ func BuildArticleList(articles []model.Article) []*model.ArticleBriefInfo {
9191
briefList[i].User = BuildUserBriefInfo(user)
9292
}
9393

94-
return briefList
94+
return briefList, minCursorTime
9595
}

service/comments.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,23 @@ func (s *commentService) GetCommentList(articleID int64, cursorTime int64) (*mod
4343
return nil, errors.New("查询评论信息出错")
4444
}
4545

46+
commentList, minCursorTime := buildCommentList(comtList)
4647
resp.ArticleID = articleID
4748
resp.TotalNum = len(comtList)
48-
resp.Cursor = cursorTime
49-
buildCommentList(comtList)
49+
resp.Cursor = minCursorTime
50+
resp.CommentList = commentList
5051
return resp, nil
5152
}
5253

53-
func buildCommentList(comtList []model.Comment) []*model.CommentInfo {
54+
func buildCommentList(comtList []model.Comment) ([]*model.CommentInfo, int64) {
55+
var minCursorTime int64 = model.MAXCursorTime
56+
5457
sortComments(comtList, func(p, q *model.Comment) bool {
5558
return p.ID < q.ID
5659
})
5760
detailedCommentList := make([]*model.CommentInfo, len(comtList))
5861
for i := range comtList {
62+
minCursorTime = util.MinInt64(minCursorTime, comtList[i].CreateTime)
5963
userInfo, err := repository.UserRepository.GetUserByUserID(util.DB(), comtList[i].UserID)
6064
if err != nil {
6165
logs.Logger.Errorf("查询作者信息出错")
@@ -70,20 +74,20 @@ func buildCommentList(comtList []model.Comment) []*model.CommentInfo {
7074
LikeCount: comtList[i].LikeCount,
7175
CreateTime: comtList[i].CreateTime,
7276
}
73-
detailedCommentList[i].ParentComment = findParentComment(i, detailedCommentList[i].CommentID, detailedCommentList)
77+
detailedCommentList[i].ParentComment = findParentComment(i, comtList[i].ParentID, detailedCommentList)
7478
}
75-
return detailedCommentList
79+
return detailedCommentList, minCursorTime
7680
}
7781

78-
func findParentComment(len int, commentID int64, detailedCommentList []*model.CommentInfo) *model.CommentInfo {
82+
func findParentComment(len int, parentID int64, detailedCommentList []*model.CommentInfo) *model.CommentInfo {
7983
var l, r int = 0, len
8084
var mid int
8185
for l <= r {
8286
mid = (l + r) >> 1
83-
if detailedCommentList[mid].CommentID == commentID {
87+
if detailedCommentList[mid].CommentID == parentID {
8488
return detailedCommentList[mid]
8589
}
86-
if detailedCommentList[mid].CommentID > commentID {
90+
if detailedCommentList[mid].CommentID > parentID {
8791
r = mid - 1
8892
} else {
8993
l = mid + 1
@@ -110,7 +114,7 @@ func (pw commentWrapper) Less(i, j int) bool { // rewrite Less()
110114
return pw.by(&pw.comments[i], &pw.comments[j])
111115
}
112116

113-
// 封装成 SortPerson 方法
117+
// sortComments
114118
func sortComments(comments []model.Comment, by sortBy) {
115119
sort.Sort(commentWrapper{comments, by})
116120
}

site/components/CommentInput.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
</div>
1919
<div class="comment-button-wrapper">
2020
<span>Ctrl or ⌘ + Enter</span>
21-
<button
22-
class="button is-small is-success"
23-
@click="create"
24-
v-text="btnName"
25-
/>
21+
<button class="button is-small is-success" @click="create" v-text="btnName" />
2622
</div>
2723
</div>
2824
<div v-else class="comment-not-login">
@@ -83,11 +79,18 @@ export default {
8379
}
8480
this.sending = true
8581
try {
86-
const data = await this.$axios.post('/api/comment/create', {
87-
entityType: this.entityType,
88-
entityId: this.entityId,
82+
console.log(
83+
this.$store.state.user.current.id,
84+
this.entityId,
85+
this.content,
86+
this.quote ? this.quote.comment_id : 0
87+
)
88+
const data = await this.$axios.post('/api/comments', {
89+
// entityType: this.entityType,
90+
user_id: this.$store.state.user.current.id,
91+
article_id: this.entityId,
8992
content: this.content,
90-
quoteId: this.quote ? this.quote.commentId : '',
93+
parent_id: this.quote ? this.quote.comment_id : 0,
9194
})
9295
this.$emit('created', data)
9396
this.content = ''

site/components/CommentList.vue

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
22
<div class="comments">
3-
<load-more
3+
<load-more-comments
44
v-if="commentsPage"
55
ref="commentsLoadMore"
66
v-slot="{ results }"
77
:init-data="commentsPage"
8-
:params="{ entityType: entityType, entityId: entityId }"
8+
:params="{ entityType: entityType, article_id: entityId }"
99
url="/api/comments"
1010
>
1111
<ul>
@@ -24,7 +24,10 @@
2424
ad-layout-key="-ht-19-1m-3j+mu"
2525
/>
2626
<div class="comment-avatar">
27-
<avatar :user="comment.user" size="35" />
27+
<avatar
28+
:user="{nickname:comment.user_nickname,username:comment.user_username,id:comment.user_id,avatar_url:comment.avatar_url}"
29+
size="35"
30+
/>
2831
</div>
2932
<div class="comment-meta">
3033
<span
@@ -33,49 +36,52 @@
3336
itemscope
3437
itemtype="http://schema.org/Person"
3538
>
36-
<a :href="'/user/' + comment.user.id" itemprop="name">{{ comment.user.nickname }}</a>
39+
<a :href="'/user/' + comment.user_id" itemprop="name">{{ comment.user_nickname }}</a>
3740
</span>
3841
<span class="comment-time">
3942
<time
4043
:datetime="
41-
comment.createTime | formatDate('yyyy-MM-ddTHH:mm:ss')
44+
comment.create_time | formatDate('yyyy-MM-ddTHH:mm:ss')
4245
"
4346
itemprop="datePublished"
44-
>{{ comment.createTime | prettyDate }}</time>
47+
>{{ comment.create_time | prettyDate }}</time>
4548
</span>
4649
<span class="comment-reply">
4750
<a @click="reply(comment)">回复</a>
4851
</span>
4952
</div>
5053
<div class="comment-content content">
51-
<blockquote v-if="comment.quote" class="comment-quote">
54+
<blockquote v-if="comment.parent_comment" class="comment-quote">
5255
<div class="comment-quote-user">
53-
<avatar :user="comment.quote.user" size="20" />
54-
<a class="quote-nickname">{{ comment.quote.user.nickname }}</a>
55-
<span class="quote-time">{{ comment.quote.createTime | prettyDate }}</span>
56+
<avatar
57+
:user="{nickname:comment.parent_comment.user_nickname,username:comment.parent_comment.user_username,id:comment.parent_comment.user_id,avatar_url:comment.parent_comment.avatar_url}"
58+
size="20"
59+
/>
60+
<a class="quote-nickname">{{ comment.parent_comment.user_nickname }}</a>
61+
<span class="quote-time">{{ comment.parent_comment.create_time | prettyDate }}</span>
5662
</div>
5763
<div
5864
v-lazy-container="{ selector: 'img' }"
5965
itemprop="text"
60-
v-html="comment.quote.content"
66+
v-html="comment.parent_comment.content"
6167
/>
6268
</blockquote>
6369
<p v-lazy-container="{ selector: 'img' }" v-html="comment.content" />
6470
</div>
6571
</li>
6672
</ul>
67-
</load-more>
73+
</load-more-comments>
6874
</div>
6975
</template>
7076

7177
<script>
7278
import Avatar from '~/components/Avatar'
73-
import LoadMore from '~/components/LoadMore'
79+
import LoadMoreComments from './LoadMoreComments.vue'
7480
7581
export default {
7682
components: {
7783
Avatar,
78-
LoadMore,
84+
LoadMoreComments,
7985
},
8086
props: {
8187
entityType: {

site/components/CommentTextInput.vue

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
</div>
1818
<div class="comment-button-wrapper">
1919
<span>Ctrl or ⌘ + Enter</span>
20-
<button
21-
class="button is-small is-success"
22-
@click="create"
23-
v-text="btnName"
24-
/>
20+
<button class="button is-small is-success" @click="create" v-text="btnName" />
2521
</div>
2622
</div>
2723
<div v-else class="comment-not-login">
@@ -78,12 +74,13 @@ export default {
7874
}
7975
this.sending = true
8076
try {
81-
const data = await this.$axios.post('/api/comment/create', {
82-
contentType: 'text',
83-
entityType: this.entityType,
84-
entityId: this.entityId,
77+
const data = await this.$axios.post('/api/comments', {
78+
// contentType: 'text',
79+
// entityType: this.entityType,
80+
user_id: this.$store.state.user.current.id,
81+
article_id: this.entityId,
8582
content: this.content,
86-
quoteId: this.quote ? this.quote.commentId : '',
83+
parent_id: this.quote ? this.quote.comment_id : 0,
8784
})
8885
this.$emit('created', data)
8986
this.content = ''

0 commit comments

Comments
 (0)