-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
94 lines (81 loc) · 2.06 KB
/
service.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
package designation
import (
"github.com/mrexmelle/connect-org/internal/config"
"github.com/mrexmelle/connect-org/internal/localerror"
"github.com/mrexmelle/connect-org/internal/role"
)
type Service struct {
ConfigService *config.Service
DesignationRepository Repository
RoleRepository role.Repository
}
func NewService(
cfg *config.Service,
r Repository,
rr role.Repository,
) *Service {
return &Service{
ConfigService: cfg,
DesignationRepository: r,
RoleRepository: rr,
}
}
func (s *Service) Create(req PostRequestDto) (*Entity, error) {
data, err := s.DesignationRepository.FindByNodeIdAndRoleId(
req.NodeId,
req.RoleId,
)
if err != nil {
data = []Entity{}
}
newEntity := &Entity{
NodeId: req.NodeId,
RoleId: req.RoleId,
Ehid: req.Ehid,
}
for i := range data {
if data[i].Ehid == req.Ehid &&
data[i].NodeId == req.NodeId &&
data[i].RoleId == req.RoleId {
newEntity.Id = data[i].Id
return newEntity, nil
}
}
maxCount := s.RoleRepository.CountById(req.RoleId)
if (int64)(len(data)) >= maxCount {
return nil, localerror.ErrAlreadyMax
}
result, err := s.DesignationRepository.Create(newEntity)
if err != nil {
return nil, err
}
return result, nil
}
func (s *Service) RetrieveById(id string) (*Entity, error) {
result, err := s.DesignationRepository.FindById(id)
if err != nil {
return nil, err
}
return result, nil
}
func (s *Service) RetrieveByNodeId(nodeId string) ([]Entity, error) {
result, err := s.DesignationRepository.FindByNodeId(nodeId)
if err != nil {
return []Entity{}, err
}
return result, nil
}
func (s *Service) RetrieveByNodeIdAndRoleId(nodeId string, roleId string) ([]Entity, error) {
result, err := s.DesignationRepository.FindByNodeIdAndRoleId(nodeId, roleId)
if err != nil {
return []Entity{}, err
}
return result, nil
}
func (s *Service) UpdateById(fields map[string]interface{}, id string) error {
return s.DesignationRepository.UpdateById(fields, id)
}
func (s *Service) DeleteById(id string) error {
err := s.DesignationRepository.DeleteById(id)
return err
}