-
Notifications
You must be signed in to change notification settings - Fork 7
/
account.go
166 lines (129 loc) · 3.81 KB
/
account.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package service
import (
"errors"
"strconv"
"github.com/asaskevich/govalidator"
"github.com/herwonowr/slackhell/internal/repository"
)
func (s *internalService) GetAccountByID(id int) (*repository.Account, error) {
if govalidator.IsNull(strconv.Itoa(id)) {
return nil, errors.New("invalid account id")
}
return s.repository.GetAccountByID(id)
}
func (s *internalService) GetAccountBySlackID(slackID string) (*repository.Account, error) {
if govalidator.IsNull(slackID) {
return nil, errors.New("invalid account Slack id")
}
return s.repository.GetAccountBySlackID(slackID)
}
func (s *internalService) GetAccountBySlackRealName(slackRealName string) (*repository.Account, error) {
if govalidator.IsNull(slackRealName) {
return nil, errors.New("invalid account slack real name")
}
return s.repository.GetAccountBySlackRealName(slackRealName)
}
func (s *internalService) GetAccounts() ([]repository.Account, error) {
return s.repository.GetAccounts()
}
func (s *internalService) CreateAccount(role repository.AccountRole, slackID string, slackRealName string) error {
if govalidator.IsNull(slackID) {
return errors.New("invalid account slack id")
}
if govalidator.IsNull(slackRealName) {
return errors.New("invalid account slack real name")
}
aSlackID, err := s.repository.GetAccountBySlackID(slackID)
if err != nil && err.Error() != "account not found" {
return err
}
if aSlackID != nil {
return errors.New("account slack id already exists")
}
aSlackRealName, err := s.repository.GetAccountBySlackRealName(slackRealName)
if err != nil && err.Error() != "account not found" {
return err
}
if aSlackRealName != nil {
return errors.New("account slack real name already exists")
}
vRole := repository.AgentRole
if !(role == repository.AdminRole || role == repository.AgentRole) {
return errors.New("invalid account role")
}
if role == repository.AdminRole {
vRole = repository.AdminRole
}
if role == repository.AgentRole {
vRole = repository.AgentRole
}
account := &repository.Account{
Role: vRole,
SlackID: slackID,
SlackRealName: slackRealName,
}
err = s.repository.CreateAccount(account)
if err != nil {
return err
}
return nil
}
func (s *internalService) PutAccount(id int, role repository.AccountRole, slackID string, slackRealName string) error {
if govalidator.IsNull(strconv.Itoa(id)) {
return errors.New("invalid account id")
}
account, err := s.repository.GetAccountByID(id)
if err != nil {
return err
}
vRole := account.Role
vSlackID := account.SlackID
vSlackRealName := account.SlackRealName
if role != 0 {
if !(role == repository.AdminRole || role == repository.AgentRole) {
return errors.New("invalid account role")
}
if role == repository.AdminRole {
vRole = repository.AdminRole
}
if role == repository.AgentRole {
vRole = repository.AgentRole
}
}
if !govalidator.IsNull(slackID) {
aSlackID, err := s.repository.GetAccountBySlackID(slackID)
if err != nil {
return err
}
if aSlackID != nil && aSlackID.ID != account.ID {
return errors.New("slack id already exists")
}
vSlackID = slackID
}
if !govalidator.IsNull(slackRealName) {
aSlackRealName, err := s.repository.GetAccountBySlackRealName(slackRealName)
if err != nil {
return err
}
if aSlackRealName != nil && aSlackRealName.ID != account.ID {
return errors.New("account already exists")
}
vSlackRealName = slackRealName
}
data := &repository.Account{
ID: account.ID,
Role: vRole,
SlackID: vSlackID,
SlackRealName: vSlackRealName,
}
if err := s.repository.PutAccount(data); err != nil {
return err
}
return nil
}
func (s *internalService) DeleteAccount(id int) error {
if govalidator.IsNull(strconv.Itoa(id)) {
return errors.New("invalid account id")
}
return s.repository.DeleteAccount(id)
}