-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat_interactor.go
108 lines (81 loc) · 2.65 KB
/
chat_interactor.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package chatty
import (
"github.com/pborman/uuid"
"github.com/ryan-berger/chatty/connection"
"github.com/ryan-berger/chatty/repositories"
)
type chatInteractor struct {
conversationRepo repositories.ConversationRepo
messageRepo repositories.MessageRepo
conversantRepo repositories.ConversantRepo
}
func (chat *chatInteractor) CreateConversation(request connection.CreateConversationRequest) (*repositories.Conversation, error) {
request.Conversants = append(request.Conversants, request.SenderID)
err := request.Validate()
if err != nil {
return nil, err
}
newConversation := repositories.Conversation{}
for _, conversantID := range request.Conversants {
newConversation.Conversants = append(newConversation.Conversants, repositories.Conversant{ID: conversantID})
}
newConversation.Name = request.Name
newConversation.Direct = len(newConversation.Conversants) == 2
convo, err := chat.conversationRepo.CreateConversation(newConversation)
if err != nil {
return nil, err
}
return convo, nil
}
func (chat *chatInteractor) GetConversation(request connection.RetrieveConversationRequest) (*repositories.Conversation, error) {
err := request.Validate()
if err != nil {
return nil, err
}
conversation, err := chat.conversationRepo.RetrieveConversation(request.ConversationID, request.Limit, request.Offset)
if err != nil {
return nil, err
}
return conversation, nil
}
func (chat *chatInteractor) SendMessage(message connection.SendMessageRequest) (*repositories.Message, error) {
err := message.Validate()
if err != nil {
return nil, err
}
msg := repositories.Message{
ID: uuid.New(),
Message: message.Message,
SenderID: message.SenderID,
ConversationID: message.ConversationID,
}
newMessage, err := chat.messageRepo.CreateMessage(msg)
if err != nil {
return nil, err
}
return newMessage, nil
}
func (chat *chatInteractor) GetConversants(conversationID string) ([]repositories.Conversant, error) {
conversants, err := chat.conversationRepo.GetConversants(conversationID)
if err != nil {
return nil, err
}
return conversants, nil
}
func (chat *chatInteractor) UpsertConvserant(conversant repositories.Conversant) (*repositories.Conversant, error) {
newConversant, err := chat.conversantRepo.UpdateOrCreate(conversant)
if err != nil {
return nil, err
}
return newConversant, nil
}
func newChatInteractor(
messageRepo repositories.MessageRepo,
conversationRepo repositories.ConversationRepo,
conversantRepo repositories.ConversantRepo) *chatInteractor {
return &chatInteractor{
conversationRepo: conversationRepo,
messageRepo: messageRepo,
conversantRepo: conversantRepo,
}
}