Dawid Wysokiński
ad9673cef5
All checks were successful
continuous-integration/drone/push Build is passing
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package msg
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/msg/internal/model"
|
|
"github.com/ThreeDotsLabs/watermill/message"
|
|
)
|
|
|
|
type SnapshotPublisher struct {
|
|
publisher message.Publisher
|
|
marshaler CommandEventMarshaler
|
|
}
|
|
|
|
func NewSnapshotPublisher(publisher message.Publisher, marshaler CommandEventMarshaler) *SnapshotPublisher {
|
|
return &SnapshotPublisher{publisher: publisher, marshaler: marshaler}
|
|
}
|
|
|
|
func (s *SnapshotPublisher) CmdCreatePlayers(ctx context.Context, payloads ...domain.CreateSnapshotsCmdPayload) error {
|
|
return s.publishCmdCreate(ctx, playersCmdCreateSnapshots, payloads...)
|
|
}
|
|
|
|
func (s *SnapshotPublisher) CmdCreateTribes(ctx context.Context, payloads ...domain.CreateSnapshotsCmdPayload) error {
|
|
return s.publishCmdCreate(ctx, tribesCmdCreateSnapshots, payloads...)
|
|
}
|
|
|
|
func (s *SnapshotPublisher) publishCmdCreate(
|
|
ctx context.Context,
|
|
topic string,
|
|
payloads ...domain.CreateSnapshotsCmdPayload,
|
|
) error {
|
|
msgs := make([]*message.Message, 0, len(payloads))
|
|
for _, p := range payloads {
|
|
msg, err := s.marshaler.Marshal(model.CreateSnapshotsCmdPayload(p))
|
|
if err != nil {
|
|
return fmt.Errorf("%s: couldn't marshal CreateSnapshotsCmdPayload: %w", p.Key, err)
|
|
}
|
|
|
|
msg.SetContext(ctx)
|
|
|
|
msgs = append(msgs, msg)
|
|
}
|
|
|
|
if err := s.publisher.Publish(topic, msgs...); err != nil {
|
|
return fmt.Errorf("%s: publisher.Publish: %w", topic, err)
|
|
}
|
|
|
|
return nil
|
|
}
|