|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/codepzj/Stellux-Server/internal/comment/internal/domain" |
| 7 | + "github.com/codepzj/Stellux-Server/internal/comment/internal/repository/dao" |
| 8 | + "github.com/samber/lo" |
| 9 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 10 | +) |
| 11 | + |
| 12 | +type ICommentRepository interface { |
| 13 | + Create(ctx context.Context, comment *domain.Comment) error |
| 14 | + GetListByPath(ctx context.Context, path string) ([]*domain.CommentShow, error) |
| 15 | + Update(ctx context.Context, comment *domain.Comment) error |
| 16 | + Delete(ctx context.Context, id bson.ObjectID) error |
| 17 | +} |
| 18 | + |
| 19 | +var _ ICommentRepository = (*CommentRepository)(nil) |
| 20 | + |
| 21 | +func NewCommentRepository(dao dao.ICommentDao) *CommentRepository { |
| 22 | + return &CommentRepository{dao: dao} |
| 23 | +} |
| 24 | + |
| 25 | +type CommentRepository struct { |
| 26 | + dao dao.ICommentDao |
| 27 | +} |
| 28 | + |
| 29 | +// Create 创建评论 |
| 30 | +func (r *CommentRepository) Create(ctx context.Context, comment *domain.Comment) error { |
| 31 | + return r.dao.Create(ctx, &dao.Comment{ |
| 32 | + Path: comment.Path, |
| 33 | + Content: comment.Content, |
| 34 | + RootId: comment.RootId, |
| 35 | + ParentId: comment.ParentId, |
| 36 | + Nickname: comment.Nickname, |
| 37 | + Avatar: comment.Avatar, |
| 38 | + Email: comment.Email, |
| 39 | + SiteUrl: comment.SiteUrl, |
| 40 | + IsAdmin: comment.IsAdmin, |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +// GetListByPath 根据路径获取评论列表 |
| 45 | +func (r *CommentRepository) GetListByPath(ctx context.Context, path string) ([]*domain.CommentShow, error) { |
| 46 | + comments, err := r.dao.GetList(ctx, bson.D{{Key: "path", Value: path}}) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + return r.CommentDaoToShowDomainList(comments), nil |
| 51 | +} |
| 52 | + |
| 53 | +// Update 更新评论 |
| 54 | +func (r *CommentRepository) Update(ctx context.Context, comment *domain.Comment) error { |
| 55 | + return r.dao.Update(ctx, comment.Id, &dao.Comment{ |
| 56 | + Content: comment.Content, |
| 57 | + Nickname: comment.Nickname, |
| 58 | + Avatar: comment.Avatar, |
| 59 | + SiteUrl: comment.SiteUrl, |
| 60 | + IsAdmin: comment.IsAdmin, |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +// Delete 删除评论 |
| 65 | +func (r *CommentRepository) Delete(ctx context.Context, id bson.ObjectID) error { |
| 66 | + return r.dao.Delete(ctx, id) |
| 67 | +} |
| 68 | + |
| 69 | +func (r *CommentRepository) CommentDaoToShowDomainList(comments []*dao.Comment) []*domain.CommentShow { |
| 70 | + return lo.Map(comments, func(comment *dao.Comment, _ int) *domain.CommentShow { |
| 71 | + return r.CommentDaoToShowDomain(comment) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +// 转成前端展示的评论 |
| 76 | +func (r *CommentRepository) CommentDaoToShowDomain(comment *dao.Comment) *domain.CommentShow { |
| 77 | + return &domain.CommentShow{ |
| 78 | + Id: comment.ID, |
| 79 | + CreatedAt: comment.CreatedAt, |
| 80 | + UpdatedAt: comment.UpdatedAt, |
| 81 | + DeletedAt: comment.DeletedAt, |
| 82 | + Path: comment.Path, |
| 83 | + Content: comment.Content, |
| 84 | + Nickname: comment.Nickname, |
| 85 | + Avatar: comment.Avatar, |
| 86 | + SiteUrl: comment.SiteUrl, |
| 87 | + IsAdmin: comment.IsAdmin, |
| 88 | + } |
| 89 | +} |
0 commit comments