/
int_value_repository.go
123 lines (101 loc) · 3.16 KB
/
int_value_repository.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
package gorm
import (
"context"
"errors"
"sort"
"github.com/yaruz/app/pkg/yarus_platform/data/domain/int_value"
"github.com/yaruz/app/pkg/yarus_platform/yaruserror"
"github.com/yaruz/app/internal/pkg/apperror"
"gorm.io/gorm"
minipkg_gorm "github.com/minipkg/db/gorm"
"github.com/minipkg/selection_condition"
)
// IntValueRepository is a repository for the model entity
type IntValueRepository struct {
repository
}
var _ int_value.Repository = (*IntValueRepository)(nil)
// New creates a new IntValueRepository
func NewIntValueRepository(repository *repository) (*IntValueRepository, error) {
return &IntValueRepository{repository: *repository}, nil
}
// Query retrieves the records with the specified offset and limit from the database.
func (r *IntValueRepository) queryTx(ctx context.Context, cond *selection_condition.SelectionCondition, tx *gorm.DB) ([]int_value.IntValue, error) {
items := []int_value.IntValue{}
db := minipkg_gorm.Conditions(tx, cond)
if db.Error != nil {
return nil, db.Error
}
err := db.Find(&items).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return items, yaruserror.ErrNotFound
}
return nil, err
}
return items, err
}
func (r *IntValueRepository) BatchDeleteTx(ctx context.Context, cond *selection_condition.SelectionCondition, tx *gorm.DB) (err error) {
db := &minipkg_gorm.DB{GormDB: tx}
if db, err = db.SchemeInitWithContext(ctx, r.model); err != nil {
return err
}
gormDB := minipkg_gorm.Conditions(db.DB(), cond)
if gormDB.Error != nil {
return gormDB.Error
}
if err = gormDB.Delete(&int_value.IntValue{}).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return apperror.ErrNotFound
}
}
return err
}
func (r *IntValueRepository) BatchSaveChangesTx(ctx context.Context, entityID uint, values []int_value.IntValue, langID uint, tx *gorm.DB) (err error) {
for i := range values {
values[i].EntityID = entityID
}
db := &minipkg_gorm.DB{GormDB: tx}
if db, err = db.SchemeInitWithContext(ctx, r.model); err != nil {
return err
}
return db.DB().Transaction(func(tx *gorm.DB) error {
oldValues, err := r.queryTx(ctx, &selection_condition.SelectionCondition{
Where: &int_value.IntValue{
EntityID: entityID,
},
}, tx)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
oldValues = make([]int_value.IntValue, 0)
}
oldValuesIds := make([]int, 0, len(oldValues))
for _, oldValue := range oldValues {
oldValuesIds = append(oldValuesIds, int(oldValue.ID))
}
sort.Ints(oldValuesIds)
for _, value := range values {
i := sort.SearchInts(oldValuesIds, int(value.ID))
if i < len(oldValuesIds) && oldValuesIds[i] == int(value.ID) {
oldValuesIds = append(oldValuesIds[:i], oldValuesIds[i+1:]...)
}
}
if len(oldValuesIds) > 0 {
if err := r.BatchDeleteTx(ctx, &selection_condition.SelectionCondition{
Where: selection_condition.WhereCondition{
Field: "ID",
Condition: selection_condition.ConditionIn,
Value: selection_condition.IntSlice2EmptyInterfaceSlice(oldValuesIds),
},
}, tx); err != nil {
return err
}
}
if len(values) > 0 {
return tx.Save(values).Error
}
return nil
})
}