/
system-notification-template.go
134 lines (114 loc) · 4.81 KB
/
system-notification-template.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
package repository
import (
"context"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/pkg/log"
)
// Interfaces
type ISystemNotificationTemplateRepository interface {
Get(ctx context.Context, systemNotificationTemplateId uuid.UUID) (model.SystemNotificationTemplate, error)
GetByName(ctx context.Context, name string) (model.SystemNotificationTemplate, error)
Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error)
FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (out []model.SystemNotificationTemplate, err error)
Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplateId uuid.UUID, err error)
Update(ctx context.Context, dto model.SystemNotificationTemplate) (err error)
Delete(ctx context.Context, dto model.SystemNotificationTemplate) (err error)
UpdateOrganizations(ctx context.Context, systemNotificationTemplateId uuid.UUID, organizations []model.Organization) (err error)
}
type SystemNotificationTemplateRepository struct {
db *gorm.DB
}
func NewSystemNotificationTemplateRepository(db *gorm.DB) ISystemNotificationTemplateRepository {
return &SystemNotificationTemplateRepository{
db: db,
}
}
// Logics
func (r *SystemNotificationTemplateRepository) Get(ctx context.Context, systemNotificationTemplateId uuid.UUID) (out model.SystemNotificationTemplate, err error) {
res := r.db.WithContext(ctx).Preload(clause.Associations).First(&out, "id = ?", systemNotificationTemplateId)
if res.Error != nil {
return out, res.Error
}
return
}
func (r *SystemNotificationTemplateRepository) GetByName(ctx context.Context, name string) (out model.SystemNotificationTemplate, err error) {
res := r.db.WithContext(ctx).Preload(clause.Associations).First(&out, "name = ?", name)
if res.Error != nil {
return out, res.Error
}
return
}
func (r *SystemNotificationTemplateRepository) Fetch(ctx context.Context, pg *pagination.Pagination) (out []model.SystemNotificationTemplate, err error) {
if pg == nil {
pg = pagination.NewPagination(nil)
}
_, res := pg.Fetch(r.db.WithContext(ctx).Preload(clause.Associations), &out)
if res.Error != nil {
return nil, res.Error
}
return
}
func (r *SystemNotificationTemplateRepository) FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (out []model.SystemNotificationTemplate, err error) {
if pg == nil {
pg = pagination.NewPagination(nil)
}
_, res := pg.Fetch(
r.db.WithContext(ctx).Preload(clause.Associations).
Joins("JOIN system_notification_template_organizations ON system_notification_template_organizations.system_notification_template_id = system_notification_templates.id AND system_notification_template_organizations.organization_id = ?", organizationId),
&out)
if res.Error != nil {
return nil, res.Error
}
return
}
func (r *SystemNotificationTemplateRepository) Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplateId uuid.UUID, err error) {
dto.ID = uuid.New()
res := r.db.WithContext(ctx).Create(&dto)
if res.Error != nil {
return uuid.Nil, res.Error
}
err = r.db.WithContext(ctx).Model(&dto).Association("MetricParameters").Replace(dto.MetricParameters)
if err != nil {
log.Error(ctx, err)
}
return dto.ID, nil
}
func (r *SystemNotificationTemplateRepository) Update(ctx context.Context, dto model.SystemNotificationTemplate) (err error) {
res := r.db.WithContext(ctx).Model(&model.SystemNotificationTemplate{}).
Where("id = ?", dto.ID).
Updates(map[string]interface{}{
"Name": dto.Name,
"Description": dto.Description,
"MetricQuery": dto.MetricQuery,
})
if res.Error != nil {
return res.Error
}
if err = r.db.WithContext(ctx).Model(&dto).Association("MetricParameters").Replace(dto.MetricParameters); err != nil {
return err
}
return nil
}
func (r *SystemNotificationTemplateRepository) Delete(ctx context.Context, dto model.SystemNotificationTemplate) (err error) {
res := r.db.WithContext(ctx).Delete(&model.SystemNotificationTemplate{}, "id = ?", dto.ID)
if res.Error != nil {
return res.Error
}
return nil
}
func (r *SystemNotificationTemplateRepository) UpdateOrganizations(ctx context.Context, systemNotificationTemplateId uuid.UUID, organizations []model.Organization) (err error) {
var systemNotificationTemplate = model.SystemNotificationTemplate{}
res := r.db.WithContext(ctx).Preload("Organizations").First(&systemNotificationTemplate, "id = ?", systemNotificationTemplateId)
if res.Error != nil {
return res.Error
}
err = r.db.WithContext(ctx).Model(&systemNotificationTemplate).Association("Organizations").Replace(organizations)
if err != nil {
return err
}
return nil
}