diff --git a/pkg/domain/repository/comment.go b/pkg/domain/repository/comment.go index eaee66d..30b2850 100644 --- a/pkg/domain/repository/comment.go +++ b/pkg/domain/repository/comment.go @@ -12,6 +12,7 @@ type CommentRepository interface { DeleteCommentByID(ctx context.Context, id string) error // 根據 ID 刪除留言 // 刪除某留言的所有子留言 UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error // 更新留言內容 ListComments(ctx context.Context, req ListCommentRequest) ([]*entity.Comments, int64, error) // 分頁列出留言 + GetCommentByID(ctx context.Context, id string) (*entity.Comments, error) } type ListCommentRequest struct { diff --git a/pkg/repository/comment.go b/pkg/repository/comment.go index 97dabf4..93dd593 100644 --- a/pkg/repository/comment.go +++ b/pkg/repository/comment.go @@ -72,6 +72,22 @@ func (repo *CommentRepository) DeleteCommentByID(ctx context.Context, id string) return err } +func (repo *CommentRepository) GetCommentByID(ctx context.Context, id string) (*entity.Comments, error) { + oid, err := primitive.ObjectIDFromHex(id) + if err != nil { + return nil, ErrInvalidObjectID + } + + result := entity.Comments{} + rk := domain.GetCommentRedisKey(id) + err = repo.DB.FindOne(ctx, rk, &result, bson.M{"_id": oid}) + if err != nil { + return nil, err + } + + return &result, nil +} + func (repo *CommentRepository) UpdateCommentMessage(ctx context.Context, id string, message string, isVisitable bool) error { // 驗證 ObjectID 是否有效 oid, err := primitive.ObjectIDFromHex(id) diff --git a/pkg/usecase/comment.go b/pkg/usecase/comment.go index 4f96b5a..8639c4b 100644 --- a/pkg/usecase/comment.go +++ b/pkg/usecase/comment.go @@ -65,11 +65,7 @@ func (use *CommentUseCase) CreateCommentItem(ctx context.Context, info usecase.C } func (use *CommentUseCase) GetCommentItem(ctx context.Context, id string) (*usecase.CommentDocs, error) { - item, _, err := use.Comment.ListComments(ctx, repository.ListCommentRequest{ - ReferenceID: &id, - PageIndex: 1, - PageSize: 20, - }) + byID, err := use.Comment.GetCommentByID(ctx, id) if err != nil { return nil, errs.DatabaseErrorWithScopeL( code.CloudEPComment, @@ -80,21 +76,21 @@ func (use *CommentUseCase) GetCommentItem(ctx context.Context, id string) (*usec {Key: "func", Value: "Comment.ListComments"}, {Key: "err", Value: err.Error()}, }, - "failed to get comment").Wrap(err) + "failed to get byID").Wrap(err) } - decodeB64Content, _ := base64.StdEncoding.DecodeString(item[0].Message) + decodeB64Content, _ := base64.StdEncoding.DecodeString(byID.Message) snappyContent, _ := snappy.Decode(nil, decodeB64Content) - item[0].Message = string(snappyContent) + byID.Message = string(snappyContent) return &usecase.CommentDocs{ - ID: item[0].ID.Hex(), - UID: item[0].UID, - ReferenceID: item[0].ReferenceID, - ParentCommentID: item[0].ParentCommentID, - Message: item[0].Message, - UpdatedAt: item[0].UpdatedAt, - CreatedAt: item[0].CreatedAt, + ID: byID.ID.Hex(), + UID: byID.UID, + ReferenceID: byID.ReferenceID, + ParentCommentID: byID.ParentCommentID, + Message: byID.Message, + UpdatedAt: byID.UpdatedAt, + CreatedAt: byID.CreatedAt, }, nil }