Skip to content

Commit e4dce17

Browse files
祖轩祖轩
authored andcommitted
✨ Refactor subscription service to use RequestSubscribeCreate type and improve create logic
1 parent 5425cf9 commit e4dce17

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

internal/repo/subscribe.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package repo
22

33
import (
4-
"gorm.io/gorm"
54
"watchAlert/internal/models"
5+
6+
"gorm.io/gorm"
67
)
78

89
type (

internal/services/subscribe.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package services
22

33
import (
44
"fmt"
5-
"gorm.io/gorm"
65
"time"
76
"watchAlert/internal/ctx"
87
"watchAlert/internal/models"
98
"watchAlert/internal/types"
109
"watchAlert/pkg/tools"
10+
11+
"gorm.io/gorm"
1112
)
1213

1314
type (
@@ -50,7 +51,7 @@ func (s alertSubscribeService) Get(req interface{}) (interface{}, interface{}) {
5051
}
5152

5253
func (s alertSubscribeService) Create(req interface{}) (interface{}, interface{}) {
53-
r := req.(*models.AlertSubscribe)
54+
r := req.(*types.RequestSubscribeCreate)
5455
_, b, err := s.ctx.DB.Subscribe().Get(r.STenantId, "", r.SUserId, r.SRuleId)
5556
if err != nil && err != gorm.ErrRecordNotFound {
5657
return nil, err
@@ -59,9 +60,22 @@ func (s alertSubscribeService) Create(req interface{}) (interface{}, interface{}
5960
return nil, fmt.Errorf("用户已订阅该规则, 请勿重复创建!")
6061
}
6162

62-
r.SId = "as-" + tools.RandId()
63-
r.SCreateAt = time.Now().Unix()
64-
err = s.ctx.DB.Subscribe().Create(*r)
63+
subscribe := models.AlertSubscribe{
64+
SId: "as-" + tools.RandId(),
65+
STenantId: r.STenantId,
66+
SUserId: r.SUserId,
67+
SUserEmail: r.SUserEmail,
68+
SRuleId: r.SRuleId,
69+
SRuleName: r.SRuleName,
70+
SRuleType: r.SRuleType,
71+
SRuleSeverity: r.SRuleSeverity,
72+
SNoticeSubject: r.SNoticeSubject,
73+
SNoticeTemplateId: r.SNoticeTemplateId,
74+
SFilter: r.SFilter,
75+
SCreateAt: time.Now().Unix(),
76+
}
77+
78+
err = s.ctx.DB.Subscribe().Create(subscribe)
6579
if err != nil {
6680
return nil, err
6781
}

internal/types/subscribe.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
package types
22

33
type RequestSubscribeCreate struct {
4-
STenantId string `json:"sTenantId"` // 订阅规则的租户
5-
SUserId string `json:"sUserId"` // 订阅的用户 ID
6-
SUserEmail string `json:"sUserEmail"` // 订阅的用户邮箱
7-
SRuleId string `json:"sRuleId"` // 订阅的规则 ID
8-
SRuleName string `json:"sRuleName"` // 订阅的规则名称
9-
SRuleType string `json:"sRuleType"` // 订阅的规则类型
10-
SRuleSeverity []string `json:"sRuleSeverity" gorm:"sRuleSeverity;serializer:json"` // 订阅的告警等级
11-
SNoticeSubject string `json:"sNoticeSubject"` // 发布订阅消息的 Title
12-
SNoticeTemplateId string `json:"sNoticeTemplateId"` // 发送订阅消息的通知模版 ID
13-
SFilter []string `json:"sFilter" gorm:"sFilter;serializer:json"` // 过滤
14-
SCreateAt int64 `json:"sCreateAt"`
15-
}
16-
17-
type RequestSubscribeUpdate struct {
184
SId string `json:"sId"` // 订阅规则ID
195
STenantId string `json:"sTenantId"` // 订阅规则的租户
206
SUserId string `json:"sUserId"` // 订阅的用户 ID

0 commit comments

Comments
 (0)