-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.go
More file actions
127 lines (111 loc) · 2.83 KB
/
mutation.go
File metadata and controls
127 lines (111 loc) · 2.83 KB
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
package sqlorm
import "gorm.io/gorm"
func (repo *Repository[M]) Create(val interface{}) (*M, error) {
input := MapOne[M](val)
result := repo.DB.Create(input)
if result.Error != nil {
return nil, result.Error
}
return input, nil
}
func (repo *Repository[M]) BatchCreate(val interface{}, size int) ([]*M, error) {
input := MapMany[M](val)
result := repo.DB.CreateInBatches(input, size)
if result.Error != nil {
return nil, result.Error
}
return input, nil
}
func (repo *Repository[M]) UpdateOne(where interface{}, val interface{}) (*M, error) {
var record M
input := MapOne[M](val)
result := repo.DB.Model(&record).Where(where).Updates(input)
if result.Error != nil {
return nil, result.Error
}
return input, nil
}
func (repo *Repository[M]) UpdateByID(id any, val interface{}) (*M, error) {
return repo.UpdateOne(map[string]any{"id": id}, val)
}
func (repo *Repository[M]) UpdateMany(where interface{}, val interface{}) error {
var model M
input := MapOne[M](val)
tx := repo.DB.Model(&model)
if where != nil {
tx = tx.Where(where)
} else {
tx = tx.Where("1 = 1")
}
result := tx.Updates(input)
if result.Error != nil {
return result.Error
}
return nil
}
func (repo *Repository[M]) DeleteOne(where interface{}, isForceDelete ...bool) error {
withDeleted := false
if len(isForceDelete) > 0 && isForceDelete[0] {
withDeleted = true
}
record, err := repo.FindOne(where, FindOneOptions{
WithDeleted: withDeleted,
})
if err != nil {
return err
}
if record == nil {
return gorm.ErrRecordNotFound
}
tx := repo.DB
if withDeleted {
tx = tx.Unscoped()
}
result := tx.Delete(record)
if result.Error != nil {
return result.Error
}
return nil
}
func (repo *Repository[M]) DeleteByID(id any, isForceDelete ...bool) error {
return repo.DeleteOne(map[string]any{"id": id}, isForceDelete...)
}
func (repo *Repository[M]) DeleteMany(where interface{}, isForceDelete ...bool) error {
var model M
tx := repo.DB
if where != nil {
tx = tx.Where(where)
} else {
tx = tx.Where("1 = 1")
}
if len(isForceDelete) > 0 && isForceDelete[0] {
tx = tx.Unscoped()
}
result := tx.Delete(&model)
if result.Error != nil {
return result.Error
}
return nil
}
func (repo *Repository[M]) Increment(id any, field string, value int) error {
record, err := repo.FindOne(map[string]interface{}{"id": id}, FindOneOptions{})
if err != nil {
return err
}
result := repo.DB.Model(record).Update(field, gorm.Expr(field+" + ?", value))
if result.Error != nil {
return result.Error
}
return nil
}
func (repo *Repository[M]) Decrement(id any, field string, value int) error {
record, err := repo.FindOne(map[string]interface{}{"id": id}, FindOneOptions{})
if err != nil {
return err
}
result := repo.DB.Model(record).Update(field, gorm.Expr(field+" - ?", value))
if result.Error != nil {
return result.Error
}
return nil
}