/
communicationmethod.go
85 lines (69 loc) · 3.99 KB
/
communicationmethod.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
package communicationmethod
import (
"context"
"database/sql"
"github.com/bungysheep/contact-management/pkg/common/constant/messagecode"
communicationmethodmodel "github.com/bungysheep/contact-management/pkg/models/v1/communicationmethod"
messagemodel "github.com/bungysheep/contact-management/pkg/models/v1/message"
communicationmethodrepository "github.com/bungysheep/contact-management/pkg/repository/v1/communicationmethod"
communicationmethodfieldrepository "github.com/bungysheep/contact-management/pkg/repository/v1/communicationmethodfield"
communicationmethodlabelrepository "github.com/bungysheep/contact-management/pkg/repository/v1/communicationmethodlabel"
contactsystemrepository "github.com/bungysheep/contact-management/pkg/repository/v1/contactsystem"
)
// ICommunicationMethodService - Communication Method service interface
type ICommunicationMethodService interface {
DoRead(context.Context, string, string) (*communicationmethodmodel.CommunicationMethod, messagemodel.IMessage)
DoReadAll(context.Context, string) ([]*communicationmethodmodel.CommunicationMethod, messagemodel.IMessage)
DoSave(context.Context, *communicationmethodmodel.CommunicationMethod) messagemodel.IMessage
DoDelete(context.Context, string, string) messagemodel.IMessage
}
type communicationMethodService struct {
communicationMethodRepo communicationmethodrepository.ICommunicationMethodRepository
communicationMethodFieldRepo communicationmethodfieldrepository.ICommunicationMethodFieldRepository
communicationMethodLabelRepo communicationmethodlabelrepository.ICommunicationMethodLabelRepository
contactSystemRepo contactsystemrepository.IContactSystemRepository
}
// NewCommunicationMethodService - Communication Method service implementation
func NewCommunicationMethodService(db *sql.DB) ICommunicationMethodService {
return &communicationMethodService{
communicationMethodRepo: communicationmethodrepository.NewCommunicationMethodRepository(db),
communicationMethodFieldRepo: communicationmethodfieldrepository.NewCommunicationMethodFieldRepository(db),
communicationMethodLabelRepo: communicationmethodlabelrepository.NewCommunicationMethodLabelRepository(db),
contactSystemRepo: contactsystemrepository.NewContactSystemRepository(db),
}
}
func (cm *communicationMethodService) DoRead(ctx context.Context, contactSystemCode string, communicationMethodCode string) (*communicationmethodmodel.CommunicationMethod, messagemodel.IMessage) {
return cm.communicationMethodRepo.DoRead(ctx, contactSystemCode, communicationMethodCode)
}
func (cm *communicationMethodService) DoReadAll(ctx context.Context, contactSystemCode string) ([]*communicationmethodmodel.CommunicationMethod, messagemodel.IMessage) {
return cm.communicationMethodRepo.DoReadAll(ctx, contactSystemCode)
}
func (cm *communicationMethodService) DoSave(ctx context.Context, data *communicationmethodmodel.CommunicationMethod) messagemodel.IMessage {
if err := data.DoValidate(); err != nil {
return err
}
if err := cm.DoValidate(ctx, data); err != nil {
return err
}
if err := cm.communicationMethodRepo.DoUpdate(ctx, data); err != nil {
if err.Code() == messagecode.NotFound {
return cm.communicationMethodRepo.DoInsert(ctx, data)
}
}
return nil
}
func (cm *communicationMethodService) DoDelete(ctx context.Context, contactSystemCode string, communicationMethodCode string) messagemodel.IMessage {
if err := cm.communicationMethodFieldRepo.DoDeleteAll(ctx, contactSystemCode, communicationMethodCode); err != nil {
return err
}
if err := cm.communicationMethodLabelRepo.DoDeleteAll(ctx, contactSystemCode, communicationMethodCode); err != nil {
return err
}
return cm.communicationMethodRepo.DoDelete(ctx, contactSystemCode, communicationMethodCode)
}
func (cm *communicationMethodService) DoValidate(ctx context.Context, data *communicationmethodmodel.CommunicationMethod) messagemodel.IMessage {
if _, err := cm.contactSystemRepo.DoRead(ctx, data.GetContactSystemCode()); err != nil {
return err
}
return nil
}