-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_repo.go
107 lines (91 loc) · 2.96 KB
/
check_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
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
package orm
import (
"context"
"fmt"
"strings"
"github.com/ScoreTrak/ScoreTrak/pkg/check"
"github.com/ScoreTrak/ScoreTrak/pkg/check/checkrepo"
"github.com/ScoreTrak/ScoreTrak/pkg/storage/orm/testutil"
"github.com/gofrs/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type checkRepo struct {
db *gorm.DB
}
func NewCheckRepo(db *gorm.DB) checkrepo.Repo {
return &checkRepo{db}
}
func (c *checkRepo) GetAllByRoundID(ctx context.Context, roundID uint64) ([]*check.Check, error) {
var checks []*check.Check
err := c.db.WithContext(ctx).Where("round_id = ?", roundID).Find(&checks).Error
return checks, err
}
func (c *checkRepo) GetAllByServiceID(ctx context.Context, serviceID uuid.UUID) ([]*check.Check, error) {
var checks []*check.Check
err := c.db.WithContext(ctx).Where("service_id = ?", serviceID).Find(&checks).Error
return checks, err
}
func (c *checkRepo) GetByRoundServiceID(ctx context.Context, roundID uint64, serviceID uuid.UUID) (*check.Check, error) {
chk := &check.Check{}
err := c.db.WithContext(ctx).Where("round_id = ?", roundID).Where("service_id = ?", serviceID).First(&chk).Error
if err != nil {
return nil, err
}
return chk, err
}
func (c *checkRepo) Delete(ctx context.Context, roundID uint64, serviceID uuid.UUID) error {
result := c.db.WithContext(ctx).Where("round_id = ?", roundID).Where("service_id = ?", serviceID).Delete(&check.Check{})
if result.Error != nil {
return fmt.Errorf("error while deleting the check with round id: %d, service id: %d, err: %w", roundID, serviceID, result.Error)
}
if result.RowsAffected == 0 {
return &NoRowsAffected{"no model found for round ID, and check_service id provided"}
}
return nil
}
func (c *checkRepo) GetAll(ctx context.Context) ([]*check.Check, error) {
checks := make([]*check.Check, 0)
err := c.db.WithContext(ctx).Find(&checks).Error
if err != nil {
return nil, err
}
return checks, nil
}
func (c *checkRepo) GetByID(ctx context.Context, roundID uint64, serviceID uuid.UUID) (*check.Check, error) {
chck := &check.Check{}
err := c.db.WithContext(ctx).Where("round_id = ? AND service_id = ?", roundID, serviceID).First(&chck).Error
if err != nil {
return nil, err
}
return chck, nil
}
func (c *checkRepo) Store(ctx context.Context, chck []*check.Check) error {
ReplaceInvalidCharacters(chck)
err := c.db.WithContext(ctx).Create(chck).Error
if err != nil {
return err
}
return nil
}
func (c *checkRepo) Upsert(ctx context.Context, chck []*check.Check) error {
ReplaceInvalidCharacters(chck)
err := c.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(chck).Error
if err != nil {
return err
}
return nil
}
func (c *checkRepo) TruncateTable(ctx context.Context) (err error) {
err = testutil.TruncateTable(ctx, &check.Check{}, c.db)
if err != nil {
return err
}
return nil
}
func ReplaceInvalidCharacters(chck []*check.Check) {
for i := range chck {
chck[i].Log = strings.ToValidUTF8(chck[i].Log, "�")
chck[i].Err = strings.ToValidUTF8(chck[i].Err, "�")
}
}