From ca9967f5726df0e604a78463837cc8e1ed4cc7e0 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 29 Sep 2023 18:40:01 +0800 Subject: [PATCH] sqlx: add highlightConment data logic --- internal/dao/sakila/auto/cc/cc.go | 10 ++++++++++ internal/dao/sakila/auto/pgc/pgc.go | 5 +++++ internal/dao/sakila/comments.go | 10 ++++++---- internal/dao/sakila/comments_pgc.go | 5 +++++ internal/dao/sakila/contacts.go | 4 ++-- internal/dao/sakila/yesql/yesql_cc.sql | 9 +++++++++ internal/dao/sakila/yesql/yesql_pgc.sql | 7 +++++++ 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/internal/dao/sakila/auto/cc/cc.go b/internal/dao/sakila/auto/cc/cc.go index b5be15bc..e84cdbb6 100644 --- a/internal/dao/sakila/auto/cc/cc.go +++ b/internal/dao/sakila/auto/cc/cc.go @@ -39,8 +39,10 @@ const ( _CommentManage_DeleteCommentReply = `UPDATE @comment_reply SET deleted_on=?, is_del=1 WHERE id=? AND is_del=0` _CommentManage_DeleteCommentThumbs = `UPDATE @tweet_comment_thumbs SET deleted_on=?, is_del=1 WHERE user_id=? AND tweet_id=? AND comment_id=? AND is_del=0` _CommentManage_DeleteReplyThumbs = `UPDATE @tweet_comment_thumbs SET deleted_on=?, is_del=1 WHERE user_id=? AND comment_id=? AND reply_id=? AND is_del=0` + _CommentManage_GetIssenceStatusById = `SELECT is_essence FROM @comment WHERE id=?` _CommentManage_GetCommentReplyThumb = `SELECT * FROM @tweet_comment_thumbs WHERE user_id=? AND tweet_id=? AND comment_id=? AND reply_id=? AND comment_type=1 AND is_del=0` _CommentManage_GetTweetCommentThumb = `SELECT * FROM @tweet_comment_thumbs WHERE user_id=? AND tweet_id=? AND comment_id=? AND comment_type=0 AND is_del=0` + _CommentManage_HighlightComment = `UPDATE @comment SET is_essence=1-is_essence, modified_on=? WHERE id=? AND user_id=? AND is_del=0` _CommentManage_IncrCommentReplyCount = `UPDATE @comment SET reply_count=reply_count+1, modified_on=? WHERE id=? AND is_del=0` _CommentManage_UpdateCommentThumbsCount = `UPDATE @comment SET thumbs_up_count=?, thumbs_down_count=?, modified_on=? WHERE id=? AND is_del=0` _CommentManage_UpdateReplyThumbsCount = `UPDATE @comment_reply SET thumbs_up_count=?, thumbs_down_count=?, modified_on=? WHERE id=? AND is_del=0` @@ -256,8 +258,10 @@ type CommentManage struct { DeleteCommentReply *sqlx.Stmt `yesql:"delete_comment_reply"` DeleteCommentThumbs *sqlx.Stmt `yesql:"delete_comment_thumbs"` DeleteReplyThumbs *sqlx.Stmt `yesql:"delete_reply_thumbs"` + GetIssenceStatusById *sqlx.Stmt `yesql:"getIssenceStatusById"` GetCommentReplyThumb *sqlx.Stmt `yesql:"get_comment_reply_thumb"` GetTweetCommentThumb *sqlx.Stmt `yesql:"get_tweet_comment_thumb"` + HighlightComment *sqlx.Stmt `yesql:"highlight_comment"` IncrCommentReplyCount *sqlx.Stmt `yesql:"incr_comment_reply_count"` UpdateCommentThumbsCount *sqlx.Stmt `yesql:"update_comment_thumbs_count"` UpdateReplyThumbsCount *sqlx.Stmt `yesql:"update_reply_thumbs_count"` @@ -575,12 +579,18 @@ func BuildCommentManage(p PreparexBuilder, ctx ...context.Context) (obj *Comment if obj.DeleteReplyThumbs, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_DeleteReplyThumbs))); err != nil { return nil, fmt.Errorf("prepare _CommentManage_DeleteReplyThumbs error: %w", err) } + if obj.GetIssenceStatusById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_GetIssenceStatusById))); err != nil { + return nil, fmt.Errorf("prepare _CommentManage_GetIssenceStatusById error: %w", err) + } if obj.GetCommentReplyThumb, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_GetCommentReplyThumb))); err != nil { return nil, fmt.Errorf("prepare _CommentManage_GetCommentReplyThumb error: %w", err) } if obj.GetTweetCommentThumb, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_GetTweetCommentThumb))); err != nil { return nil, fmt.Errorf("prepare _CommentManage_GetTweetCommentThumb error: %w", err) } + if obj.HighlightComment, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_HighlightComment))); err != nil { + return nil, fmt.Errorf("prepare _CommentManage_HighlightComment error: %w", err) + } if obj.IncrCommentReplyCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_IncrCommentReplyCount))); err != nil { return nil, fmt.Errorf("prepare _CommentManage_IncrCommentReplyCount error: %w", err) } diff --git a/internal/dao/sakila/auto/pgc/pgc.go b/internal/dao/sakila/auto/pgc/pgc.go index b7878edd..72f1a649 100644 --- a/internal/dao/sakila/auto/pgc/pgc.go +++ b/internal/dao/sakila/auto/pgc/pgc.go @@ -21,6 +21,7 @@ const ( _CommentManage_CreateComment = `INSERT INTO @comment (post_id, user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?) RETURNING *` _CommentManage_CreateCommentContent = `INSERT INTO @comment_content (comment_id, user_id, content, type, sort, created_on) VALUES (?, ?, ?, ?, ?, ?) RETURNING *` _CommentManage_CreateCommentReply = `INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *` + _CommentManage_HighlightComment = `UPDATE @comment SET is_essence=1-is_essence, modified_on=? WHERE id=? AND user_id=? AND is_del=0 RETURNING is_essence` _ContactManager_CreateContact = `INSERT INTO @contact (user_id, friend_id, status, created_on) VALUES (?, ?, ?, ?) RETURNING *` _Message_CreateMessage = `INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, post_id, comment_id, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :post_id, :comment_id, :reply_id, :created_on) RETURNING id` _Tweet_ListIndexHotsTweets = `SELECT post.* FROM @post post LEFT JOIN @post_metric metric ON post.id=metric.post_id WHERE post.visibility>=90 AND post.is_del=0 ORDER BY post.is_top DESC, metric.rank_score DESC NULLS LAST, post.latest_replied_on DESC LIMIT ? OFFSET ?` @@ -63,6 +64,7 @@ type CommentManage struct { CreateComment *sqlx.Stmt `yesql:"create_comment"` CreateCommentContent *sqlx.Stmt `yesql:"create_comment_content"` CreateCommentReply *sqlx.Stmt `yesql:"create_comment_reply"` + HighlightComment *sqlx.Stmt `yesql:"highlight_comment"` } type ContactManager struct { @@ -130,6 +132,9 @@ func BuildCommentManage(p PreparexBuilder, ctx ...context.Context) (obj *Comment if obj.CreateCommentReply, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_CreateCommentReply))); err != nil { return nil, fmt.Errorf("prepare _CommentManage_CreateCommentReply error: %w", err) } + if obj.HighlightComment, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_CommentManage_HighlightComment))); err != nil { + return nil, fmt.Errorf("prepare _CommentManage_HighlightComment error: %w", err) + } return } diff --git a/internal/dao/sakila/comments.go b/internal/dao/sakila/comments.go index bacb3a92..ef8c7e33 100644 --- a/internal/dao/sakila/comments.go +++ b/internal/dao/sakila/comments.go @@ -178,7 +178,7 @@ func (s *commentManageSrv) DeleteCommentReply(r *ms.CommentReply) error { if _, err = tx.Stmtx(s.q.DeleteCommentReply).Exec(now, r.ID); err != nil { return } - if _, err = tx.Stmtx(s.q.DeleteCommentThumbs).Exec(now, r.UserID, r.CommentID, r.ID); err == nil { + if _, err = tx.Stmtx(s.q.DeleteReplyThumbs).Exec(now, r.UserID, r.CommentID, r.ID); err == nil { // 宽松处理错误 tx.Stmtx(s.q.DecrCommentReplyCount).Exec(now, r.CommentID) } @@ -394,9 +394,11 @@ func (s *commentManageSrv) ThumbsDownReply(userId int64, tweetId, commentId, rep }) } -func (s *commentManageSrv) HighlightComment(userId, commentId int64) (int8, error) { - // TODO - return 0, cs.ErrNotImplemented +func (s *commentManageSrv) HighlightComment(userId, commentId int64) (res int8, err error) { + if _, err = s.q.HighlightComment.Exec(time.Now().Unix(), commentId, userId); err == nil { + err = s.q.GetIssenceStatusById.Get(&res, commentId) + } + return } func newCommentService(db *sqlx.DB) (s core.CommentService) { diff --git a/internal/dao/sakila/comments_pgc.go b/internal/dao/sakila/comments_pgc.go index 19f96c57..28acaa37 100644 --- a/internal/dao/sakila/comments_pgc.go +++ b/internal/dao/sakila/comments_pgc.go @@ -49,6 +49,11 @@ func (s *pgcCommentManageSrv) CreateCommentContent(r *ms.CommentContent) (*ms.Co r.Type, r.Sort, time.Now().Unix()) } +func (s *pgcCommentManageSrv) HighlightComment(userId, commentId int64) (res int8, err error) { + err = s.q.HighlightComment.Get(&res, time.Now().Unix(), commentId, userId) + return +} + func (s *pgcCommentSrv) GetComments(tweetId int64, style cs.StyleCommentType, limit int, offset int) (res []*ms.Comment, total int64, err error) { switch style { case cs.StyleCommentHots: diff --git a/internal/dao/sakila/contacts.go b/internal/dao/sakila/contacts.go index 0eb518f2..bcdaf8bd 100644 --- a/internal/dao/sakila/contacts.go +++ b/internal/dao/sakila/contacts.go @@ -67,7 +67,7 @@ func (s *contactManageSrv) RequestingFriend(userId int64, friendId int64, greeti func (s *contactManageSrv) AddFriend(userId int64, friendId int64) error { return s.db.Withx(func(tx *sqlx.Tx) error { contact := &cs.Contact{} - err := tx.Stmtx(s.q.GetUserFriend).Get(contact, userId, friendId) + err := tx.Stmtx(s.q.GetUserFriend).Get(contact, friendId, userId) if err != nil { return err } @@ -103,7 +103,7 @@ func (s *contactManageSrv) AddFriend(userId int64, friendId int64) error { func (s *contactManageSrv) RejectFriend(userId int64, friendId int64) error { return s.db.Withx(func(tx *sqlx.Tx) error { contact := &cs.Contact{} - err := tx.Stmtx(s.q.GetUserFriend).Get(contact, userId, friendId) + err := tx.Stmtx(s.q.GetUserFriend).Get(contact, friendId, userId) if err != nil { return err } diff --git a/internal/dao/sakila/yesql/yesql_cc.sql b/internal/dao/sakila/yesql/yesql_cc.sql index 73d6f490..c7ab08a2 100644 --- a/internal/dao/sakila/yesql/yesql_cc.sql +++ b/internal/dao/sakila/yesql/yesql_cc.sql @@ -111,6 +111,15 @@ VALUES (?, ?, ?, ?, ?); INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?, ?, ?); +-- name: highlight_comment@comment_manage +UPDATE @comment +SET is_essence=1-is_essence, + modified_on=? +WHERE id=? AND user_id=? AND is_del=0; + +-- name: getIssenceStatusById@comment_manage +SELECT is_essence FROM @comment WHERE id=?; + -- name: incr_comment_reply_count@comment_manage -- prepare: stmt UPDATE @comment diff --git a/internal/dao/sakila/yesql/yesql_pgc.sql b/internal/dao/sakila/yesql/yesql_pgc.sql index b7270ed9..d0eff6a6 100644 --- a/internal/dao/sakila/yesql/yesql_pgc.sql +++ b/internal/dao/sakila/yesql/yesql_pgc.sql @@ -98,6 +98,13 @@ INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *; +-- name: highlight_comment@comment_manage +UPDATE @comment +SET is_essence=1-is_essence, + modified_on=? +WHERE id=? AND user_id=? AND is_del=0 +RETURNING is_essence; + -------------------------------------------------------------------------------- -- contact_manager sql dml --------------------------------------------------------------------------------