mirror of https://github.com/go-sonic/sonic.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package assembler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-sonic/sonic/model/entity"
|
|
"github.com/go-sonic/sonic/model/vo"
|
|
"github.com/go-sonic/sonic/service"
|
|
)
|
|
|
|
type SheetCommentAssembler interface {
|
|
BaseCommentAssembler
|
|
ConvertToWithSheet(ctx context.Context, comments []*entity.Comment) ([]*vo.SheetCommentWithSheet, error)
|
|
}
|
|
|
|
func NewSheetCommentAssembler(
|
|
optionService service.OptionService,
|
|
baseCommentService service.BaseCommentService,
|
|
baseCommentAssembler BaseCommentAssembler,
|
|
sheetService service.SheetService,
|
|
sheetAssembler SheetAssembler,
|
|
) SheetCommentAssembler {
|
|
return &sheetCommentAssembler{
|
|
OptionService: optionService,
|
|
BaseCommentService: baseCommentService,
|
|
BaseCommentAssembler: baseCommentAssembler,
|
|
SheetAssembler: sheetAssembler,
|
|
SheetService: sheetService,
|
|
}
|
|
}
|
|
|
|
type sheetCommentAssembler struct {
|
|
OptionService service.OptionService
|
|
BaseCommentService service.BaseCommentService
|
|
BaseCommentAssembler
|
|
SheetAssembler
|
|
SheetService service.SheetService
|
|
}
|
|
|
|
func (p *sheetCommentAssembler) ConvertToWithSheet(ctx context.Context, comments []*entity.Comment) ([]*vo.SheetCommentWithSheet, error) {
|
|
sheetIDs := make([]int32, 0, len(comments))
|
|
for _, comment := range comments {
|
|
sheetIDs = append(sheetIDs, comment.PostID)
|
|
}
|
|
sheets, err := p.SheetService.GetByPostIDs(ctx, sheetIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]*vo.SheetCommentWithSheet, 0, len(comments))
|
|
for _, comment := range comments {
|
|
commentDTO, err := p.BaseCommentAssembler.ConvertToDTO(ctx, comment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
commentWithSheet := &vo.SheetCommentWithSheet{
|
|
Comment: *commentDTO,
|
|
}
|
|
result = append(result, commentWithSheet)
|
|
sheet, ok := sheets[comment.PostID]
|
|
if ok {
|
|
commentWithSheet.PostMinimal, err = p.SheetAssembler.ConvertToMinimalDTO(ctx, sheet)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|