-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
130 lines (110 loc) · 3.19 KB
/
gorm.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
124
125
126
127
128
129
130
package gosdk
import (
"gorm.io/gorm"
"math"
)
type Entity interface {
TableName() string
}
func Pagination[T Entity](value *[]T, meta *PaginationMetadata, db *gorm.DB, scopes ...func(db *gorm.DB) *gorm.DB) func(db *gorm.DB) *gorm.DB {
var totalItems int64
db.Model(&value).
Scopes(scopes...).
Count(&totalItems)
meta.TotalItem = int(totalItems)
totalPages := math.Ceil(float64(totalItems) / float64(meta.GetItemPerPage()))
meta.TotalPage = int(totalPages)
return func(db *gorm.DB) *gorm.DB {
return db.Offset(meta.GetOffset()).Limit(meta.ItemsPerPage)
}
}
func FindOneByID[T Entity](id string, entity T) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.
First(&entity, "id = ?", id)
}
}
func UpdateWithoutResult[T Entity](id string, entity T) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.
Where(id, "id = ?", id).
Updates(&entity)
}
}
func UpdateByIDWithResult[T Entity](id string, entity T) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.
Where(id, "id = ?", id).
Updates(&entity).
First(&entity, "id = ?", id)
}
}
func DeleteWithResult[T Entity](id string, entity T) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.
First(&entity, "id = ?", id).
Delete(&entity, "id = ?", id)
}
}
func DeleteWithoutResult[T Entity](id string, entity T) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.
Delete(&entity, "id = ?", id)
}
}
type GormRepository[T Entity] interface {
FindAll(metadata *PaginationMetadata, entities *[]T, scope ...func(db *gorm.DB) *gorm.DB) error
FindOne(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error
Create(entity T, scope ...func(db *gorm.DB) *gorm.DB) error
Update(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error
Delete(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error
GetDB() *gorm.DB
}
type gormRepository[T Entity] struct {
db *gorm.DB
}
func NewGormRepository[T Entity](db *gorm.DB) GormRepository[T] {
return &gormRepository[T]{
db: db,
}
}
func (r *gormRepository[T]) GetDB() *gorm.DB {
return r.db
}
func (r *gormRepository[T]) FindAll(metadata *PaginationMetadata, entities *[]T, scope ...func(db *gorm.DB) *gorm.DB) error {
if err := r.db.
Scopes(Pagination[T](entities, metadata, r.db, scope...)).
Find(&entities).
Error; err != nil {
return err
}
metadata.ItemCount = len(*entities)
return nil
}
func (r *gormRepository[T]) FindOne(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error {
return r.db.
Scopes(scope...).
First(entity, "id = ?", id).
Error
}
func (r *gormRepository[T]) Create(entity T, scope ...func(db *gorm.DB) *gorm.DB) error {
return r.db.
Scopes(scope...).
Create(entity).
Error
}
func (r *gormRepository[T]) Update(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error {
return r.db.
Scopes(scope...).
Where(id, "id = ?", id).
Updates(&entity).
First(&entity, "id = ?", id).
Error
}
func (r *gormRepository[T]) Delete(id string, entity T, scope ...func(db *gorm.DB) *gorm.DB) error {
return r.db.
Scopes(scope...).
First(&entity, "id = ?", id).
Delete(&entity).
Error
}