-
Notifications
You must be signed in to change notification settings - Fork 462
/
SMTPNotificationRepository.go
109 lines (95 loc) · 3.66 KB
/
SMTPNotificationRepository.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
package repository
import (
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
)
type SMTPNotificationRepository interface {
FindOne(id int) (*SMTPConfig, error)
UpdateSMTPConfig(smtpConfig *SMTPConfig) (*SMTPConfig, error)
SaveSMTPConfig(smtpConfig *SMTPConfig) (*SMTPConfig, error)
FindAll() ([]*SMTPConfig, error)
FindByIdsIn(ids []int) ([]*SMTPConfig, error)
FindByTeamIdOrOwnerId(ownerId int32) ([]*SMTPConfig, error)
UpdateSMTPConfigDefault() (bool, error)
FindByIds(ids []*int) ([]*SMTPConfig, error)
FindDefault() (*SMTPConfig, error)
MarkSMTPConfigDeleted(smtpConfig *SMTPConfig) error
}
type SMTPNotificationRepositoryImpl struct {
dbConnection *pg.DB
}
func NewSMTPNotificationRepositoryImpl(dbConnection *pg.DB) *SMTPNotificationRepositoryImpl {
return &SMTPNotificationRepositoryImpl{dbConnection: dbConnection}
}
type SMTPConfig struct {
tableName struct{} `sql:"smtp_config" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Port string `sql:"port"`
Host string `sql:"host"`
AuthType string `sql:"auth_type"`
AuthUser string `sql:"auth_user"`
AuthPassword string `sql:"auth_password"`
FromEmail string `sql:"from_email"`
ConfigName string `sql:"config_name"`
Description string `sql:"description"`
OwnerId int32 `sql:"owner_id"`
Default bool `sql:"default,notnull"`
Deleted bool `sql:"deleted,notnull"`
sql.AuditLog
}
func (impl *SMTPNotificationRepositoryImpl) FindByIdsIn(ids []int) ([]*SMTPConfig, error) {
var configs []*SMTPConfig
err := impl.dbConnection.Model(&configs).
Where("id in (?)", pg.In(ids)).
Where("deleted = ?", false).
Select()
return configs, err
}
func (impl *SMTPNotificationRepositoryImpl) FindOne(id int) (*SMTPConfig, error) {
details := &SMTPConfig{}
err := impl.dbConnection.Model(details).Where("id = ?", id).
Where("deleted = ?", false).Select()
return details, err
}
func (impl *SMTPNotificationRepositoryImpl) FindAll() ([]*SMTPConfig, error) {
var smtpConfigs []*SMTPConfig
err := impl.dbConnection.Model(&smtpConfigs).
Where("deleted = ?", false).Select()
return smtpConfigs, err
}
func (impl *SMTPNotificationRepositoryImpl) FindByTeamIdOrOwnerId(ownerId int32) ([]*SMTPConfig, error) {
var smtpConfigs []*SMTPConfig
err := impl.dbConnection.Model(&smtpConfigs).Where(`owner_id = ?`, ownerId).
Where("deleted = ?", false).Select()
return smtpConfigs, err
}
func (impl *SMTPNotificationRepositoryImpl) UpdateSMTPConfig(smtpConfig *SMTPConfig) (*SMTPConfig, error) {
return smtpConfig, impl.dbConnection.Update(smtpConfig)
}
func (impl *SMTPNotificationRepositoryImpl) SaveSMTPConfig(smtpConfig *SMTPConfig) (*SMTPConfig, error) {
return smtpConfig, impl.dbConnection.Insert(smtpConfig)
}
func (impl *SMTPNotificationRepositoryImpl) UpdateSMTPConfigDefault() (bool, error) {
SMTPConfigs, err := impl.FindAll()
for _, SMTPConfig := range SMTPConfigs {
SMTPConfig.Default = false
err = impl.dbConnection.Update(SMTPConfig)
}
return true, err
}
func (impl *SMTPNotificationRepositoryImpl) FindByIds(ids []*int) ([]*SMTPConfig, error) {
var objects []*SMTPConfig
err := impl.dbConnection.Model(&objects).Where("id in (?)", pg.In(ids)).
Where("deleted = ?", false).Select()
return objects, err
}
func (impl *SMTPNotificationRepositoryImpl) FindDefault() (*SMTPConfig, error) {
details := &SMTPConfig{}
err := impl.dbConnection.Model(details).Where("smtp_config.default = ?", true).
Where("deleted = ?", false).Select()
return details, err
}
func (impl *SMTPNotificationRepositoryImpl) MarkSMTPConfigDeleted(smtpConfig *SMTPConfig) error {
smtpConfig.Deleted = true
return impl.dbConnection.Update(smtpConfig)
}