-
Notifications
You must be signed in to change notification settings - Fork 3
/
posting.go
43 lines (37 loc) · 945 Bytes
/
posting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package usecase
import (
"context"
"github.com/morikuni/chat/src/application"
"github.com/morikuni/chat/src/domain/model"
"github.com/morikuni/chat/src/domain/model/aggregate"
"github.com/morikuni/chat/src/domain/repository"
"github.com/morikuni/chat/src/infra"
)
type Posting interface {
PostChat(ctx context.Context, message string) error
}
func NewPosting(
chatRepository repository.Chat,
clock infra.Clock,
) Posting {
return posting{
chatRepository,
clock,
}
}
type posting struct {
chatRepository repository.Chat
clock infra.Clock
}
func (p posting) PostChat(ctx context.Context, message string) error {
cm, verr := model.ValidateChatMessage(message)
if verr != nil {
return application.TranslateValidationError(verr, "message")
}
id, err := p.chatRepository.GenerateID(ctx)
if err != nil {
return err
}
chat := aggregate.NewChat(id, cm, p.clock.Now())
return p.chatRepository.Save(ctx, chat)
}