-
Notifications
You must be signed in to change notification settings - Fork 15
/
repository.go
116 lines (93 loc) · 2.68 KB
/
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
package gorm_generics
import (
"context"
"gorm.io/gorm"
)
type GormModel[E any] interface {
ToEntity() E
FromEntity(entity E) interface{}
}
func NewRepository[M GormModel[E], E any](db *gorm.DB) *GormRepository[M, E] {
return &GormRepository[M, E]{
db: db,
}
}
type GormRepository[M GormModel[E], E any] struct {
db *gorm.DB
}
func (r *GormRepository[M, E]) Insert(ctx context.Context, entity *E) error {
var start M
model := start.FromEntity(*entity).(M)
err := r.db.WithContext(ctx).Create(&model).Error
if err != nil {
return err
}
*entity = model.ToEntity()
return nil
}
func (r *GormRepository[M, E]) Delete(ctx context.Context, entity *E) error {
var start M
model := start.FromEntity(*entity).(M)
err := r.db.WithContext(ctx).Delete(model).Error
if err != nil {
return err
}
return nil
}
func (r *GormRepository[M, E]) DeleteById(ctx context.Context, id any) error {
var start M
err := r.db.WithContext(ctx).Delete(&start, &id).Error
if err != nil {
return err
}
return nil
}
func (r *GormRepository[M, E]) Update(ctx context.Context, entity *E) error {
var start M
model := start.FromEntity(*entity).(M)
err := r.db.WithContext(ctx).Save(&model).Error
if err != nil {
return err
}
*entity = model.ToEntity()
return nil
}
func (r *GormRepository[M, E]) FindByID(ctx context.Context, id any) (E, error) {
var model M
err := r.db.WithContext(ctx).First(&model, id).Error
if err != nil {
return *new(E), err
}
return model.ToEntity(), nil
}
func (r *GormRepository[M, E]) Find(ctx context.Context, specifications ...Specification) ([]E, error) {
return r.FindWithLimit(ctx, -1, -1, specifications...)
}
func (r *GormRepository[M, E]) Count(ctx context.Context, specifications ...Specification) (i int64, err error) {
model := new(M)
err = r.getPreWarmDbForSelect(ctx, specifications...).Model(model).Count(&i).Error
return
}
func (r *GormRepository[M, E]) getPreWarmDbForSelect(ctx context.Context, specification ...Specification) *gorm.DB {
dbPrewarm := r.db.WithContext(ctx)
for _, s := range specification {
dbPrewarm = dbPrewarm.Where(s.GetQuery(), s.GetValues()...)
}
return dbPrewarm
}
func (r *GormRepository[M, E]) FindWithLimit(ctx context.Context, limit int, offset int, specifications ...Specification) ([]E, error) {
var models []M
dbPrewarm := r.getPreWarmDbForSelect(ctx, specifications...)
err := dbPrewarm.Limit(limit).Offset(offset).Find(&models).Error
if err != nil {
return nil, err
}
result := make([]E, 0, len(models))
for _, row := range models {
result = append(result, row.ToEntity())
}
return result, nil
}
func (r *GormRepository[M, E]) FindAll(ctx context.Context) ([]E, error) {
return r.FindWithLimit(ctx, -1, -1)
}