From 35b850d53d5b47ce764d2c75bc1e6761fb9acea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Thu, 6 Mar 2025 19:16:09 +0800 Subject: [PATCH] feat: new proto --- pkg/domain/entity/comment.go | 1 + pkg/domain/repository/comment.go | 3 ++- pkg/domain/usecase/comment.go | 4 +++- pkg/repository/comment.go | 11 ++++++++--- pkg/usecase/comment.go | 6 ++++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/domain/entity/comment.go b/pkg/domain/entity/comment.go index ebb761e..005f782 100644 --- a/pkg/domain/entity/comment.go +++ b/pkg/domain/entity/comment.go @@ -14,6 +14,7 @@ type Comments struct { ParentCommentID string `bson:"parent_comment_id"` // 父留言 id,沒有就空白 Message string `bson:"message"` // 留言內容 Level comment.Level `bson:"level"` // 等級,數字較小的為上層 + IsVisitable bool `bson:"is_visitable"` // 是否隱藏 UpdatedAt int64 `bson:"updated_at"` // 更新時間 CreatedAt int64 `bson:"created_at"` // 建立時間 } diff --git a/pkg/domain/repository/comment.go b/pkg/domain/repository/comment.go index 9258d3f..eaee66d 100644 --- a/pkg/domain/repository/comment.go +++ b/pkg/domain/repository/comment.go @@ -10,7 +10,7 @@ import ( type CommentRepository interface { CreateComment(ctx context.Context, comment *entity.Comments) error // 新增留言 // 根據 ID 獲取單條留言 DeleteCommentByID(ctx context.Context, id string) error // 根據 ID 刪除留言 // 刪除某留言的所有子留言 - UpdateCommentMessage(ctx context.Context, id string, message string) error // 更新留言內容 + UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error // 更新留言內容 ListComments(ctx context.Context, req ListCommentRequest) ([]*entity.Comments, int64, error) // 分頁列出留言 } @@ -20,4 +20,5 @@ type ListCommentRequest struct { ReferenceID *string ParentID *string Level comment.Level + IsVisitable *bool } diff --git a/pkg/domain/usecase/comment.go b/pkg/domain/usecase/comment.go index 1d10ca1..bb5946e 100644 --- a/pkg/domain/usecase/comment.go +++ b/pkg/domain/usecase/comment.go @@ -14,7 +14,7 @@ type CommentUseCase interface { // RemoveCommentItem 移除 CommentItem 列表 RemoveCommentItem(ctx context.Context, id string) error // UpdateCommentMessage 更新 - UpdateCommentMessage(ctx context.Context, id string, message string) error + UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error } type CreateCommentDocs struct { @@ -22,6 +22,7 @@ type CreateCommentDocs struct { ReferenceID string `bson:"reference_id"` // 參考的 id ex product_id or product_item_id 之類的 ParentCommentID string `bson:"parent_comment_id"` // 父留言 id 沒有就空白 Message string `bson:"message"` // 留言內容 + IsVisitable bool `bson:"is_visitable"` // 是否可見 } type CommentDocs struct { @@ -41,4 +42,5 @@ type ListCommentRequest struct { ReferenceID *string ParentID *string IsSub bool + IsVisitable *bool } diff --git a/pkg/repository/comment.go b/pkg/repository/comment.go index a54101d..97dabf4 100644 --- a/pkg/repository/comment.go +++ b/pkg/repository/comment.go @@ -72,7 +72,7 @@ func (repo *CommentRepository) DeleteCommentByID(ctx context.Context, id string) return err } -func (repo *CommentRepository) UpdateCommentMessage(ctx context.Context, id string, message string) error { +func (repo *CommentRepository) UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error { // 驗證 ObjectID 是否有效 oid, err := primitive.ObjectIDFromHex(id) if err != nil { @@ -81,8 +81,9 @@ func (repo *CommentRepository) UpdateCommentMessage(ctx context.Context, id stri // 初始化更新字段 updates := bson.M{ - "message": message, // 更新留言內容 - "updated_at": time.Now().UTC().UnixNano(), // 更新時間 + "message": message, // 更新留言內容 + "is_visitable": isVisitable, + "updated_at": time.Now().UTC().UnixNano(), // 更新時間 } // 查詢條件:根據 _id 查詢 @@ -120,6 +121,10 @@ func (repo *CommentRepository) ListComments(ctx context.Context, req repository. filter["reference_id"] = req.ReferenceID } + if req.IsVisitable != nil { + filter["is_visitable"] = req.IsVisitable + } + // 設置排序選項 opts := options.Find().SetSkip((req.PageIndex - 1) * req.PageSize).SetLimit(req.PageSize) opts.SetSort(bson.D{{Key: "created_at", Value: -1}}) diff --git a/pkg/usecase/comment.go b/pkg/usecase/comment.go index ccbdae9..4f96b5a 100644 --- a/pkg/usecase/comment.go +++ b/pkg/usecase/comment.go @@ -37,6 +37,7 @@ func (use *CommentUseCase) CreateCommentItem(ctx context.Context, info usecase.C UID: info.UID, ReferenceID: info.ReferenceID, Level: comment.TopLevelComment, + IsVisitable: true, } snappyContent := snappy.Encode(nil, []byte(info.Message)) insert.Message = base64.StdEncoding.EncodeToString(snappyContent) @@ -97,8 +98,8 @@ func (use *CommentUseCase) GetCommentItem(ctx context.Context, id string) (*usec }, nil } -func (use *CommentUseCase) UpdateCommentMessage(ctx context.Context, id string, message string) error { - err := use.Comment.UpdateCommentMessage(ctx, id, message) +func (use *CommentUseCase) UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error { + err := use.Comment.UpdateCommentMessage(ctx, id, message, isVisitable) if err != nil { return errs.DatabaseErrorWithScopeL( code.CloudEPComment, @@ -141,6 +142,7 @@ func (use *CommentUseCase) ListComment(ctx context.Context, req usecase.ListComm PageSize: req.PageSize, PageIndex: req.PageIndex, ReferenceID: req.ReferenceID, + IsVisitable: req.IsVisitable, }) if err != nil { return nil, 0, errs.DatabaseErrorWithScopeL(