-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
88 lines (72 loc) · 1.85 KB
/
postgres.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
package gormDb
//special thanks to tomi for the initial code
import (
"errors"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Repository[T any] struct {
url string
db *gorm.DB
}
func NewPostgresRepository[T any](url string) (Repository[T], error) {
db, err := gorm.Open(postgres.Open(url), &gorm.Config{})
if err != nil {
return Repository[T]{}, err
}
return Repository[T]{url: url, db: db}, nil
}
func (r *Repository[T]) Init(models []interface{}) error {
return r.db.AutoMigrate(models...)
}
func (r *Repository[T]) Put(data *T) error {
result := r.db.Save(data)
if result.Error != nil {
return result.Error
}
return nil
}
func (r *Repository[T]) Get(id int) (T, error) {
var data T
result := r.db.First(&data, id)
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return data, result.Error
}
return data, nil
}
func (r *Repository[T]) Delete(id int) (T, error) {
var data T
result := r.db.Delete(&data, id)
if result.Error != nil {
return data, result.Error
}
return data, nil
}
func (r *Repository[T]) GetFiltered(filters map[string]string, orderBy string, limit int, offset int) ([]T, int64, error) {
var model T
result := make([]T, 0)
var totalCount int64
queryCount := r.db.Model(model)
queryCount = applyFilters(queryCount, filters)
err := queryCount.Count(&totalCount).Error
if err != nil {
return result, 0, err
}
query := r.db.Model(result)
query = applyFilters(query, filters)
if orderBy != "" {
query = query.Order(orderBy)
}
query = query.Limit(limit).Offset(offset)
if err := query.Find(result).Error; err != nil {
return result, 0, err
}
return result, totalCount, nil
}
// applyFilters apply filters to the query
func applyFilters(query *gorm.DB, filters map[string]string) *gorm.DB {
for campo, valor := range filters {
query = query.Where(campo, valor)
}
return query
}