/
server_iplist_svc.go
59 lines (54 loc) · 2.11 KB
/
server_iplist_svc.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
package service
import (
"context"
"fmt"
"github.com/lidenger/otpserver/internal/model"
"github.com/lidenger/otpserver/internal/param"
"github.com/lidenger/otpserver/internal/store"
"github.com/lidenger/otpserver/pkg/otperr"
"github.com/lidenger/otpserver/pkg/util"
)
type ServerIpListSvc struct {
Store store.ServerIpListStore // 主存储
StoreBackup store.ServerIpListStore // 备存储
StoreMemory store.ServerIpListStore // 内存存储
storeDetectionEventChan chan<- struct{}
}
func (s *ServerIpListSvc) GetBySign(ctx context.Context, serverSign string) ([]*model.ServerIpListModel, error) {
var err error
p := ¶m.ServerIpListParam{Sign: serverSign}
data, err := MultiStoreSelectByCondition[*param.ServerIpListParam, *model.ServerIpListModel](ctx, s.storeDetectionEventChan, p, s.StoreMemory, s.Store, s.StoreBackup)
if err != nil {
return nil, err
}
return data, err
}
func (s *ServerIpListSvc) Add(ctx context.Context, p *param.ServerIpListParam) error {
ips, err := MultiStoreSelectByCondition[*param.ServerIpListParam, *model.ServerIpListModel](ctx, s.storeDetectionEventChan, p, s.StoreMemory, s.Store, s.StoreBackup)
if err != nil {
return err
}
if len(ips) != 0 {
msg := fmt.Sprintf("服务:%s,IP:%s已存在不能重复添加", p.Sign, p.IP)
return otperr.ErrRepeatAdd(msg)
}
m := &model.ServerIpListModel{
ServerSign: p.Sign,
IP: p.IP,
}
err = MultiStoreInsert[*model.ServerIpListModel](ctx, s.storeDetectionEventChan, m, s.Store, s.StoreBackup, s.StoreMemory)
return err
}
func (s *ServerIpListSvc) Remove(ctx context.Context, p *param.ServerIpListParam) error {
ips, err := MultiStoreSelectByCondition[*param.ServerIpListParam, *model.ServerIpListModel](ctx, s.storeDetectionEventChan, p, s.StoreMemory, s.Store, s.StoreBackup)
if err != nil {
return err
}
if len(ips) == 0 {
msg := fmt.Sprintf("服务:%s,IP:%s不存在无法删除", p.Sign, p.IP)
return otperr.ErrParamIllegal(msg)
}
m := util.GetArrFirstItem(ips)
err = MultiStoreDelete(ctx, s.storeDetectionEventChan, m.ID, s.Store, s.StoreBackup, s.StoreMemory)
return err
}