Skip to content

Commit 2bdc23b

Browse files
committed
fix:correct record is who like
1 parent 8fc385c commit 2bdc23b

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

api/guest/articles.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func PostArticle(c *gin.Context) {
4545
// GetArticleList is the api that returns a list of articles
4646
// if you want to add parameter like limit, please use url like /article?limit=10
4747
func GetArticleList(c *gin.Context) {
48+
user := service.UserService.GetCurrentUser(c)
4849
limit := c.DefaultQuery("limit", "10")
4950
sortby := c.DefaultQuery("sortby", "create_time")
5051
order := c.DefaultQuery("order", "desc")
@@ -62,7 +63,7 @@ func GetArticleList(c *gin.Context) {
6263
setAPIResponse(c, nil, err.Error(), false)
6364
}
6465

65-
resp, err := service.ArticleService.GetArticleList(limitNum, cursorTime, sortby, order)
66+
resp, err := service.ArticleService.GetArticleList(user, limitNum, cursorTime, sortby, order)
6667
if err != nil {
6768
setAPIResponse(c, nil, err.Error(), false)
6869
} else {
@@ -72,6 +73,7 @@ func GetArticleList(c *gin.Context) {
7273

7374
// GetArticleByID finds the article with ID
7475
func GetArticleByID(c *gin.Context) {
76+
user := service.UserService.GetCurrentUser(c)
7577
id := c.Param("id")
7678

7779
var err error
@@ -81,7 +83,7 @@ func GetArticleByID(c *gin.Context) {
8183
return
8284
}
8385

84-
resp, err := service.ArticleService.GetArticleByID(articleID)
86+
resp, err := service.ArticleService.GetArticleByID(user, articleID)
8587
if err != nil {
8688
setAPIResponse(c, nil, err.Error(), false)
8789
return

model/response.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type ArticleResponse struct {
5151
Title string `json:"title"`
5252
User *UserBriefInfo `json:"user"`
5353
Content string `json:"content"`
54+
Liked bool `json:"liked"`
5455
CommentCount int `json:"commnet_count"`
5556
LikeCount int `json:"like_count"`
5657
CreateTime int64 `json:"create_time"`

service/articles.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func (s *articleService) BuildArticle(user *model.User, title string, content st
4545
return article, nil
4646
}
4747

48-
func (s *articleService) GetArticleList(limit int, cursorTime int64, sortby string, order string) (*model.ArticleListResponse, error) {
48+
func (s *articleService) GetArticleList(currentUser *model.User, limit int, cursorTime int64, sortby string, order string) (*model.ArticleListResponse, error) {
4949
resp := &model.ArticleListResponse{}
5050
fields := []string{"id", "title", "create_time", "user_id", "view_count", "comment_count", "like_count", "content"}
5151
articles := repository.ArticleRepository.GetArticleFields(util.DB(), fields, cursorTime, limit, sortby, order)
5252

53-
briefList, minCursorTime := buildArticleList(articles)
53+
briefList, minCursorTime := buildArticleList(currentUser, articles)
5454

5555
resp.Cursor = minCursorTime
5656
for i := range briefList {
@@ -63,7 +63,7 @@ func (s *articleService) GetArticleList(limit int, cursorTime int64, sortby stri
6363
return resp, nil
6464
}
6565

66-
func (s *articleService) GetArticleByID(id int64) (*model.ArticleResponse, error) {
66+
func (s *articleService) GetArticleByID(currentUser *model.User, id int64) (*model.ArticleResponse, error) {
6767
articleInfo, err := repository.ArticleRepository.GetArticleByID(util.DB(), id)
6868
if err != nil {
6969
return nil, errors.New("查询文章信息出错")
@@ -78,14 +78,15 @@ func (s *articleService) GetArticleByID(id int64) (*model.ArticleResponse, error
7878
Title: articleInfo.Title,
7979
User: BuildUserBriefInfo(userInfo),
8080
Content: util.MarkdownToHTML(articleInfo.Content),
81+
Liked: LCService.JudgeArticleLiked(articleInfo, currentUser),
8182
CommentCount: articleInfo.CommentCount,
8283
LikeCount: articleInfo.LikeCount,
8384
CreateTime: articleInfo.CreateTime,
8485
}
8586
return resp, nil
8687
}
8788

88-
func buildArticleList(articles []model.Article) ([]*model.ArticleBriefInfo, int64) {
89+
func buildArticleList(currentUser *model.User, articles []model.Article) ([]*model.ArticleBriefInfo, int64) {
8990
var minCursorTime int64 = model.MAXCursorTime
9091
briefList := make([]*model.ArticleBriefInfo, len(articles))
9192
for i := range articles {
@@ -99,8 +100,8 @@ func buildArticleList(articles []model.Article) ([]*model.ArticleBriefInfo, int6
99100
briefList[i].LikeCount = articles[i].LikeCount
100101
briefList[i].ViewCount = articles[i].ViewCount
101102
briefList[i].CreateTime = articles[i].CreateTime
103+
briefList[i].Liked = LCService.JudgeArticleLiked(&articles[i], currentUser)
102104
user, _ := repository.UserRepository.GetUserByUserID(util.DB(), articles[i].UserID)
103-
briefList[i].Liked = LCService.JudgeArticleLiked(&articles[i], user)
104105
briefList[i].User = BuildUserBriefInfo(user)
105106
}
106107

service/like_collection.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (s *lcService) PostLikeArticle(userID int64, articleID int64) error {
4949
if err != nil {
5050
return err
5151
}
52-
err = util.DB().Exec("update t_article set like_count = like_count+1 where id = ? and user_id = ?", articleID, userID).Error
52+
err = util.DB().Exec("update t_article set like_count = like_count+1 where id = ?", articleID).Error
5353
return err
5454
})
5555
if err != nil {
@@ -60,6 +60,9 @@ func (s *lcService) PostLikeArticle(userID int64, articleID int64) error {
6060

6161
// JudgeArticleLiked judge user has liked the article or not.
6262
func (s *lcService) JudgeArticleLiked(article *model.Article, user *model.User) bool {
63+
if user == nil {
64+
return false
65+
}
6366
lcStatus, _ := repository.LCRepository.GetUserLikeOperation(util.DB(), user.ID, article.ID)
6467
return lcStatus.Status == LikeArticle
6568
}
@@ -82,7 +85,7 @@ func (s *lcService) PostDelLikeArticle(userID int64, articleID int64) error {
8285
if err != nil {
8386
return err
8487
}
85-
err = util.DB().Exec("update t_article set like_count = like_count-1 where id = ? and user_id = ?", articleID, userID).Error
88+
err = util.DB().Exec("update t_article set like_count = like_count-1 where id = ?", articleID).Error
8689
return err
8790
})
8891
if err != nil {

site/components/TopicList.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,17 @@ export default {
8282
methods: {
8383
async like(article) {
8484
try {
85+
if (article.liked) {
86+
this.$message.success('已赞过,请勿重复点赞')
87+
return
88+
}
89+
if (this.$store.state.user.current == null) {
90+
this.$message.success('请登录后操作')
91+
return
92+
}
8593
let data = {
8694
article_id: article.article_id,
87-
user_id: article.user.id,
95+
user_id: this.$store.state.user.current.id,
8896
}
8997
await this.$axios.post('/api/topics/like', data)
9098
article.liked = true

site/pages/topic/_id.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export default {
231231
return {
232232
topic,
233233
commentsPage,
234-
liked: false,
234+
liked: topic.liked,
235235
favorited: false,
236236
}
237237
},
@@ -275,6 +275,10 @@ export default {
275275
if (this.liked) {
276276
return
277277
}
278+
if (this.$store.state.user.current == null) {
279+
this.$message.error('请登录后操作')
280+
return
281+
}
278282
await this.$axios.post('/api/topics/like', {
279283
article_id: topic.article_id,
280284
user_id: this.$store.state.user.current.id,

0 commit comments

Comments
 (0)