/
service.go
176 lines (141 loc) · 4 KB
/
service.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package service
import (
"fmt"
"github.com/dimasbagussusilo/gin-golang-boilerplate/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
)
type Entity interface {
TableName() string
}
type Repository struct {
entity Entity
db *gorm.DB
}
type IRepository interface {
FindOne(ctx *gin.Context, entity Entity, id int, dbTransaction *gorm.DB) error
FindAll(ctx *gin.Context, entities any, options *OptionQuery, dbTransaction *gorm.DB) (*utils.Pagination, error)
Create(ctx *gin.Context, form Entity, dbTransaction *gorm.DB) (Entity, error)
Update(ctx *gin.Context, id int, form Entity, dbTransaction *gorm.DB) (Entity, error)
Delete(ctx *gin.Context, id int, dbTransaction *gorm.DB) error
CustomQuery(ctx *gin.Context) *gorm.DB
}
type OptionQuery struct {
Page int
Limit int
Filter map[string]any
Search []string
Order string
}
func NewRepository(entity Entity, db *gorm.DB) IRepository {
return &Repository{
entity: entity,
db: db,
}
}
func (r *Repository) getDB(dbTransaction *gorm.DB) *gorm.DB {
if dbTransaction != nil {
return dbTransaction
}
return r.db
}
func (r *Repository) FindOne(ctx *gin.Context, entity Entity, id int, dbTransaction *gorm.DB) error {
db := r.getDB(dbTransaction)
err := db.Model(r.entity).Where("id = ?", id).First(entity).Error
if err != nil {
return err
}
return nil
}
func (r *Repository) FindAll(ctx *gin.Context, entities any, options *OptionQuery, dbTransaction *gorm.DB) (*utils.Pagination, error) {
page := 1
limit := 10
db := r.getDB(dbTransaction)
var count int64
query := db.Model(r.entity)
if options != nil {
if options.Page != 0 {
page = options.Page
}
if options.Limit != 0 {
limit = options.Limit
}
if len(options.Filter) != 0 {
if customFields, ok := options.Filter["custom_fields"]; ok {
for i := range customFields.([]clause.Expr) {
query = query.Where(customFields.([]clause.Expr)[i])
}
delete(options.Filter, "custom_fields")
}
query = query.Where(options.Filter)
}
searchParam := ctx.Query("search")
if searchParam != "" {
if options.Search != nil {
var searchQueries string
for i := range options.Search {
if strings.ContainsAny(searchParam, ";") {
searchParam = strings.Join(strings.Split(searchParam, ";"), "")
}
if i == 0 {
searchQueries += fmt.Sprintf("LOWER(%v) LIKE %v", options.Search[i], "'%"+strings.ToLower(searchParam)+"%'")
} else {
searchQueries += fmt.Sprintf(" OR LOWER(%v) LIKE %v", options.Search[i], "'%"+strings.ToLower(searchParam)+"%'")
}
}
query = query.Where(searchQueries)
}
}
}
query.Count(&count)
query = query.Limit(limit).Offset((page - 1) * limit)
if options != nil && options.Order != "" {
query = utils.SortBy(options.Order, query)
}
res := query.Find(entities)
if res.Error != nil {
fmt.Printf("error, %+v\n", res.Error)
return nil, res.Error
}
pagination := utils.Paginate(count, page, limit)
return pagination, nil
}
func (r *Repository) Create(ctx *gin.Context, form Entity, dbTransaction *gorm.DB) (Entity, error) {
db := r.getDB(dbTransaction)
result := db.Table(r.entity.TableName()).Select("*").Create(form)
if result.Error != nil {
return nil, result.Error
}
return form, nil
}
func (r *Repository) Update(ctx *gin.Context, id int, form Entity, dbTransaction *gorm.DB) (Entity, error) {
db := r.getDB(dbTransaction)
entity := r.entity
err := db.Model(entity).Where("id = ?", id).First(entity).Error
if err != nil {
return nil, err
}
result := db.Save(form)
if result.Error != nil {
return nil, result.Error
}
return entity, nil
}
func (r *Repository) Delete(ctx *gin.Context, id int, dbTransaction *gorm.DB) error {
db := r.getDB(dbTransaction)
entity := r.entity
err := db.Model(entity).Where("id = ?", id).First(entity).Error
if err != nil {
return err
}
result := db.Delete(entity)
if result.Error != nil {
return result.Error
}
return nil
}
func (r *Repository) CustomQuery(ctx *gin.Context) *gorm.DB {
return r.db
}