-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_group_repo.go
74 lines (63 loc) · 2.08 KB
/
service_group_repo.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
package orm
import (
"context"
"fmt"
"github.com/ScoreTrak/ScoreTrak/pkg/servicegroup/servicegrouprepo"
"github.com/ScoreTrak/ScoreTrak/pkg/servicegroup"
"github.com/gofrs/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type serviceGroupRepo struct {
db *gorm.DB
}
func NewServiceGroupRepo(db *gorm.DB) servicegrouprepo.Repo {
return &serviceGroupRepo{db}
}
func (s *serviceGroupRepo) Delete(ctx context.Context, id uuid.UUID) error {
result := s.db.WithContext(ctx).Delete(&servicegroup.ServiceGroup{}, "id = ?", id)
if result.Error != nil {
return fmt.Errorf("error while deleting the Service Group with id: %d, err: %w", id, result.Error)
}
if result.RowsAffected == 0 {
return &NoRowsAffected{"no model found"}
}
return nil
}
func (s *serviceGroupRepo) GetAll(ctx context.Context) ([]*servicegroup.ServiceGroup, error) {
serviceGroups := make([]*servicegroup.ServiceGroup, 0)
err := s.db.WithContext(ctx).Find(&serviceGroups).Error
if err != nil {
return nil, err
}
return serviceGroups, nil
}
func (s *serviceGroupRepo) GetByID(ctx context.Context, id uuid.UUID) (*servicegroup.ServiceGroup, error) {
sgr := &servicegroup.ServiceGroup{}
err := s.db.WithContext(ctx).Where("id = ?", id).First(sgr).Error
if err != nil {
return nil, err
}
return sgr, nil
}
func (s *serviceGroupRepo) Store(ctx context.Context, sgr *servicegroup.ServiceGroup) error {
err := s.db.WithContext(ctx).Create(sgr).Error
if err != nil {
return err
}
return nil
}
func (s *serviceGroupRepo) Upsert(ctx context.Context, sgr *servicegroup.ServiceGroup) error {
err := s.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(sgr).Error
if err != nil {
return err
}
return nil
}
func (s *serviceGroupRepo) Update(ctx context.Context, sgr *servicegroup.ServiceGroup) error {
err := s.db.WithContext(ctx).Model(sgr).Updates(servicegroup.ServiceGroup{Enabled: sgr.Enabled, DisplayName: sgr.DisplayName}).Error // Updating check_service group names is not supported because check_service group name tightly coupled with platform operations
if err != nil {
return err
}
return nil
}